PostgresTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. use \Codeception\Lib\Driver\Db;
  3. use \Codeception\Test\Unit;
  4. /**
  5. * @group appveyor
  6. * @group db
  7. */
  8. class PostgresTest extends Unit
  9. {
  10. protected static $config = [
  11. 'dsn' => 'pgsql:host=localhost;dbname=codeception_test',
  12. 'user' => 'postgres',
  13. 'password' => null,
  14. ];
  15. protected static $sql;
  16. protected $postgres;
  17. public static function setUpBeforeClass()
  18. {
  19. if (!function_exists('pg_connect')) {
  20. return;
  21. }
  22. if (getenv('APPVEYOR')) {
  23. self::$config['password'] = 'Password12!';
  24. }
  25. $dumpFile = 'dumps/postgres.sql';
  26. if (defined('HHVM_VERSION')) {
  27. $dumpFile = 'dumps/postgres-hhvm.sql';
  28. }
  29. $sql = file_get_contents(codecept_data_dir($dumpFile));
  30. $sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', '', $sql);
  31. self::$sql = explode("\n", $sql);
  32. }
  33. public function setUp()
  34. {
  35. try {
  36. $this->postgres = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  37. $this->postgres->cleanup();
  38. } catch (\Exception $e) {
  39. $this->markTestSkipped('Coudn\'t establish connection to database: ' . $e->getMessage());
  40. }
  41. $this->postgres->load(self::$sql);
  42. }
  43. public function tearDown()
  44. {
  45. if (isset($this->postgres)) {
  46. $this->postgres->cleanup();
  47. }
  48. }
  49. public function testCleanupDatabase()
  50. {
  51. $this->assertNotEmpty(
  52. $this->postgres->getDbh()->query("SELECT * FROM pg_tables where schemaname = 'public'")->fetchAll()
  53. );
  54. $this->postgres->cleanup();
  55. $this->assertEmpty(
  56. $this->postgres->getDbh()->query("SELECT * FROM pg_tables where schemaname = 'public'")->fetchAll()
  57. );
  58. }
  59. public function testCleanupDatabaseDeletesTypes()
  60. {
  61. $customTypes = ['composite_type', 'enum_type', 'range_type', 'base_type'];
  62. foreach ($customTypes as $customType) {
  63. $this->assertNotEmpty(
  64. $this->postgres->getDbh()
  65. ->query("SELECT 1 FROM pg_type WHERE typname = '" . $customType . "';")
  66. ->fetchAll()
  67. );
  68. }
  69. $this->postgres->cleanup();
  70. foreach ($customTypes as $customType) {
  71. $this->assertEmpty(
  72. $this->postgres->getDbh()
  73. ->query("SELECT 1 FROM pg_type WHERE typname = '" . $customType . "';")
  74. ->fetchAll()
  75. );
  76. }
  77. }
  78. public function testLoadDump()
  79. {
  80. $res = $this->postgres->getDbh()->query("select * from users where name = 'davert'");
  81. $this->assertNotEquals(false, $res);
  82. $this->assertGreaterThan(0, $res->rowCount());
  83. $res = $this->postgres->getDbh()->query("select * from groups where name = 'coders'");
  84. $this->assertNotEquals(false, $res);
  85. $this->assertGreaterThan(0, $res->rowCount());
  86. $res = $this->postgres->getDbh()->query("select * from users where email = 'user2@example.org'");
  87. $this->assertNotEquals(false, $res);
  88. $this->assertGreaterThan(0, $res->rowCount());
  89. $res = $this->postgres->getDbh()
  90. ->query("select * from anotherschema.users where email = 'schemauser@example.org'");
  91. $this->assertEquals(1, $res->rowCount());
  92. }
  93. public function testSelectWithEmptyCriteria()
  94. {
  95. $emptyCriteria = [];
  96. $generatedSql = $this->postgres->select('test_column', 'test_table', $emptyCriteria);
  97. $this->assertNotContains('where', $generatedSql);
  98. }
  99. public function testGetSingleColumnPrimaryKey()
  100. {
  101. $this->assertEquals(['id'], $this->postgres->getPrimaryKey('order'));
  102. }
  103. public function testGetCompositePrimaryKey()
  104. {
  105. $this->assertEquals(['group_id', 'id'], $this->postgres->getPrimaryKey('composite_pk'));
  106. }
  107. public function testGetEmptyArrayIfTableHasNoPrimaryKey()
  108. {
  109. $this->assertEquals([], $this->postgres->getPrimaryKey('no_pk'));
  110. }
  111. public function testLastInsertIdReturnsSequenceValueWhenNonStandardSequenceNameIsUsed()
  112. {
  113. $this->postgres->executeQuery('INSERT INTO seqnames(name) VALUES(?)',['test']);
  114. $this->assertEquals(1, $this->postgres->lastInsertId('seqnames'));
  115. }
  116. public function testGetPrimaryColumnOfTableUsingReservedWordAsTableName()
  117. {
  118. $this->assertEquals('id', $this->postgres->getPrimaryColumn('order'));
  119. }
  120. public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
  121. {
  122. $this->setExpectedException(
  123. '\Exception',
  124. 'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
  125. );
  126. $this->postgres->getPrimaryColumn('composite_pk');
  127. }
  128. /**
  129. * @issue https://github.com/Codeception/Codeception/issues/4059
  130. */
  131. public function testLoadDumpEndingWithoutDelimiter()
  132. {
  133. $newDriver = new \Codeception\Lib\Driver\PostgreSql(self::$config['dsn'], self::$config['user'], self::$config['password']);
  134. $newDriver->load(['INSERT INTO empty_table VALUES(1, \'test\')']);
  135. $res = $newDriver->getDbh()->query("select * from empty_table where field = 'test'");
  136. $this->assertNotEquals(false, $res);
  137. $this->assertNotEmpty($res->fetchAll());
  138. }
  139. }