SchemaLoaderTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Console\ConsoleIo;
  17. use Cake\Database\Connection;
  18. use Cake\Database\Driver\Sqlite;
  19. use Cake\Database\Schema\TableSchema;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\Fixture\SchemaCleaner;
  22. use Cake\TestSuite\Fixture\SchemaLoader;
  23. use Cake\TestSuite\TestCase;
  24. use InvalidArgumentException;
  25. class SchemaLoaderTest extends TestCase
  26. {
  27. /**
  28. * @var bool|null
  29. */
  30. protected $restore;
  31. /**
  32. * @var \Cake\TestSuite\Fixture\SchemaLoader
  33. */
  34. protected $loader;
  35. protected $truncateDbFile = TMP . 'schema_loader_test.sqlite';
  36. public function setUp(): void
  37. {
  38. parent::setUp();
  39. $this->restore = $GLOBALS['__PHPUNIT_BOOTSTRAP'];
  40. unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
  41. $this->loader = new SchemaLoader(['outputLevel' => ConsoleIo::QUIET]);
  42. }
  43. public function tearDown(): void
  44. {
  45. parent::tearDown();
  46. $GLOBALS['__PHPUNIT_BOOTSTRAP'] = $this->restore;
  47. (new SchemaCleaner())->dropTables('test', ['schema_loader_test_one', 'schema_loader_test_two']);
  48. ConnectionManager::drop('test_schema_loader');
  49. if (file_exists($this->truncateDbFile)) {
  50. unlink($this->truncateDbFile);
  51. }
  52. }
  53. /**
  54. * Tests loading schema files.
  55. */
  56. public function testLoadSqlFiles(): void
  57. {
  58. $connection = ConnectionManager::get('test');
  59. $schemaFiles[] = $this->createSchemaFile('schema_loader_test_one');
  60. $schemaFiles[] = $this->createSchemaFile('schema_loader_test_two');
  61. $this->loader->loadSqlFiles($schemaFiles, 'test', false, false);
  62. $connection = ConnectionManager::get('test');
  63. $tables = $connection->getSchemaCollection()->listTables();
  64. $this->assertContains('schema_loader_test_one', $tables);
  65. $this->assertContains('schema_loader_test_two', $tables);
  66. }
  67. /**
  68. * Tests loading missing files.
  69. */
  70. public function testLoadMissingFile(): void
  71. {
  72. $this->expectException(InvalidArgumentException::class);
  73. $this->loader->loadSqlFiles('missing_schema_file.sql', 'test', false, false);
  74. }
  75. /**
  76. * Tests dropping and truncating tables during schema load.
  77. */
  78. public function testDropTruncateTables(): void
  79. {
  80. $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  81. ConnectionManager::setConfig('test_schema_loader', [
  82. 'className' => Connection::class,
  83. 'driver' => Sqlite::class,
  84. 'database' => $this->truncateDbFile,
  85. ]);
  86. $schemaFile = $this->createSchemaFile('schema_loader_first');
  87. $this->loader->loadSqlFiles($schemaFile, 'test_schema_loader');
  88. $connection = ConnectionManager::get('test_schema_loader');
  89. $result = $connection->getSchemaCollection()->listTables();
  90. $this->assertEquals(['schema_loader_first'], $result);
  91. $schemaFile = $this->createSchemaFile('schema_loader_second');
  92. $this->loader->loadSqlFiles($schemaFile, 'test_schema_loader');
  93. $result = $connection->getSchemaCollection()->listTables();
  94. $this->assertEquals(['schema_loader_second'], $result);
  95. $statement = $connection->query('SELECT * FROM schema_loader_second');
  96. $result = $statement->fetchAll();
  97. $this->assertCount(0, $result, 'Table should be empty.');
  98. }
  99. public function testLoadInternalFiles(): void
  100. {
  101. $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
  102. ConnectionManager::setConfig('test_schema_loader', [
  103. 'className' => Connection::class,
  104. 'driver' => Sqlite::class,
  105. 'database' => $this->truncateDbFile,
  106. ]);
  107. $this->loader->loadInternalFile(__DIR__ . '/test_schema.php', 'test_schema_loader');
  108. $connection = ConnectionManager::get('test_schema_loader');
  109. $tables = $connection->getSchemaCollection()->listTables();
  110. $this->assertContains('schema_generator', $tables);
  111. $this->assertContains('schema_generator_comment', $tables);
  112. }
  113. protected function createSchemaFile(string $tableName): string
  114. {
  115. $connection = ConnectionManager::get('test');
  116. $schema = new TableSchema($tableName);
  117. $schema
  118. ->addColumn('id', 'integer')
  119. ->addColumn('name', 'string');
  120. $query = $schema->createSql($connection)[0] . ';';
  121. $query .= "\nINSERT INTO {$tableName} (id, name) VALUES (1, 'testing');";
  122. $tmpFile = tempnam(sys_get_temp_dir(), 'SchemaLoaderTest');
  123. file_put_contents($tmpFile, $query);
  124. return $tmpFile;
  125. }
  126. }