SqliteTest.php 4.1 KB

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