diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f7a5a0b..9852101 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: [ '8.3', '8.4' ] + php-version: [ '8.3', '8.4', '8.5' ] name: Tests on PHP ${{ matrix.php-version }} diff --git a/composer.json b/composer.json index e09cc3b..8075b2c 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "^8.3", + "php": ">=8.3 <8.6", "phpdocumentor/reflection-docblock": "^5.6", "psr/container": "^2.0" }, diff --git a/src/Container/Enum/Locales.php b/src/Container/Enum/Locales.php new file mode 100644 index 0000000..7c1926c --- /dev/null +++ b/src/Container/Enum/Locales.php @@ -0,0 +1,8 @@ +getLocale()]) && !isset($extension['locales'][null])) { - throw new NoExtensionLocaleFound(sprintf('Locale \'%s\' and \'null\' for method \'%s\' was not found', $this->getLocale(), $method)); + if (!isset($extension['locales'][$this->getLocale()]) && !isset($extension['locales'][Locales::DEFAULT])) { + throw new NoExtensionLocaleFound(sprintf('Locale \'%s\' and \'%s\' for method \'%s\' was not found', $this->getLocale(), Locales::DEFAULT, $method)); } - $extension = $extension['locales'][$this->getLocale()] ?? $extension['locales'][null]; + $extension = $extension['locales'][$this->getLocale()] ?? $extension['locales'][Locales::DEFAULT]; } return $extension->$method(...$parameters); diff --git a/src/Container/Traits/HasLocale.php b/src/Container/Traits/HasLocale.php index 3fddf6f..6d0d323 100644 --- a/src/Container/Traits/HasLocale.php +++ b/src/Container/Traits/HasLocale.php @@ -2,6 +2,8 @@ namespace Xefi\Faker\Container\Traits; +use Xefi\Faker\Container\Enum\Locales; + trait HasLocale { /** @@ -18,7 +20,7 @@ trait HasLocale */ public function getLocale(): ?string { - return $this->locale ?? null; + return $this->locale ?? Locales::DEFAULT; } /** diff --git a/src/Extensions/Traits/HasLocale.php b/src/Extensions/Traits/HasLocale.php index a5c2349..bcd1146 100644 --- a/src/Extensions/Traits/HasLocale.php +++ b/src/Extensions/Traits/HasLocale.php @@ -2,6 +2,8 @@ namespace Xefi\Faker\Extensions\Traits; +use Xefi\Faker\Container\Enum\Locales; + trait HasLocale { /** @@ -11,6 +13,6 @@ trait HasLocale */ public function getLocale(): ?string { - return null; + return Locales::DEFAULT; } } diff --git a/tests/Unit/Extensions/NumbersExtensionTest.php b/tests/Unit/Extensions/NumbersExtensionTest.php index 0a245a4..8de48e8 100644 --- a/tests/Unit/Extensions/NumbersExtensionTest.php +++ b/tests/Unit/Extensions/NumbersExtensionTest.php @@ -71,7 +71,7 @@ public static function floatProvider() [-0.0001, 0.0001, 6], [1.111, 1.119, 4], [2500.0, 5000.0, 0], - [-0.999, 0.999, 2], + [-0.995, 0.995, 2], [1234.567, 2345.678, 3], [-300.3, -100.1, 1], [0.000001, 0.000009, 7], diff --git a/tests/Unit/LocaleTest.php b/tests/Unit/LocaleTest.php index 36196a4..b256a67 100644 --- a/tests/Unit/LocaleTest.php +++ b/tests/Unit/LocaleTest.php @@ -19,10 +19,10 @@ public function testExtensionsCorrectlyRegistered() $this->assertEquals( [ 'locales' => [ - null => new \Xefi\Faker\Tests\Support\Extensions\NullLocaleExtensionTest(new \Random\Randomizer()), - 'en_EN' => new \Xefi\Faker\Tests\Support\Extensions\EnEnExtensionTest(new \Random\Randomizer()), - 'en_US' => new \Xefi\Faker\Tests\Support\Extensions\EnUsExtensionTest(new \Random\Randomizer()), - 'fr_FR' => new \Xefi\Faker\Tests\Support\Extensions\FrFrExtensionTest(new \Random\Randomizer()), + 'default' => new \Xefi\Faker\Tests\Support\Extensions\NullLocaleExtensionTest(new \Random\Randomizer()), + 'en_EN' => new \Xefi\Faker\Tests\Support\Extensions\EnEnExtensionTest(new \Random\Randomizer()), + 'en_US' => new \Xefi\Faker\Tests\Support\Extensions\EnUsExtensionTest(new \Random\Randomizer()), + 'fr_FR' => new \Xefi\Faker\Tests\Support\Extensions\FrFrExtensionTest(new \Random\Randomizer()), ], ], (new \Xefi\Faker\Container\Container())->getExtensions()['locale-extension-test'] @@ -32,7 +32,7 @@ public function testExtensionsCorrectlyRegistered() public function testCallingDefaultExtension() { $this->assertEquals( - null, + 'default', (new \Xefi\Faker\Faker())->returnLocale() ); } @@ -75,16 +75,21 @@ public function testResettingLocale() ); $this->assertEquals( - null, + 'default', $faker->locale(null)->returnLocale() ); + + $this->assertEquals( + 'default', + $faker->locale('default')->returnLocale() + ); } public function testUsingNotExistingLocaleFallingBackToNullLocale() { $faker = new Xefi\Faker\Faker('not-existing-locale'); $this->assertEquals( - null, + 'default', $faker->returnLocale() ); } @@ -101,7 +106,7 @@ public function testUsingNotExistingLocaleWithoutNullLocale() ]); $this->expectException(\Xefi\Faker\Exceptions\NoExtensionLocaleFound::class); - $this->expectExceptionMessage('Locale \'not-existing-locale\' and \'null\' for method \'returnLocale\' was not found'); + $this->expectExceptionMessage('Locale \'not-existing-locale\' and \'default\' for method \'returnLocale\' was not found'); $faker = new Xefi\Faker\Faker('not-existing-locale'); $faker->returnLocale();