diff --git a/src/Concerns/HasKey.php b/src/Concerns/HasKey.php index 19187ab..5a782d2 100644 --- a/src/Concerns/HasKey.php +++ b/src/Concerns/HasKey.php @@ -8,7 +8,7 @@ trait HasKey { - protected function resolveKey(BackedEnum|int|string $key): int|string + protected function resolveKey(BackedEnum | int | string $key): int | string { if ($key instanceof BackedEnum) { return $key->value ?? $key->name; diff --git a/src/Concerns/HasValues.php b/src/Concerns/HasValues.php index 79e0fe8..efab230 100644 --- a/src/Concerns/HasValues.php +++ b/src/Concerns/HasValues.php @@ -10,20 +10,26 @@ trait HasValues { use HasKey; - public function get(BackedEnum|int|string $key): mixed + public function get(BackedEnum | int | string $key): mixed { $key = $this->resolveKey($key); - $main = $this->key . '.' . $key; + $main = $this->key . '.' . $key; $default = $this->default ? $this->default . '.' . $key : null; + if ($this->default) { + return $this->value($main, $default) ?? $this->value($this->default); + } + return $this->value($main, $default); } protected function value(string $key, ?string $default = null): mixed { - return $default - ? config()->get($key, config()->get($default)) - : config()->get($key); + if ($default) { + return config()->get($key) ?: config()->get($default); + } + + return config()->get($key); } } diff --git a/tests/Unit/Facades/Config/HiddenWithoutEnvTest.php b/tests/Unit/Facades/Config/HiddenWithoutEnvTest.php new file mode 100644 index 0000000..f24e18f --- /dev/null +++ b/tests/Unit/Facades/Config/HiddenWithoutEnvTest.php @@ -0,0 +1,43 @@ + ['foo', 'bar']]); + + expect(Config::hidden()->plugins->all()) + ->toBeArray() + ->toBe(['foo', 'bar']) + ->toBe(config('localization-private.plugins')); + + Config::hidden()->plugins->push('baz'); + + expect(Config::hidden()->plugins->all()) + ->toBeArray() + ->toBe(['foo', 'bar', 'baz']) + ->toBe(config('localization-private.plugins')); +}); + +test('packages', function () { + config(['localization-private.packages' => ['foo', 'bar']]); + + expect(Config::hidden()->packages->all()) + ->toBeArray() + ->toBe(['foo', 'bar']) + ->toBe(config('localization-private.packages')); + + Config::hidden()->packages->push('baz'); + + expect(Config::hidden()->packages->all()) + ->toBeArray() + ->toBe(['foo', 'bar', 'baz']) + ->toBe(config('localization-private.packages')); +}); + +test('map', function () { + expect(Config::hidden()->map->all()) + ->toBeArray() + ->toBe(config('localization-private.map')); +}); diff --git a/tests/Unit/Facades/Config/SharedWithEnvTest.php b/tests/Unit/Facades/Config/SharedWithEnvTest.php index 274e9a1..6402c54 100644 --- a/tests/Unit/Facades/Config/SharedWithEnvTest.php +++ b/tests/Unit/Facades/Config/SharedWithEnvTest.php @@ -26,7 +26,7 @@ }); test('aliases', function () { - expect(Config::shared()->aliases) + expect(Config::shared()->aliases->all()) ->toBeArray() ->toBe(config('localization.aliases')); }); diff --git a/tests/Unit/Facades/Config/SharedWithoutEnvTest.php b/tests/Unit/Facades/Config/SharedWithoutEnvTest.php new file mode 100644 index 0000000..6402c54 --- /dev/null +++ b/tests/Unit/Facades/Config/SharedWithoutEnvTest.php @@ -0,0 +1,63 @@ +inline) + ->toBeBool() + ->toBeTrue() + ->toBe(config('localization.inline')); +}); + +test('align', function () { + expect(Config::shared()->align) + ->toBeBool() + ->toBeFalse() + ->toBe(config('localization.align')); +}); + +test('aliases', function () { + expect(Config::shared()->aliases->all()) + ->toBeArray() + ->toBe(config('localization.aliases')); +}); + +test('smart punctuation: enabled', function () { + expect(Config::shared()->punctuation->enabled) + ->toBeBool() + ->toBeTrue() + ->toBe(config('localization.smart_punctuation.enabled')); +}); + +test('smart punctuation: common', function () { + expect(Config::shared()->punctuation->common) + ->toBeArray() + ->toBe(config('localization.smart_punctuation.common')); +}); + +test('smart punctuation: locales', function () { + expect(Config::shared()->punctuation->locales->all()) + ->toBeArray() + ->toBe(config('localization.smart_punctuation.locales')); +}); + +test('smart punctuation: get locale', function () { + expect(Config::shared()->punctuation->locales->get(Locale::French)) + ->toBeArray() + ->toBe(config('localization.smart_punctuation.locales.' . Locale::French->value)); +}); + +test('smart punctuation: get default locale', function () { + expect(Config::shared()->punctuation->locales->get(Locale::Zulu)) + ->toBeArray() + ->toBe(config('localization.smart_punctuation.common')); +});