MysqlTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. use \Codeception\Lib\Driver\Db;
  3. use \Codeception\Test\Unit;
  4. /**
  5. * @group appveyor
  6. * @group db
  7. */
  8. class MysqlTest extends Unit
  9. {
  10. protected static $config = [
  11. 'dsn' => 'mysql:host=localhost;dbname=codeception_test',
  12. 'user' => 'root',
  13. 'password' => ''
  14. ];
  15. protected static $sql;
  16. /**
  17. * @var \Codeception\Lib\Driver\MySql
  18. */
  19. protected $mysql;
  20. public static function setUpBeforeClass()
  21. {
  22. if (getenv('APPVEYOR')) {
  23. self::$config['password'] = 'Password12!';
  24. }
  25. $sql = file_get_contents(\Codeception\Configuration::dataDir() . '/dumps/mysql.sql');
  26. $sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $sql);
  27. self::$sql = explode("\n", $sql);
  28. try {
  29. $mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  30. $mysql->cleanup();
  31. } catch (\Exception $e) {
  32. }
  33. }
  34. public function setUp()
  35. {
  36. try {
  37. $this->mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  38. } catch (\Exception $e) {
  39. $this->markTestSkipped('Couldn\'t establish connection to database: ' . $e->getMessage());
  40. }
  41. $this->mysql->cleanup();
  42. $this->mysql->load(self::$sql);
  43. }
  44. public function tearDown()
  45. {
  46. if (isset($this->mysql)) {
  47. $this->mysql->cleanup();
  48. }
  49. }
  50. public function testCleanupDatabase()
  51. {
  52. $this->assertNotEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
  53. $this->mysql->cleanup();
  54. $this->assertEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
  55. }
  56. /**
  57. * @group appveyor
  58. */
  59. public function testLoadDump()
  60. {
  61. $res = $this->mysql->getDbh()->query("select * from users where name = 'davert'");
  62. $this->assertNotEquals(false, $res);
  63. $this->assertGreaterThan(0, $res->rowCount());
  64. $res = $this->mysql->getDbh()->query("select * from groups where name = 'coders'");
  65. $this->assertNotEquals(false, $res);
  66. $this->assertGreaterThan(0, $res->rowCount());
  67. }
  68. public function testGetSingleColumnPrimaryKey()
  69. {
  70. $this->assertEquals(['id'], $this->mysql->getPrimaryKey('order'));
  71. }
  72. public function testGetCompositePrimaryKey()
  73. {
  74. $this->assertEquals(['group_id', 'id'], $this->mysql->getPrimaryKey('composite_pk'));
  75. }
  76. public function testGetEmptyArrayIfTableHasNoPrimaryKey()
  77. {
  78. $this->assertEquals([], $this->mysql->getPrimaryKey('no_pk'));
  79. }
  80. public function testGetPrimaryColumnOfTableUsingReservedWordAsTableName()
  81. {
  82. $this->assertEquals('id', $this->mysql->getPrimaryColumn('order'));
  83. }
  84. public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
  85. {
  86. $this->setExpectedException(
  87. '\Exception',
  88. 'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
  89. );
  90. $this->mysql->getPrimaryColumn('composite_pk');
  91. }
  92. public function testDeleteFromTableUsingReservedWordAsTableName()
  93. {
  94. $this->mysql->deleteQuery('order', 1);
  95. $res = $this->mysql->getDbh()->query("select id from `order` where id = 1");
  96. $this->assertEquals(0, $res->rowCount());
  97. }
  98. public function testDeleteFromTableUsingReservedWordAsPrimaryKey()
  99. {
  100. $this->mysql->deleteQuery('table_with_reserved_primary_key', 1, 'unique');
  101. $res = $this->mysql->getDbh()->query("select name from `table_with_reserved_primary_key` where `unique` = 1");
  102. $this->assertEquals(0, $res->rowCount());
  103. }
  104. public function testSelectWithBooleanParam()
  105. {
  106. $res = $this->mysql->executeQuery("select `id` from `users` where `is_active` = ?", [false]);
  107. $this->assertEquals(1, $res->rowCount());
  108. }
  109. public function testInsertIntoBitField()
  110. {
  111. if (getenv('WERCKER_ROOT')) {
  112. $this->markTestSkipped('Disabled on Wercker CI');
  113. }
  114. $res = $this->mysql->executeQuery(
  115. "insert into `users`(`id`,`name`,`email`,`is_active`,`created_at`) values (?,?,?,?,?)",
  116. [5,'insert.test','insert.test@mail.ua',false,'2012-02-01 21:17:47']
  117. );
  118. $this->assertEquals(1, $res->rowCount());
  119. }
  120. /**
  121. * THis will fail if MariaDb is used
  122. */
  123. public function testLoadThrowsExceptionWhenDumpFileContainsSyntaxError()
  124. {
  125. $sql = "INSERT INTO `users` (`name`) VALS('')";
  126. $expectedMessage = 'You have an error in your SQL syntax; ' .
  127. 'check the manual that corresponds to your MySQL server version for the right syntax to use near ' .
  128. "'VALS('')' at line 1\nSQL query being executed: \n" . $sql;
  129. $this->setExpectedException('Codeception\Exception\ModuleException', $expectedMessage);
  130. $this->mysql->load([$sql]);
  131. }
  132. }