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.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. /**
  67. * @expectedException GeneralException
  68. * @expectedExceptionMessage Unknown database
  69. */
  70. public function testGetConnectionWrongConfig()
  71. {
  72. if (!defined('DEBUG')) {
  73. define('DEBUG', false);
  74. }
  75. $this->conf['database'] = 'nodb';
  76. $driver = new MySQLiDriver($this->conf);
  77. $this->assertNull('mysqli', $driver->getConnection());
  78. }
  79. public function testDisconnect()
  80. {
  81. if (!defined('DEBUG')) {
  82. define('DEBUG', false);
  83. }
  84. $driver = new MySQLiDriver($this->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. $driver = new MySQLiDriver($this->conf);
  98. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  99. $this->assertEquals(3, $driver->getInsertId());
  100. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  101. $this->assertEquals(4, $driver->getInsertId());
  102. $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  103. $this->assertEquals(5, $driver->getInsertId());
  104. $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)));
  105. $this->assertEquals(8, $driver->getInsertId());
  106. }
  107. /**
  108. * @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used
  109. */
  110. public function testGetInsertId()
  111. {
  112. if (!defined('DEBUG')) {
  113. define('DEBUG', false);
  114. }
  115. $driver = new MySQLiDriver($this->conf);
  116. $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  117. $this->assertEquals(3, $driver->getInsertId());
  118. }
  119. public function testTransaction()
  120. {
  121. if (!defined('DEBUG')) {
  122. define('DEBUG', false);
  123. }
  124. $driver = new MySQLiDriver($this->conf);
  125. $queryTable = $this->getConnection()->createQueryTable(
  126. 'table', 'SELECT * FROM `table`'
  127. );
  128. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  129. ->getTable("table");
  130. $this->assertTablesEqual($expectedTable, $queryTable);
  131. $driver->getConnection();
  132. $driver->beginTransaction();
  133. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  134. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  135. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  136. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  137. $queryTable = $this->getConnection()->createQueryTable(
  138. 'table', 'SELECT * FROM `table`'
  139. );
  140. $driver->commit();
  141. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testDataAfterCommit.xml')
  142. ->getTable("table");
  143. $this->assertTablesEqual($expectedTable, $queryTable);
  144. }
  145. public function testRollback()
  146. {
  147. if (!defined('DEBUG')) {
  148. define('DEBUG', false);
  149. }
  150. $driver = new MySQLiDriver($this->conf);
  151. $queryTable = $this->getConnection()->createQueryTable(
  152. 'table', 'SELECT * FROM `table`'
  153. );
  154. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  155. ->getTable("table");
  156. $this->assertTablesEqual($expectedTable, $queryTable);
  157. $driver->getConnection();
  158. $driver->beginTransaction();
  159. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  160. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  161. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  162. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  163. $driver->rollback();
  164. $queryTable = $this->getConnection()->createQueryTable(
  165. 'table', 'SELECT * FROM `table`'
  166. );
  167. $this->assertTablesEqual($expectedTable, $queryTable);
  168. }
  169. }