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.

175 lines
5.5 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. try {
  27. $this->driver = $this->getMockForAbstractClass('DbDriver', array($conf));
  28. }
  29. catch (GeneralException $expected) {
  30. $this->fail($expected->getMessage());
  31. }
  32. $this->assertInstanceOf('DbDriver', $this->driver);
  33. }
  34. public function testConstructWrongConfig()
  35. {
  36. $conf = array('hostname' => 'localhost', 'database' => 'db');
  37. $this->setExpectedException('GeneralException', 'Configuration must have a "username"');
  38. $this->getMockForAbstractClass('DbDriver', array($conf));
  39. }
  40. public function testGetConnection()
  41. {
  42. $this->assertNull($this->driver->getConnection());
  43. }
  44. public function testBeginTransaction()
  45. {
  46. $this->assertSame($this->driver, $this->driver->beginTransaction());
  47. }
  48. public function testCommit()
  49. {
  50. $this->assertSame($this->driver, $this->driver->commit());
  51. }
  52. public function testRollback()
  53. {
  54. $this->assertSame($this->driver, $this->driver->rollback());
  55. }
  56. public function testQuery()
  57. {
  58. $this->setDriverPrepareFunction();
  59. $stmt = $this->driver->query('SELECT * FROM table');
  60. $this->assertInstanceOf('DbStmt', $stmt);
  61. $this->assertSame('SELECT * FROM table', $stmt->string());
  62. $stmt = $this->driver->query('SELECT * FROM table', 'simple');
  63. $this->assertInstanceOf('DbStmt', $stmt);
  64. $this->assertSame('SELECT * FROM table', $stmt->string());
  65. }
  66. public function testInsert()
  67. {
  68. $this->setDriverPrepareFunction()
  69. ->setDriverQuoteFunction();
  70. $bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
  71. $sql = $this->driver->insert('users', $bind);
  72. $this->assertSame('INSERT INTO `users` (`table`.`name`, `chair`.`age`, `height`) VALUES (tony, 21, 185)', $sql);
  73. }
  74. public function testUpdate()
  75. {
  76. $this->setDriverPrepareFunction()
  77. ->setDriverQuoteFunction();
  78. $bind = array('table.name' => 'tony', 'chair.age' => 21, 'height' => 185);
  79. $sql = $this->driver->update('users', $bind);
  80. $this->assertSame('UPDATE `users` SET `table`.`name`=tony, `chair`.`age`=21, `height`=185', $sql);
  81. }
  82. public function testDeleteNoWHERE()
  83. {
  84. $this->setDriverPrepareFunction();
  85. $sql = $this->driver->delete('users');
  86. $this->assertSame('DELETE FROM `users`', $sql);
  87. }
  88. public function testDeleteWithWHEREArray()
  89. {
  90. $this->setDriverPrepareFunction()
  91. ->setDriverQuoteFunction();
  92. $sql = $this->driver->delete('users', array('name?tony' => new DbExpr('='), 'height?185' => '>'));
  93. $this->assertSame('DELETE FROM `users` WHERE name=tony AND height>185', $sql);
  94. }
  95. public function testDeleteWithWHERESimpleCond()
  96. {
  97. $this->setDriverPrepareFunction();
  98. $sql = $this->driver->delete('users', 'name=tony');
  99. $this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
  100. }
  101. public function testDeleteWithWHEREKeyArray()
  102. {
  103. $this->setDriverPrepareFunction();
  104. $sql = $this->driver->delete('users', array('name=tony' => 0));
  105. $this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
  106. }
  107. public function testDeleteWithWHEREDbExpr()
  108. {
  109. $this->setDriverPrepareFunction();
  110. $sql = $this->driver->delete('users', new DbExpr('name=tony'));
  111. $this->assertSame('DELETE FROM `users` WHERE name=tony', $sql);
  112. }
  113. protected function setDriverPrepareFunction()
  114. {
  115. $this->driver
  116. ->expects($this->any())
  117. ->method('prepare')
  118. ->with($this->anything())
  119. ->will($this->returnCallback(array($this, 'dbDriverPrepare')));
  120. return $this;
  121. }
  122. protected function setDriverQuoteFunction()
  123. {
  124. $this->driver
  125. ->expects($this->any())
  126. ->method('driverQuote')
  127. ->with($this->anything())
  128. ->will($this->returnArgument(0));
  129. return $this;
  130. }
  131. public function dbDriverPrepare($sql)
  132. {
  133. $stmt = $this->getMock('DbStmt', array('execute', 'string', 'affectedRows'));
  134. $stmt->expects($this->any())
  135. ->method('execute')
  136. ->with($this->anything());
  137. $stmt->expects($this->any())
  138. ->method('string')
  139. ->will($this->returnValue($sql));
  140. $stmt->expects($this->any())
  141. ->method('affectedRows')
  142. ->will($this->returnValue($sql));
  143. return $stmt;
  144. }
  145. }