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.

207 lines
8.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-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. class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase
  17. {
  18. static private $pdo = null;
  19. private $conn = null;
  20. protected function getConnection()
  21. {
  22. if ($this->conn === null) {
  23. if (self::$pdo == null) {
  24. self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
  25. }
  26. $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
  27. }
  28. return $this->conn;
  29. }
  30. protected function getDataSet()
  31. {
  32. return $this->createFlatXMLDataSet(dirname(__FILE__) . '/testData.xml');
  33. }
  34. public function run(PHPUnit_Framework_TestResult $result = NULL)
  35. {
  36. $this->setPreserveGlobalState(false);
  37. return parent::run($result);
  38. }
  39. public function testDriver()
  40. {
  41. if (!defined('DEBUG')) {
  42. define('DEBUG', false);
  43. }
  44. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  45. $driver = new MySQLiDriver($conf);
  46. $queryTable = $this->getConnection()->createQueryTable(
  47. 'table', 'SELECT * FROM `table`'
  48. );
  49. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  50. ->getTable("table");
  51. $this->assertTablesEqual($expectedTable, $queryTable);
  52. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  53. $this->assertEquals(3, $this->getConnection()->getRowCount('table'));
  54. }
  55. public function testGetConnection()
  56. {
  57. if (!defined('DEBUG')) {
  58. define('DEBUG', false);
  59. }
  60. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  61. $driver = new MySQLiDriver($conf);
  62. $this->assertInstanceOf('mysqli', $driver->getConnection());
  63. $this->assertTrue($driver->isConnected());
  64. }
  65. /**
  66. * @expectedException Exception
  67. * @expectedExceptionMessage Unknown database
  68. */
  69. public function testGetConnectionWrongConfig()
  70. {
  71. if (!defined('DEBUG')) {
  72. define('DEBUG', false);
  73. }
  74. $conf = array('hostname' => 'localhost', 'database' => 'nodb', 'username' => 'root', 'password' => '1234');
  75. $driver = new MySQLiDriver($conf);
  76. $this->assertNull('mysqli', $driver->getConnection());
  77. }
  78. public function testDisconnect()
  79. {
  80. if (!defined('DEBUG')) {
  81. define('DEBUG', false);
  82. }
  83. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  84. $driver = new MySQLiDriver($conf);
  85. $driver->disconnect();
  86. $this->assertAttributeEquals(null, 'connection', $driver);
  87. $this->assertInstanceOf('mysqli', $driver->getConnection());
  88. $this->assertAttributeInstanceOf('mysqli', 'connection', $driver);
  89. $driver->disconnect();
  90. $this->assertAttributeEquals(null, 'connection', $driver);
  91. }
  92. public function testInsert()
  93. {
  94. if (!defined('DEBUG')) {
  95. define('DEBUG', false);
  96. }
  97. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  98. $driver = new MySQLiDriver($conf);
  99. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  100. $this->assertEquals(3, $driver->getInsertId());
  101. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  102. $this->assertEquals(4, $driver->getInsertId());
  103. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  104. $this->assertEquals(5, $driver->getInsertId());
  105. $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)));
  106. $this->assertEquals(8, $driver->getInsertId());
  107. }
  108. /**
  109. * @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used
  110. */
  111. public function testGetInsertId()
  112. {
  113. if (!defined('DEBUG')) {
  114. define('DEBUG', false);
  115. }
  116. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  117. $driver = new MySQLiDriver($conf);
  118. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  119. $this->assertEquals(3, $driver->getInsertId());
  120. }
  121. public function testTransaction()
  122. {
  123. if (!defined('DEBUG')) {
  124. define('DEBUG', false);
  125. }
  126. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  127. $driver = new MySQLiDriver($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. public function testRollback()
  149. {
  150. if (!defined('DEBUG')) {
  151. define('DEBUG', false);
  152. }
  153. $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234');
  154. $driver = new MySQLiDriver($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. }