MysqlTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use \Codeception\Lib\Driver\Db;
  3. /**
  4. * @group appveyor
  5. */
  6. class MysqlTest extends \PHPUnit_Framework_TestCase
  7. {
  8. protected static $config = [
  9. 'dsn' => 'mysql:host=localhost;dbname=codeception_test',
  10. 'user' => 'root',
  11. 'password' => ''
  12. ];
  13. protected static $sql;
  14. protected $mysql;
  15. public static function setUpBeforeClass()
  16. {
  17. if (getenv('APPVEYOR')) {
  18. self::$config['password'] = 'Password12!';
  19. }
  20. $sql = file_get_contents(\Codeception\Configuration::dataDir() . '/dumps/mysql.sql');
  21. $sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $sql);
  22. self::$sql = explode("\n", $sql);
  23. try {
  24. $mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  25. $mysql->cleanup();
  26. } catch (\Exception $e) {
  27. }
  28. }
  29. public function setUp()
  30. {
  31. try {
  32. $this->mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  33. } catch (\Exception $e) {
  34. $this->markTestSkipped('Couldn\'t establish connection to database');
  35. }
  36. $this->mysql->load(self::$sql);
  37. }
  38. public function tearDown()
  39. {
  40. if (isset($this->mysql)) {
  41. $this->mysql->cleanup();
  42. }
  43. }
  44. public function testCleanupDatabase()
  45. {
  46. $this->assertNotEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
  47. $this->mysql->cleanup();
  48. $this->assertEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
  49. }
  50. /**
  51. * @group appveyor
  52. */
  53. public function testLoadDump()
  54. {
  55. $res = $this->mysql->getDbh()->query("select * from users where name = 'davert'");
  56. $this->assertNotEquals(false, $res);
  57. $this->assertGreaterThan(0, $res->rowCount());
  58. $res = $this->mysql->getDbh()->query("select * from groups where name = 'coders'");
  59. $this->assertNotEquals(false, $res);
  60. $this->assertGreaterThan(0, $res->rowCount());
  61. }
  62. public function testGetSingleColumnPrimaryKey()
  63. {
  64. $this->assertEquals(['id'], $this->mysql->getPrimaryKey('order'));
  65. }
  66. public function testGetCompositePrimaryKey()
  67. {
  68. $this->assertEquals(['group_id', 'id'], $this->mysql->getPrimaryKey('composite_pk'));
  69. }
  70. public function testGetEmptyArrayIfTableHasNoPrimaryKey()
  71. {
  72. $this->assertEquals([], $this->mysql->getPrimaryKey('no_pk'));
  73. }
  74. public function testGetPrimaryColumnOfTableUsingReservedWordAsTableName()
  75. {
  76. $this->assertEquals('id', $this->mysql->getPrimaryColumn('order'));
  77. }
  78. public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
  79. {
  80. $this->setExpectedException(
  81. '\Exception',
  82. 'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
  83. );
  84. $this->mysql->getPrimaryColumn('composite_pk');
  85. }
  86. public function testDeleteFromTableUsingReservedWordAsTableName()
  87. {
  88. $this->mysql->deleteQuery('order', 1);
  89. $res = $this->mysql->getDbh()->query("select id from `order` where id = 1");
  90. $this->assertEquals(0, $res->rowCount());
  91. }
  92. public function testDeleteFromTableUsingReservedWordAsPrimaryKey()
  93. {
  94. $this->mysql->deleteQuery('table_with_reserved_primary_key', 1, 'unique');
  95. $res = $this->mysql->getDbh()->query("select name from `table_with_reserved_primary_key` where `unique` = 1");
  96. $this->assertEquals(0, $res->rowCount());
  97. }
  98. public function testSelectWithBooleanParam()
  99. {
  100. $res = $this->mysql->executeQuery("select `id` from `users` where `is_active` = ?", [false]);
  101. $this->assertEquals(1, $res->rowCount());
  102. }
  103. public function testInsertIntoBitField()
  104. {
  105. $res = $this->mysql->executeQuery(
  106. "insert into `users`(`id`,`name`,`email`,`is_active`,`created_at`) values (?,?,?,?,?)",
  107. [5,'insert.test','insert.test@mail.ua',false,'2012-02-01 21:17:47']
  108. );
  109. $this->assertEquals(1, $res->rowCount());
  110. }
  111. }