diff --git a/framework/core/src/Foundation/Config.php b/framework/core/src/Foundation/Config.php new file mode 100644 index 000000000..e540bedfa --- /dev/null +++ b/framework/core/src/Foundation/Config.php @@ -0,0 +1,75 @@ +data = $data; + + $this->requireKeys('url'); + } + + public function url(): UriInterface + { + return new Uri(rtrim($this->data['url'], '/')); + } + + public function inDebugMode(): bool + { + return $this->data['debug'] ?? false; + } + + public function inMaintenanceMode(): bool + { + return $this->data['offline'] ?? false; + } + + private function requireKeys(...$keys) + { + foreach ($keys as $key) { + if (! array_key_exists($key, $this->data)) { + throw new InvalidArgumentException( + "Configuration is invalid without a $key key" + ); + } + } + } + + public function offsetGet($offset) + { + return Arr::get($this->data, $offset); + } + + public function offsetExists($offset) + { + return Arr::has($this->data, $offset); + } + + public function offsetSet($offset, $value) + { + throw new RuntimeException('The Config is immutable'); + } + + public function offsetUnset($offset) + { + throw new RuntimeException('The Config is immutable'); + } +} diff --git a/framework/core/tests/unit/Foundation/ConfigTest.php b/framework/core/tests/unit/Foundation/ConfigTest.php new file mode 100644 index 000000000..a80acf2b7 --- /dev/null +++ b/framework/core/tests/unit/Foundation/ConfigTest.php @@ -0,0 +1,155 @@ +expectException(InvalidArgumentException::class); + + new Config([]); + } + + /** @test */ + public function it_wraps_base_url_in_value_object() + { + $config = new Config([ + 'url' => 'https://flarum.local/myforum/', + ]); + + $url = $config->url(); + $this->assertEquals('https', $url->getScheme()); + $this->assertEquals('/myforum', $url->getPath()); // Note that trailing slashes are removed + $this->assertEquals('https://flarum.local/myforum', (string) $url); + } + + /** @test */ + public function it_has_a_helper_for_debug_mode() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'debug' => false, + ]); + + $this->assertFalse($config->inDebugMode()); + + $config = new Config([ + 'url' => 'https://flarum.local', + 'debug' => true, + ]); + + $this->assertTrue($config->inDebugMode()); + } + + /** @test */ + public function it_turns_off_debug_mode_by_default() + { + $config = new Config([ + 'url' => 'https://flarum.local', + ]); + + $this->assertFalse($config->inDebugMode()); + } + + /** @test */ + public function it_has_a_helper_for_maintenance_mode() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'offline' => false, + ]); + + $this->assertFalse($config->inMaintenanceMode()); + + $config = new Config([ + 'url' => 'https://flarum.local', + 'offline' => true, + ]); + + $this->assertTrue($config->inMaintenanceMode()); + } + + /** @test */ + public function it_turns_off_maintenance_mode_by_default() + { + $config = new Config([ + 'url' => 'https://flarum.local', + ]); + + $this->assertFalse($config->inMaintenanceMode()); + } + + /** @test */ + public function it_exposes_additional_keys_via_array_access() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'custom_a' => 'b', + ]); + + $this->assertEquals('b', $config['custom_a']); + } + + /** @test */ + public function it_exposes_nested_keys_via_dot_syntax() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'nested' => [ + 'first' => '1', + 'second' => '2', + ], + ]); + + $this->assertEquals('1', $config['nested.first']); + $this->assertEquals('2', $config['nested.second']); + } + + /** @test */ + public function it_does_not_allow_mutation_via_array_access() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'custom_a' => 'b', + ]); + + try { + $config['custom_a'] = 'c'; + } catch (RuntimeException $_) { + } + + // Ensure the value was not changed + $this->assertEquals('b', $config['custom_a']); + } + + /** @test */ + public function it_does_not_allow_removal_via_array_access() + { + $config = new Config([ + 'url' => 'https://flarum.local', + 'custom_a' => 'b', + ]); + + try { + unset($config['custom_a']); + } catch (RuntimeException $_) { + } + + // Ensure the value was not changed + $this->assertEquals('b', $config['custom_a']); + } +}