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.

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