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.

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