PostgresTest.php 4.9 KB

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