SchemaLoaderTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @since 4.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite\Fixture;
  16. use Cake\Database\Connection;
  17. use Cake\Database\Driver\Sqlite;
  18. use Cake\Database\Schema\TableSchema;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\ConnectionHelper;
  21. use Cake\TestSuite\Fixture\SchemaLoader;
  22. use Cake\TestSuite\TestCase;
  23. use InvalidArgumentException;
  24. class SchemaLoaderTest extends TestCase
  25. {
  26. /**
  27. * @var bool|null
  28. */
  29. protected $restore;
  30. /**
  31. * @var \Cake\TestSuite\Fixture\SchemaLoader
  32. */
  33. protected $loader;
  34. protected $truncateDbFile = TMP . 'schema_loader_test.sqlite';
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
  39. $this->restore = $GLOBALS['__PHPUNIT_BOOTSTRAP'];
  40. unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
  41. }
  42. $this->loader = new SchemaLoader();
  43. }
  44. public function tearDown(): void
  45. {
  46. parent::tearDown();
  47. if ($this->restore !== null) {
  48. $GLOBALS['__PHPUNIT_BOOTSTRAP'] = $this->restore;
  49. }
  50. ConnectionHelper::dropTables('test', ['schema_loader_test_one', 'schema_loader_test_two']);
  51. ConnectionManager::drop('test_schema_loader');
  52. if (file_exists($this->truncateDbFile)) {
  53. unlink($this->truncateDbFile);
  54. }
  55. }
  56. /**
  57. * Tests loading schema files.
  58. */
  59. public function testLoadSqlFiles(): void
  60. {
  61. $connection = ConnectionManager::get('test');
  62. $schemaFiles[] = $this->createSchemaFile('schema_loader_test_one');
  63. $schemaFiles[] = $this->createSchemaFile('schema_loader_test_two');
  64. $this->loader->loadSqlFiles($schemaFiles, 'test', false, false);
  65. $connection = ConnectionManager::get('test');
  66. $tables = $connection->getSchemaCollection()->listTables();
  67. $this->assertContains('schema_loader_test_one', $tables);
  68. $this->assertContains('schema_loader_test_two', $tables);
  69. }
  70. /**
  71. * Tests loading missing files.
  72. */
  73. public function testLoadMissingFile(): void
  74. {
  75. $this->expectException(InvalidArgumentException::class);
  76. $this->loader->loadSqlFiles('missing_schema_file.sql', 'test', false, false);
  77. }
  78. /**
  79. * Tests dropping and truncating tables during schema load.
  80. */
  81. public function testDropTruncateTables(): void
  82. {
  83. $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  84. ConnectionManager::setConfig('test_schema_loader', [
  85. 'className' => Connection::class,
  86. 'driver' => Sqlite::class,
  87. 'database' => $this->truncateDbFile,
  88. ]);
  89. $schemaFile = $this->createSchemaFile('schema_loader_first');
  90. $this->loader->loadSqlFiles($schemaFile, 'test_schema_loader', true, true);
  91. $connection = ConnectionManager::get('test_schema_loader');
  92. $result = $connection->getSchemaCollection()->listTables();
  93. $this->assertEquals(['schema_loader_first'], $result);
  94. $schemaFile = $this->createSchemaFile('schema_loader_second');
  95. $this->loader->loadSqlFiles($schemaFile, 'test_schema_loader', true, true);
  96. $result = $connection->getSchemaCollection()->listTables();
  97. $this->assertEquals(['schema_loader_second'], $result);
  98. $statement = $connection->execute('SELECT * FROM schema_loader_second');
  99. $result = $statement->fetchAll();
  100. $this->assertCount(0, $result, 'Table should be empty.');
  101. }
  102. public function testLoadInternalFiles(): void
  103. {
  104. $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  105. ConnectionManager::setConfig('test_schema_loader', [
  106. 'className' => Connection::class,
  107. 'driver' => Sqlite::class,
  108. 'database' => $this->truncateDbFile,
  109. ]);
  110. $this->loader->loadInternalFile(__DIR__ . '/test_schema.php', 'test_schema_loader');
  111. $connection = ConnectionManager::get('test_schema_loader');
  112. $tables = $connection->getSchemaCollection()->listTables();
  113. $this->assertContains('schema_generator', $tables);
  114. $this->assertContains('schema_generator_comment', $tables);
  115. }
  116. protected function createSchemaFile(string $tableName): string
  117. {
  118. $connection = ConnectionManager::get('test');
  119. $schema = new TableSchema($tableName);
  120. $schema
  121. ->addColumn('id', 'integer')
  122. ->addColumn('name', 'string');
  123. $query = $schema->createSql($connection)[0] . ';';
  124. $query .= "\nINSERT INTO {$tableName} (id, name) VALUES (1, 'testing');";
  125. $tmpFile = tempnam(sys_get_temp_dir(), 'SchemaLoaderTest');
  126. file_put_contents($tmpFile, $query);
  127. return $tmpFile;
  128. }
  129. }