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.

222 lines
8.1 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. if (!defined('DEBUG')) {
  49. define('DEBUG', false);
  50. }
  51. $driver = new MySQLiDriver($this->conf);
  52. $queryTable = $this->getConnection()->createQueryTable(
  53. 'table', 'SELECT * FROM `table`'
  54. );
  55. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  56. ->getTable("table");
  57. $this->assertTablesEqual($expectedTable, $queryTable);
  58. $this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  59. $this->assertSame(3, $this->getConnection()->getRowCount('table'));
  60. }
  61. /**
  62. * @group MySQL
  63. */
  64. public function testGetConnection()
  65. {
  66. if (!defined('DEBUG')) {
  67. define('DEBUG', false);
  68. }
  69. $driver = new MySQLiDriver($this->conf);
  70. $this->assertInstanceOf('mysqli', $driver->getConnection());
  71. $this->assertTrue($driver->isConnected());
  72. }
  73. /**
  74. * @group MySQL
  75. */
  76. public function testGetConnectionWrongConfig()
  77. {
  78. if (!defined('DEBUG')) {
  79. define('DEBUG', false);
  80. }
  81. $this->conf['database'] = 'nodb';
  82. $driver = new MySQLiDriver($this->conf);
  83. $this->setExpectedException('GeneralException', 'Unknown database \'nodb\'');
  84. $this->assertNull('mysqli', $driver->getConnection());
  85. }
  86. /**
  87. * @group MySQL
  88. */
  89. public function testDisconnect()
  90. {
  91. if (!defined('DEBUG')) {
  92. define('DEBUG', false);
  93. }
  94. $driver = new MySQLiDriver($this->conf);
  95. $driver->disconnect();
  96. $this->assertAttributeEquals(null, 'connection', $driver);
  97. $this->assertInstanceOf('mysqli', $driver->getConnection());
  98. $this->assertAttributeInstanceOf('mysqli', 'connection', $driver);
  99. $driver->disconnect();
  100. $this->assertAttributeEquals(null, 'connection', $driver);
  101. }
  102. /**
  103. * @group MySQL
  104. */
  105. public function testInsert()
  106. {
  107. if (!defined('DEBUG')) {
  108. define('DEBUG', false);
  109. }
  110. $driver = new MySQLiDriver($this->conf);
  111. $this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  112. $this->assertSame(3, $driver->getInsertId());
  113. $this->assertSame(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  114. $this->assertSame(4, $driver->getInsertId());
  115. $this->assertSame(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  116. $this->assertSame(5, $driver->getInsertId());
  117. $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)));
  118. $this->assertSame(8, $driver->getInsertId());
  119. }
  120. /**
  121. * @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used
  122. * @group MySQL
  123. */
  124. public function testGetInsertId()
  125. {
  126. if (!defined('DEBUG')) {
  127. define('DEBUG', false);
  128. }
  129. $driver = new MySQLiDriver($this->conf);
  130. $this->assertSame(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')));
  131. $this->assertSame(3, $driver->getInsertId());
  132. }
  133. /**
  134. * @group MySQL
  135. */
  136. public function testTransaction()
  137. {
  138. if (!defined('DEBUG')) {
  139. define('DEBUG', false);
  140. }
  141. $driver = new MySQLiDriver($this->conf);
  142. $queryTable = $this->getConnection()->createQueryTable(
  143. 'table', 'SELECT * FROM `table`'
  144. );
  145. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  146. ->getTable("table");
  147. $this->assertTablesEqual($expectedTable, $queryTable);
  148. $driver->getConnection();
  149. $driver->beginTransaction();
  150. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  151. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  152. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  153. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  154. $queryTable = $this->getConnection()->createQueryTable(
  155. 'table', 'SELECT * FROM `table`'
  156. );
  157. $driver->commit();
  158. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testDataAfterCommit.xml')
  159. ->getTable("table");
  160. $this->assertTablesEqual($expectedTable, $queryTable);
  161. }
  162. /**
  163. * @group MySQL
  164. */
  165. public function testRollback()
  166. {
  167. if (!defined('DEBUG')) {
  168. define('DEBUG', false);
  169. }
  170. $driver = new MySQLiDriver($this->conf);
  171. $queryTable = $this->getConnection()->createQueryTable(
  172. 'table', 'SELECT * FROM `table`'
  173. );
  174. $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml')
  175. ->getTable("table");
  176. $this->assertTablesEqual($expectedTable, $queryTable);
  177. $driver->getConnection();
  178. $driver->beginTransaction();
  179. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  180. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  181. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  182. $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'));
  183. $driver->rollback();
  184. $queryTable = $this->getConnection()->createQueryTable(
  185. 'table', 'SELECT * FROM `table`'
  186. );
  187. $this->assertTablesEqual($expectedTable, $queryTable);
  188. }
  189. }