|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Core\Config; |
| 4 | + |
| 5 | +use Tempest\Core\AppConfig; |
| 6 | +use Tempest\Core\ConfigCache; |
| 7 | +use Tempest\Core\Environment; |
| 8 | +use Tempest\Core\Kernel\LoadConfig; |
| 9 | +use Tempest\Discovery\DiscoveryLocation; |
| 10 | +use Tempest\Support\Filesystem; |
| 11 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 12 | + |
| 13 | +final class LoadConfigTest extends FrameworkIntegrationTestCase |
| 14 | +{ |
| 15 | + protected function tearDown(): void |
| 16 | + { |
| 17 | + Filesystem\delete_directory(__DIR__ . '/Fixtures'); |
| 18 | + |
| 19 | + parent::tearDown(); |
| 20 | + } |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + Filesystem\ensure_directory_empty(__DIR__ . '/Fixtures'); |
| 27 | + |
| 28 | + $this->container->get(ConfigCache::class)->clear(); |
| 29 | + $this->kernel->discoveryLocations = [ |
| 30 | + new DiscoveryLocation('App', __DIR__ . '/Fixtures'), |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + public function test_config_loaded_in_order(): void |
| 35 | + { |
| 36 | + $this->setupFixtures([ |
| 37 | + 'db.local.config.php', |
| 38 | + 'db.config.php', |
| 39 | + 'db.stg.config.php', |
| 40 | + 'db.prd.config.php', |
| 41 | + 'db.test.config.php', |
| 42 | + 'db.production.config.php', |
| 43 | + ]); |
| 44 | + |
| 45 | + $config = $this->container->get(LoadConfig::class)->find(); |
| 46 | + |
| 47 | + $this->assertStringContainsString('db.config.php', $config[0]); |
| 48 | + $this->assertStringContainsString('db.stg.config.php', $config[1]); |
| 49 | + $this->assertStringContainsString('db.prd.config.php', $config[2]); |
| 50 | + $this->assertStringContainsString('db.production.config.php', $config[3]); |
| 51 | + $this->assertStringContainsString('db.local.config.php', $config[4]); |
| 52 | + $this->assertStringContainsString('db.test.config.php', $config[5]); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_non_production_configs_are_discarded_in_production(): void |
| 56 | + { |
| 57 | + $this->setupFixtures([ |
| 58 | + 'db.local.config.php', |
| 59 | + 'db.dev.config.php', |
| 60 | + 'db.stg.config.php', |
| 61 | + 'db.production.config.php', |
| 62 | + 'db.config.php', |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 66 | + $config = $this->container->get(LoadConfig::class)->find(); |
| 67 | + |
| 68 | + $this->assertCount(2, $config); |
| 69 | + $this->assertStringContainsString('db.config.php', $config[0]); |
| 70 | + $this->assertStringContainsString('db.production.config.php', $config[1]); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_non_staging_configs_are_discarded_in_staging(): void |
| 74 | + { |
| 75 | + $this->setupFixtures([ |
| 76 | + 'db.local.config.php', |
| 77 | + 'db.dev.config.php', |
| 78 | + 'db.stg.config.php', |
| 79 | + 'db.production.config.php', |
| 80 | + 'db.config.php', |
| 81 | + ]); |
| 82 | + |
| 83 | + $this->container->get(AppConfig::class)->environment = Environment::STAGING; |
| 84 | + $config = $this->container->get(LoadConfig::class)->find(); |
| 85 | + |
| 86 | + $this->assertCount(2, $config); |
| 87 | + $this->assertStringContainsString('db.config.php', $config[0]); |
| 88 | + $this->assertStringContainsString('db.stg.config.php', $config[1]); |
| 89 | + } |
| 90 | + |
| 91 | + public function test_non_dev_configs_are_discarded_in_dev(): void |
| 92 | + { |
| 93 | + $this->setupFixtures([ |
| 94 | + 'db.local.config.php', |
| 95 | + 'db.dev.config.php', |
| 96 | + 'db.stg.config.php', |
| 97 | + 'db.production.config.php', |
| 98 | + 'db.config.php', |
| 99 | + ]); |
| 100 | + |
| 101 | + $this->container->get(AppConfig::class)->environment = Environment::LOCAL; |
| 102 | + $config = $this->container->get(LoadConfig::class)->find(); |
| 103 | + |
| 104 | + $this->assertCount(3, $config); |
| 105 | + $this->assertStringContainsString('db.config.php', $config[0]); |
| 106 | + $this->assertStringContainsString('db.dev.config.php', $config[1]); |
| 107 | + $this->assertStringContainsString('db.local.config.php', $config[2]); |
| 108 | + } |
| 109 | + |
| 110 | + private function setupFixtures(array $configs): void |
| 111 | + { |
| 112 | + foreach ($configs as $config) { |
| 113 | + $db = str_replace('.php', '.sqlite', $config); |
| 114 | + |
| 115 | + Filesystem\write_file(__DIR__ . '/Fixtures/' . $config, <<<PHP |
| 116 | + <?php |
| 117 | +
|
| 118 | + use Tempest\Database\Config\SQLiteConfig; |
| 119 | +
|
| 120 | + return new SQLiteConfig( |
| 121 | + path: '{$db}', |
| 122 | + ); |
| 123 | + PHP); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments