SqliteTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. use \Codeception\Lib\Driver\Db;
  3. class SqliteTest extends \PHPUnit_Framework_TestCase
  4. {
  5. protected static $config = array(
  6. 'dsn' => 'sqlite:tests/data/sqlite.db',
  7. 'user' => 'root',
  8. 'password' => ''
  9. );
  10. protected static $sqlite;
  11. protected static $sql;
  12. public static function setUpBeforeClass()
  13. {
  14. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  15. $dumpFile = '/dumps/sqlite-54.sql';
  16. } else {
  17. $dumpFile = '/dumps/sqlite.sql';
  18. }
  19. $sql = file_get_contents(\Codeception\Configuration::dataDir() . $dumpFile);
  20. $sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $sql);
  21. self::$sql = explode("\n", $sql);
  22. try {
  23. self::$sqlite = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
  24. self::$sqlite->cleanup();
  25. } catch (\Exception $e) {
  26. }
  27. }
  28. public function setUp()
  29. {
  30. if (!isset(self::$sqlite)) {
  31. $this->markTestSkipped('Coudn\'t establish connection to database');
  32. }
  33. self::$sqlite->load(self::$sql);
  34. }
  35. public function tearDown()
  36. {
  37. if (isset(self::$sqlite)) {
  38. self::$sqlite->cleanup();
  39. }
  40. }
  41. public function testCleanupDatabase()
  42. {
  43. $this->assertGreaterThan(
  44. 0,
  45. count(self::$sqlite->getDbh()->query('SELECT name FROM sqlite_master WHERE type = "table";')->fetchAll())
  46. );
  47. self::$sqlite->cleanup();
  48. $this->assertEmpty(
  49. self::$sqlite->getDbh()->query('SELECT name FROM sqlite_master WHERE type = "table";')->fetchAll()
  50. );
  51. }
  52. public function testLoadDump()
  53. {
  54. $res = self::$sqlite->getDbh()->query("select * from users where name = 'davert'");
  55. $this->assertNotEquals(false, $res);
  56. $this->assertNotEmpty($res->fetchAll());
  57. $res = self::$sqlite->getDbh()->query("select * from groups where name = 'coders'");
  58. $this->assertNotEquals(false, $res);
  59. $this->assertNotEmpty($res->fetchAll());
  60. }
  61. public function testGetPrimaryKeyReturnsRowIdIfTableHasIt()
  62. {
  63. $this->assertEquals(['_ROWID_'], self::$sqlite->getPrimaryKey('groups'));
  64. }
  65. public function testGetPrimaryKeyReturnsRowIdIfTableHasNoPrimaryKey()
  66. {
  67. $this->assertEquals(['_ROWID_'], self::$sqlite->getPrimaryKey('no_pk'));
  68. }
  69. public function testGetSingleColumnPrimaryKeyWhenTableHasNoRowId()
  70. {
  71. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  72. $this->markTestSkipped('Sqlite does not support WITHOUT ROWID on travis');
  73. }
  74. $this->assertEquals(['id'], self::$sqlite->getPrimaryKey('order'));
  75. }
  76. public function testGetCompositePrimaryKeyWhenTableHasNoRowId()
  77. {
  78. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  79. $this->markTestSkipped('Sqlite does not support WITHOUT ROWID on travis');
  80. }
  81. $this->assertEquals(['group_id', 'id'], self::$sqlite->getPrimaryKey('composite_pk'));
  82. }
  83. public function testGetPrimaryColumnOfTableUsingReservedWordAsTableNameWhenTableHasNoRowId()
  84. {
  85. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  86. $this->markTestSkipped('Sqlite does not support WITHOUT ROWID on travis');
  87. }
  88. $this->assertEquals('id', self::$sqlite->getPrimaryColumn('order'));
  89. }
  90. public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
  91. {
  92. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  93. $this->markTestSkipped('Sqlite does not support WITHOUT ROWID on travis');
  94. }
  95. $this->setExpectedException(
  96. '\Exception',
  97. 'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
  98. );
  99. self::$sqlite->getPrimaryColumn('composite_pk');
  100. }
  101. public function testThrowsExceptionIfInMemoryDatabaseIsUsed()
  102. {
  103. $this->setExpectedException(
  104. '\Codeception\Exception\ModuleException',
  105. ':memory: database is not supported'
  106. );
  107. Db::create('sqlite::memory:', '', '');
  108. }
  109. }