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.

203 lines
7.8 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-7
  8. *
  9. * Unit tests for MySQLiDriver class
  10. */
  11. require_once dirname(__FILE__) . '/../../model/Db.php';
  12. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  13. require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php';
  14. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  15. require_once dirname(__FILE__) . '/../../model/MySQLiDriver.php';
  16. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  17. class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase
  18. {
  19. static private $pdo = null;
  20. private $conn = null;
  21. private $conf = array();
  22. protected function getConnection()
  23. {
  24. if ($this->conn === null) {
  25. if (self::$pdo == null) {
  26. self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
  27. }
  28. $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
  29. }
  30. $this->conf = array('hostname' => 'localhost', 'database' => $GLOBALS['DB_DBNAME'], 'username' => $GLOBALS['DB_USER'], 'password' => $GLOBALS['DB_PASSWD']);
  31. return $this->conn;
  32. }
  33. protected function getDataSet()
  34. {
  35. return $this->createFlatXMLDataSet(dirname(__FILE__) . '/testData.xml');
  36. }
  37. public function run(PHPUnit_Framework_TestResult $result = NULL)
  38. {
  39. $this->setPreserveGlobalState(false);
  40. return parent::run($result);
  41. }
  42. public function testDriver()
  43. {
  44. if (!defined('DEBUG')) {
  45. define('DEBUG', false);
  46. }
  47. $driver = new MySQLiDriver($this->conf);
  48. $queryTable = $this->getConnection()->createQueryTable(
  49. 'table', 'SELECT * FROM `table`'
  50. );
  51. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  52. ->getTable("table");
  53. $this->assertTablesEqual($expectedTable, $queryTable);
  54. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  55. $this->assertEquals(3, $this->getConnection()->getRowCount('table'));
  56. }
  57. public function testGetConnection()
  58. {
  59. if (!defined('DEBUG')) {
  60. define('DEBUG', false);
  61. }
  62. $driver = new MySQLiDriver($this->conf);
  63. $this->assertInstanceOf('mysqli', $driver->getConnection());
  64. $this->assertTrue($driver->isConnected());
  65. }
  66. public function testGetConnectionWrongConfig()
  67. {
  68. if (!defined('DEBUG')) {
  69. define('DEBUG', false);
  70. }
  71. $this->setExpectedException('GeneralException', 'Unknown database \'nodb\'');
  72. $this->conf['database'] = 'nodb';
  73. $driver = new MySQLiDriver($this->conf);
  74. $this->assertNull('mysqli', $driver->getConnection());
  75. }
  76. public function testDisconnect()
  77. {
  78. if (!defined('DEBUG')) {
  79. define('DEBUG', false);
  80. }
  81. $driver = new MySQLiDriver($this->conf);
  82. $driver->disconnect();
  83. $this->assertAttributeEquals(null, 'connection', $driver);
  84. $this->assertInstanceOf('mysqli', $driver->getConnection());
  85. $this->assertAttributeInstanceOf('mysqli', 'connection', $driver);
  86. $driver->disconnect();
  87. $this->assertAttributeEquals(null, 'connection', $driver);
  88. }
  89. public function testInsert()
  90. {
  91. if (!defined('DEBUG')) {
  92. define('DEBUG', false);
  93. }
  94. $driver = new MySQLiDriver($this->conf);
  95. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  96. $this->assertEquals(3, $driver->getInsertId());
  97. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  98. $this->assertEquals(4, $driver->getInsertId());
  99. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  100. $this->assertEquals(5, $driver->getInsertId());
  101. $this->assertEquals(2, $driver->insert('table', array('id' => '5', 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'), array('id' => 8)));
  102. $this->assertEquals(8, $driver->getInsertId());
  103. }
  104. /**
  105. * @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used
  106. */
  107. public function testGetInsertId()
  108. {
  109. if (!defined('DEBUG')) {
  110. define('DEBUG', false);
  111. }
  112. $driver = new MySQLiDriver($this->conf);
  113. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  114. $this->assertEquals(3, $driver->getInsertId());
  115. }
  116. public function testTransaction()
  117. {
  118. if (!defined('DEBUG')) {
  119. define('DEBUG', false);
  120. }
  121. $driver = new MySQLiDriver($this->conf);
  122. $queryTable = $this->getConnection()->createQueryTable(
  123. 'table', 'SELECT * FROM `table`'
  124. );
  125. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  126. ->getTable("table");
  127. $this->assertTablesEqual($expectedTable, $queryTable);
  128. $driver->getConnection();
  129. $driver->beginTransaction();
  130. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  131. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  132. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  133. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  134. $queryTable = $this->getConnection()->createQueryTable(
  135. 'table', 'SELECT * FROM `table`'
  136. );
  137. $driver->commit();
  138. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testDataAfterCommit.xml')
  139. ->getTable("table");
  140. $this->assertTablesEqual($expectedTable, $queryTable);
  141. }
  142. public function testRollback()
  143. {
  144. if (!defined('DEBUG')) {
  145. define('DEBUG', false);
  146. }
  147. $driver = new MySQLiDriver($this->conf);
  148. $queryTable = $this->getConnection()->createQueryTable(
  149. 'table', 'SELECT * FROM `table`'
  150. );
  151. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  152. ->getTable("table");
  153. $this->assertTablesEqual($expectedTable, $queryTable);
  154. $driver->getConnection();
  155. $driver->beginTransaction();
  156. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  157. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  158. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  159. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  160. $driver->rollback();
  161. $queryTable = $this->getConnection()->createQueryTable(
  162. 'table', 'SELECT * FROM `table`'
  163. );
  164. $this->assertTablesEqual($expectedTable, $queryTable);
  165. }
  166. }