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.

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