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.

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