You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.3 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-3
  8. *
  9. * Unit tests for DbDriver class
  10. */
  11. require_once dirname(__FILE__) . '/../../model/DbExpr.php';
  12. require_once dirname(__FILE__) . '/../../model/Db.php';
  13. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  14. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  15. class DbDriverTest extends PHPUnit_Framework_TestCase
  16. {
  17. private $driver;
  18. public function setUp()
  19. {
  20. $conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  21. $this->driver = $this->getMockForAbstractClass('DbDriver', array($conf));
  22. }
  23. public function testConstruct()
  24. {
  25. $conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234');
  26. $this->driver = $this->getMockForAbstractClass('DbDriver', array($conf));
  27. }
  28. /**
  29. * @expectedException GeneralException
  30. * @expectedExceptionMessage Configuration must have a "username".
  31. */
  32. public function testConstructWrongConfig()
  33. {
  34. $conf = array('hostname' => 'localhost', 'database' => 'db');
  35. $this->getMockForAbstractClass('DbDriver', array($conf));
  36. }
  37. public function testGetConnection()
  38. {
  39. $this->assertNull($this->driver->getConnection());
  40. }
  41. public function testBeginTransaction()
  42. {
  43. $this->assertEquals($this->driver, $this->driver->beginTransaction());
  44. }
  45. public function testCommit()
  46. {
  47. $this->assertEquals($this->driver, $this->driver->commit());
  48. }
  49. public function testRollback()
  50. {
  51. $this->assertEquals($this->driver, $this->driver->rollback());
  52. }
  53. public function testQuery()
  54. {
  55. $this->setDriverPrepareFunction();
  56. $stmt = $this->driver->query('SELECT * FROM table');
  57. $this->assertInstanceOf('DbStmt', $stmt);
  58. $this->assertEquals('SELECT * FROM table', $stmt->string());
  59. $stmt = $this->driver->query('SELECT * FROM table', 'simple');
  60. $this->assertInstanceOf('DbStmt', $stmt);
  61. $this->assertEquals('SELECT * FROM table', $stmt->string());
  62. }
  63. public function testInsert()
  64. {
  65. $this->setDriverPrepareFunction()
  66. ->setDriverQuoteFunction();
  67. $bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
  68. $sql = $this->driver->insert('users', $bind);
  69. $this->assertEquals('INSERT INTO `users` (`table`.`name`, `chair`.`age`, `height`) VALUES (tony, 21, 185)', $sql);
  70. }
  71. public function testUpdate()
  72. {
  73. $this->setDriverPrepareFunction()
  74. ->setDriverQuoteFunction();
  75. $bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
  76. $sql = $this->driver->update('users', $bind);
  77. $this->assertEquals('UPDATE `users` SET `table`.`name`=tony, `chair`.`age`=21, `height`=185', $sql);
  78. }
  79. public function testDeleteNoWHERE()
  80. {
  81. $this->setDriverPrepareFunction();
  82. $sql = $this->driver->delete('users');
  83. $this->assertEquals('DELETE FROM `users`', $sql);
  84. }
  85. public function testDeleteWithWHEREArray()
  86. {
  87. $this->setDriverPrepareFunction()
  88. ->setDriverQuoteFunction();
  89. $sql = $this->driver->delete('users', array('name?tony' => new DbExpr('='), 'height?185' => '>'));
  90. $this->assertEquals('DELETE FROM `users` WHERE name=tony AND height>185', $sql);
  91. }
  92. public function testDeleteWithWHERESimpleCond()
  93. {
  94. $this->setDriverPrepareFunction();
  95. $sql = $this->driver->delete('users', 'name=tony');
  96. $this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql);
  97. }
  98. public function testDeleteWithWHEREKeyArray()
  99. {
  100. $this->setDriverPrepareFunction();
  101. $sql = $this->driver->delete('users', array('name=tony' => 0));
  102. $this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql);
  103. }
  104. public function testDeleteWithWHEREDbExpr()
  105. {
  106. $this->setDriverPrepareFunction();
  107. $sql = $this->driver->delete('users', new DbExpr('name=tony'));
  108. $this->assertEquals('DELETE FROM `users` WHERE name=tony', $sql);
  109. }
  110. protected function setDriverPrepareFunction()
  111. {
  112. $this->driver
  113. ->expects($this->any())
  114. ->method('prepare')
  115. ->with($this->anything())
  116. ->will($this->returnCallback(array($this, 'dbDriverPrepare')));
  117. return $this;
  118. }
  119. protected function setDriverQuoteFunction()
  120. {
  121. $this->driver
  122. ->expects($this->any())
  123. ->method('driverQuote')
  124. ->with($this->anything())
  125. ->will($this->returnArgument(0));
  126. return $this;
  127. }
  128. public function dbDriverPrepare($sql)
  129. {
  130. $stmt = $this->getMock('DbStmt', array('execute', 'string', 'affectedRows'));
  131. $stmt->expects($this->any())
  132. ->method('execute')
  133. ->with($this->anything());
  134. $stmt->expects($this->any())
  135. ->method('string')
  136. ->will($this->returnValue($sql));
  137. $stmt->expects($this->any())
  138. ->method('affectedRows')
  139. ->will($this->returnValue($sql));
  140. return $stmt;
  141. }
  142. }