Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.3",
"php": ">=8.3 <8.6",
"phpdocumentor/reflection-docblock": "^5.6",
"psr/container": "^2.0"
},
Expand Down
8 changes: 8 additions & 0 deletions src/Container/Enum/Locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Xefi\Faker\Container\Enum;

class Locales
{
public const string DEFAULT = 'default';
}
7 changes: 4 additions & 3 deletions src/Container/Traits/HasExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Random\Randomizer;
use Xefi\Faker\Container\Container;
use Xefi\Faker\Container\Enum\Locales;
use Xefi\Faker\Exceptions\NoExtensionLocaleFound;
use Xefi\Faker\Extensions\Extension;

Expand Down Expand Up @@ -189,10 +190,10 @@ public function callExtensionMethod(string $method, array $parameters = [])
// We assume we here have multiple extensions declined by locale, we will try
// to get the extension with the current locale, defaulting to first element
if (is_array($extension) && isset($extension['locales'])) {
if (!isset($extension['locales'][$this->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);
Expand Down
4 changes: 3 additions & 1 deletion src/Container/Traits/HasLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xefi\Faker\Container\Traits;

use Xefi\Faker\Container\Enum\Locales;

trait HasLocale
{
/**
Expand All @@ -18,7 +20,7 @@ trait HasLocale
*/
public function getLocale(): ?string
{
return $this->locale ?? null;
return $this->locale ?? Locales::DEFAULT;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Extensions/Traits/HasLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Xefi\Faker\Extensions\Traits;

use Xefi\Faker\Container\Enum\Locales;

trait HasLocale
{
/**
Expand All @@ -11,6 +13,6 @@ trait HasLocale
*/
public function getLocale(): ?string
{
return null;
return Locales::DEFAULT;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Extensions/NumbersExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
21 changes: 13 additions & 8 deletions tests/Unit/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -32,7 +32,7 @@ public function testExtensionsCorrectlyRegistered()
public function testCallingDefaultExtension()
{
$this->assertEquals(
null,
'default',
(new \Xefi\Faker\Faker())->returnLocale()
);
}
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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();
Expand Down