From 7b3f233d7e40b1467e3c1c1e44e462c3acf398f5 Mon Sep 17 00:00:00 2001 From: Konrad Oboza Date: Mon, 17 Jul 2023 14:44:29 +0200 Subject: [PATCH] IBX-4853: Introduced `Locale::convertToRepository` method (#245) For more details see https://issues.ibexa.co/browse/IBX-4853 and https://github.com/ibexa/core/pull/245 * Introduced `Ibexa\Core\MVC\Symfony\Locale::convertToRepository` method * Deprecated `Ibexa\Core\MVC\Symfony\Locale::convertToEz` method --- .../MVC/Symfony/Locale/LocaleConverter.php | 11 ++++ .../Locale/LocaleConverterInterface.php | 9 +++ .../Symfony/Locale/LocaleConverterTest.php | 62 +++++++++++++------ 3 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/lib/MVC/Symfony/Locale/LocaleConverter.php b/src/lib/MVC/Symfony/Locale/LocaleConverter.php index c3194d18e5..5f99846a5a 100644 --- a/src/lib/MVC/Symfony/Locale/LocaleConverter.php +++ b/src/lib/MVC/Symfony/Locale/LocaleConverter.php @@ -72,6 +72,17 @@ public function convertToEz($posixLocale) return $this->reverseConversionMap[$posixLocale]; } + + public function convertToRepository(string $posixLocale): ?string + { + if (!isset($this->reverseConversionMap[$posixLocale])) { + $this->logger->warning("Could not convert locale '$posixLocale' to Repository format. Please check your locale configuration in ibexa.yaml"); + + return null; + } + + return $this->reverseConversionMap[$posixLocale]; + } } class_alias(LocaleConverter::class, 'eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverter'); diff --git a/src/lib/MVC/Symfony/Locale/LocaleConverterInterface.php b/src/lib/MVC/Symfony/Locale/LocaleConverterInterface.php index 9df20e56f5..41e66d79d0 100644 --- a/src/lib/MVC/Symfony/Locale/LocaleConverterInterface.php +++ b/src/lib/MVC/Symfony/Locale/LocaleConverterInterface.php @@ -19,6 +19,9 @@ interface LocaleConverterInterface * Converts a locale in Ibexa internal format to POSIX format. * Returns null if conversion cannot be made. * + * @deprecated use convertToRepository instead + * @see convertToRepository + * * @param string $ezpLocale * * @return string|null @@ -34,6 +37,12 @@ public function convertToPOSIX($ezpLocale); * @return string|null */ public function convertToEz($posixLocale); + + /** + * Converts a locale in POSIX format to Repository internal format. + * Returns null if conversion cannot be made. + */ + public function convertToRepository(string $posixLocale): ?string; } class_alias(LocaleConverterInterface::class, 'eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface'); diff --git a/tests/lib/MVC/Symfony/Locale/LocaleConverterTest.php b/tests/lib/MVC/Symfony/Locale/LocaleConverterTest.php index ad876f0761..826db2d144 100644 --- a/tests/lib/MVC/Symfony/Locale/LocaleConverterTest.php +++ b/tests/lib/MVC/Symfony/Locale/LocaleConverterTest.php @@ -13,19 +13,27 @@ /** * @covers \Ibexa\Core\MVC\Symfony\Locale\LocaleConverter */ -class LocaleConverterTest extends TestCase +final class LocaleConverterTest extends TestCase { - /** @var \Ibexa\Core\MVC\Symfony\Locale\LocaleConverter */ - private $localeConverter; + private LocaleConverter $localeConverter; - /** @var \PHPUnit\Framework\MockObject\MockObject */ - private $logger; + /** @var \Psr\Log\LoggerInterface&\PHPUnit\Framework\MockObject\MockObject */ + private LoggerInterface $logger; - private $conversionMap; + /** + * @var array{ + * array{ + * string, + * string|null, + * } + * } + */ + private array $conversionMap; protected function setUp(): void { parent::setUp(); + $this->conversionMap = [ 'eng-GB' => 'en_GB', 'eng-US' => 'en_US', @@ -41,11 +49,8 @@ protected function setUp(): void /** * @dataProvider convertToPOSIXProvider - * - * @param $ezpLocale - * @param $expected */ - public function testConvertToPOSIX($ezpLocale, $expected) + public function testConvertToPOSIX(string $repositoryLocale, ?string $expected): void { if ($expected === null) { $this->logger @@ -53,10 +58,10 @@ public function testConvertToPOSIX($ezpLocale, $expected) ->method('warning'); } - $this->assertSame($expected, $this->localeConverter->convertToPOSIX($ezpLocale)); + self::assertSame($expected, $this->localeConverter->convertToPOSIX($repositoryLocale)); } - public function convertToPOSIXProvider() + public function convertToPOSIXProvider(): array { return [ ['eng-GB', 'en_GB'], @@ -69,12 +74,23 @@ public function convertToPOSIXProvider() } /** - * @dataProvider convertToEzProvider - * - * @param $posixLocale - * @param $expected + * @dataProvider convertToRepositoryProvider + */ + public function testConvertToEz(string $posixLocale, ?string $expected): void + { + if ($expected === null) { + $this->logger + ->expects($this->once()) + ->method('warning'); + } + + self::assertSame($expected, $this->localeConverter->convertToEz($posixLocale)); + } + + /** + * @dataProvider convertToRepositoryProvider */ - public function testConvertToEz($posixLocale, $expected) + public function testConvertToRepository(string $posixLocale, ?string $expected): void { if ($expected === null) { $this->logger @@ -82,10 +98,18 @@ public function testConvertToEz($posixLocale, $expected) ->method('warning'); } - $this->assertSame($expected, $this->localeConverter->convertToEz($posixLocale)); + self::assertSame($expected, $this->localeConverter->convertToRepository($posixLocale)); } - public function convertToEzProvider() + /** + * @return array{ + * array{ + * string, + * string|null, + * } + * } + */ + public function convertToRepositoryProvider(): array { return [ ['en_GB', 'eng-GB'],