diff --git a/src/Shared/Infrastructure/LanguageWrapper.php b/src/Shared/Infrastructure/LanguageWrapper.php index 63a546f..c1f8feb 100644 --- a/src/Shared/Infrastructure/LanguageWrapper.php +++ b/src/Shared/Infrastructure/LanguageWrapper.php @@ -18,15 +18,9 @@ public function __construct( ) { } - public function getBaseLanguage(): int + public function getCurrentLanguageAbbr(): string { - /** @var int|null $langId */ - $langId = $this->language->getBaseLanguage(); - return (int)$langId; - } - - public function getLanguageAbbr(?int $langId = null): string - { - return $this->language->getLanguageAbbr(iLanguage: $langId); + $this->language->getLanguageAbbr(); + return $this->language->getLanguageAbbr(); } } diff --git a/src/Shared/Infrastructure/LanguageWrapperInterface.php b/src/Shared/Infrastructure/LanguageWrapperInterface.php index 551cc58..27ba8c0 100644 --- a/src/Shared/Infrastructure/LanguageWrapperInterface.php +++ b/src/Shared/Infrastructure/LanguageWrapperInterface.php @@ -4,6 +4,5 @@ interface LanguageWrapperInterface { - public function getBaseLanguage(): int; - public function getLanguageAbbr(?int $langId = null): string; + public function getCurrentLanguageAbbr(): string; } diff --git a/src/Shared/Service/LanguageService.php b/src/Shared/Service/LanguageService.php index 89bbd94..aa5512c 100644 --- a/src/Shared/Service/LanguageService.php +++ b/src/Shared/Service/LanguageService.php @@ -14,23 +14,23 @@ class LanguageService implements LanguageServiceInterface { public function __construct( - private LanguageWrapperInterface $languageWrapper + private LanguageWrapperInterface $language ) { } - public function filterByLanguageAbbreviation(array $data): ?string + public function filterByLanguageAbbreviation(array $data, string $defaultLang): string { - $langId = $this->languageWrapper->getBaseLanguage(); - $languageAbbr = $this->languageWrapper->getLanguageAbbr(langId: $langId); + $languageAbbr = $this->language->getCurrentLanguageAbbr(); if (isset($data[$languageAbbr])) { return $data[$languageAbbr]; } - if (isset($data['en'])) { - return $data['en']; + if (isset($data[$defaultLang])) { + return $data[$defaultLang]; } - return null; + $dataReversed = array_reverse($data); + return array_pop($dataReversed); } } diff --git a/src/Shared/Service/LanguageServiceInterface.php b/src/Shared/Service/LanguageServiceInterface.php index b42deb5..57e9ec6 100644 --- a/src/Shared/Service/LanguageServiceInterface.php +++ b/src/Shared/Service/LanguageServiceInterface.php @@ -9,5 +9,5 @@ interface LanguageServiceInterface { - public function filterByLanguageAbbreviation(array $data): ?string; + public function filterByLanguageAbbreviation(array $data, string $defaultLang): string; } diff --git a/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php b/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php index 2bb5a5b..65525a7 100644 --- a/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php +++ b/tests/Unit/Module/DataType/ModuleDataTypeFactoryTest.php @@ -9,7 +9,6 @@ namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Module\DataType; -use OxidEsales\Eshop\Core\Module\Module; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; use OxidEsales\GraphQL\ConfigurationAccess\Module\DataType\ModuleDataTypeFactory; use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\LanguageService; @@ -36,24 +35,29 @@ public function testCreateFromCoreModule() $expectedAuthor = uniqid(); $expectedUrl = uniqid(); $expectedEmail = uniqid(); + $expectedLang = uniqid(); + $expectedIsActivated = (bool)random_int(0, 1); - $moduleConfigMock = $this->createMock(ModuleConfiguration::class); - $moduleConfigMock->method('getId')->willReturn($expectedId); - $moduleConfigMock->method('getVersion')->willReturn($expectedVersion); - $moduleConfigMock->method('getTitle')->willReturn($titlesData); - $moduleConfigMock->method('getDescription')->willReturn($descriptionData); - $moduleConfigMock->method('getThumbnail')->willReturn($expectedThumbnail); - $moduleConfigMock->method('getAuthor')->willReturn($expectedAuthor); - $moduleConfigMock->method('getUrl')->willReturn($expectedUrl); - $moduleConfigMock->method('getEmail')->willReturn($expectedEmail); - $moduleConfigMock->method('isActivated')->willReturn((bool)random_int(0, 1)); + $moduleConfigMock = $this->createConfiguredStub(ModuleConfiguration::class, [ + 'getId' => $expectedId, + 'getVersion' => $expectedVersion, + 'getTitle' => $titlesData, + 'getDescription' => $descriptionData, + 'getThumbnail' => $expectedThumbnail, + 'getAuthor' => $expectedAuthor, + 'getUrl' => $expectedUrl, + 'getEmail' => $expectedEmail, + 'isActivated' => $expectedIsActivated, + 'getLang' => $expectedLang + ]); $languageServiceMock = $this->createMock(LanguageService::class); $languageServiceMock + ->expects($this->exactly(2)) ->method('filterByLanguageAbbreviation') ->willReturnMap([ - [$titlesData, $titlesData['de']], - [$descriptionData, $descriptionData['de']] + [$titlesData, $expectedLang, $titlesData['de']], + [$descriptionData, $expectedLang, $descriptionData['de']] ]); $moduleDataTypeFactory = new ModuleDataTypeFactory($languageServiceMock); @@ -67,6 +71,6 @@ public function testCreateFromCoreModule() $this->assertSame($expectedAuthor, $moduleDataType->getAuthor()); $this->assertSame($expectedUrl, $moduleDataType->getUrl()); $this->assertSame($expectedEmail, $moduleDataType->getEmail()); - $this->assertIsBool($moduleDataType->isActive()); + $this->assertSame($expectedIsActivated, $moduleDataType->isActive()); } } diff --git a/tests/Unit/Shared/Infrastructure/LanguageWrapperTest.php b/tests/Unit/Shared/Infrastructure/LanguageWrapperTest.php index 35bed99..6b4ecbe 100644 --- a/tests/Unit/Shared/Infrastructure/LanguageWrapperTest.php +++ b/tests/Unit/Shared/Infrastructure/LanguageWrapperTest.php @@ -18,28 +18,16 @@ */ class LanguageWrapperTest extends TestCase { - public function testGetBaseLanguage(): void + public function testGetCurrentLanguageAbbr(): void { - $expectedLangId = 1; - $languageMock = $this->createPartialMock(Language::class, ['getBaseLanguage']); - $languageMock->method('getBaseLanguage')->willReturn($expectedLangId); + $langAbbr = uniqid(); + $languageMock = $this->createConfiguredStub(Language::class, [ + 'getLanguageAbbr' => $langAbbr, + ]); $languageWrapper = new LanguageWrapper($languageMock); - $actualLangId = $languageWrapper->getBaseLanguage(); + $actualLangAbbr = $languageWrapper->getCurrentLanguageAbbr(); - $this->assertSame($expectedLangId, $actualLangId); - } - - public function testGetLanguageAbbr(): void - { - $langId = 1; - $expectedLangAbbr = uniqid(); - $languageMock = $this->createPartialMock(Language::class, ['getLanguageAbbr']); - $languageMock->method('getLanguageAbbr')->willReturn($expectedLangAbbr); - - $languageWrapper = new LanguageWrapper($languageMock); - $actualLangAbbr = $languageWrapper->getLanguageAbbr($langId); - - $this->assertSame($expectedLangAbbr, $actualLangAbbr); + $this->assertSame($langAbbr, $actualLangAbbr); } } diff --git a/tests/Unit/Shared/Service/LanguageServiceTest.php b/tests/Unit/Shared/Service/LanguageServiceTest.php index 2ba8103..e346a6a 100644 --- a/tests/Unit/Shared/Service/LanguageServiceTest.php +++ b/tests/Unit/Shared/Service/LanguageServiceTest.php @@ -20,51 +20,53 @@ class LanguageServiceTest extends TestCase { public function testFilterByLanguageAbbreviationCorrectLangValue() { - $expectedResult = "Title in de language"; $titlesData = [ 'de' => 'Title in de language', 'en' => 'Title in en language', ]; + $expectedExistingLanguageAbbreviation = 'de'; $languageWrapperMock = $this->createMock(LanguageWrapperInterface::class); - $languageWrapperMock->method('getBaseLanguage')->willReturn(1); - $languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de'); + $languageWrapperMock->method('getCurrentLanguageAbbr') + ->willReturn($expectedExistingLanguageAbbreviation); $languageService = new LanguageService($languageWrapperMock); - $actualResult = $languageService->filterByLanguageAbbreviation($titlesData); + $actualResult = $languageService->filterByLanguageAbbreviation($titlesData, 'en'); - $this->assertSame($actualResult, $expectedResult); + $this->assertSame($titlesData[$expectedExistingLanguageAbbreviation], $actualResult); } public function testFilterByLanguageAbbreviationDefaultedToEnglish() { - $expectedResult = "Title in en language"; $titlesData = [ 'de' => 'Title in de language', 'en' => 'Title in en language', ]; + $defaultLangAbbr = 'en'; $languageWrapperMock = $this->createMock(LanguageWrapperInterface::class); - $languageWrapperMock->method('getBaseLanguage')->willReturn(2); - $languageWrapperMock->method('getLanguageAbbr')->with(2)->willReturn('fr'); + $languageWrapperMock->method('getCurrentLanguageAbbr')->willReturn('fr'); $languageService = new LanguageService($languageWrapperMock); - $actualResult = $languageService->filterByLanguageAbbreviation($titlesData); + $actualResult = $languageService->filterByLanguageAbbreviation($titlesData, $defaultLangAbbr); - $this->assertSame($expectedResult, $actualResult); + $this->assertSame($titlesData[$defaultLangAbbr], $actualResult); } - public function testFilterByLanguageAbbreviationReturnNull(): void + public function testFilterByLanguageAbbreviationDefaultedToFirstInArray() { - $titlesData = []; + $titlesData = [ + 'de' => 'Title in de language', + 'en' => 'Title in en language', + ]; $languageWrapperMock = $this->createMock(LanguageWrapperInterface::class); - $languageWrapperMock->method('getBaseLanguage')->willReturn(1); - $languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de'); + $languageWrapperMock->expects($this->once()) + ->method('getCurrentLanguageAbbr')->willReturn('pl'); $languageService = new LanguageService($languageWrapperMock); - $actualResult = $languageService->filterByLanguageAbbreviation($titlesData); + $actualResult = $languageService->filterByLanguageAbbreviation($titlesData, 'fr'); - $this->assertNull($actualResult); + $this->assertSame($titlesData['de'], $actualResult); } }