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.

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