Skip to content

Commit

Permalink
OXDEV-8215 Decoupled LanguageService from core language class
Browse files Browse the repository at this point in the history
  • Loading branch information
RahatHameed committed Jul 4, 2024
1 parent b615494 commit deab496
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
32 changes: 32 additions & 0 deletions src/Shared/Core/LanguageWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Shared\Core;

use OxidEsales\Eshop\Core\Language;

class LanguageWrapper implements LanguageWrapperInterface
{
public function __construct(
private Language $language
) {
}

public function getBaseLanguage(): int
{
/** @var int|null $langId */
$langId = $this->language->getBaseLanguage();
return (int)$langId;
}

public function getLanguageAbbr(?int $langId = null): string
{
return $this->language->getLanguageAbbr(iLanguage: $langId);
}
}
9 changes: 9 additions & 0 deletions src/Shared/Core/LanguageWrapperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OxidEsales\GraphQL\ConfigurationAccess\Shared\Core;

interface LanguageWrapperInterface
{
public function getBaseLanguage(): int;
public function getLanguageAbbr(?int $langId = null): string;
}
13 changes: 13 additions & 0 deletions src/Shared/Core/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
_defaults:
public: false
autowire: true
bind:
OxidEsales\Eshop\Core\Language: '@=service("OxidEsales\\GraphQL\\ConfigurationAccess\\Shared\\Core\\Registry").getLang()'

OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\Registry:
class: OxidEsales\Eshop\Core\Registry
public: true

OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapperInterface:
class: OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapper
7 changes: 3 additions & 4 deletions src/Shared/Service/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Shared\Service;

use OxidEsales\Eshop\Core\Language;
use OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapperInterface;

class LanguageService implements LanguageServiceInterface
{
public function __construct(
protected Language $language
private LanguageWrapperInterface $language
) {
}

public function filterByLanguageAbbreviation(array $data): ?string
{
/** @var int|null $langId */
$langId = $this->language->getBaseLanguage();
$languageAbbr = $this->language->getLanguageAbbr(iLanguage: $langId);
$languageAbbr = $this->language->getLanguageAbbr(langId: $langId);

return $data[$languageAbbr] ?? null;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Shared/services.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
imports:
- { resource: Core/services.yaml }

services:
_defaults:
public: false
autowire: true
bind:
OxidEsales\Eshop\Core\Language: '@=service("OxidEsales\\ConfigurationAccess\\Core\\Registry").getLang()'

OxidEsales\ConfigurationAccess\Core\Registry:
class: OxidEsales\Eshop\Core\Registry
public: true

OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\:
resource: 'Service/'
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Shared/Core/LanguageWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Shared\Core;

use OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapper;
use PHPUnit\Framework\TestCase;
use OxidEsales\Eshop\Core\Language;

/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapper;
*/
class LanguageWrapperTest extends TestCase
{
public function testGetBaseLanguage(): void
{
$expectedLangId = 1;
$languageMock = $this->createPartialMock(Language::class, ['getBaseLanguage']);
$languageMock->method('getBaseLanguage')->willReturn($expectedLangId);

$languageWrapper = new LanguageWrapper($languageMock);
$actualLangId = $languageWrapper->getBaseLanguage();

$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);
}
}
18 changes: 9 additions & 9 deletions tests/Unit/Shared/Service/LanguageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Shared\Service;

use OxidEsales\Eshop\Core\Language;
use OxidEsales\GraphQL\ConfigurationAccess\Shared\Core\LanguageWrapperInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Shared\Service\LanguageService;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,11 +26,11 @@ public function testFilterByLanguageAbbreviationCorrectLangValue()
'en' => 'Title in en language',
];

$languageMock = $this->createPartialMock(Language::class, ['getBaseLanguage','getLanguageAbbr']);
$languageMock->method('getBaseLanguage')->willReturn(1);
$languageMock->method('getLanguageAbbr')->with(1)->willReturn('de');
$languageWrapperMock = $this->createMock(LanguageWrapperInterface::class);
$languageWrapperMock->method('getBaseLanguage')->willReturn(1);
$languageWrapperMock->method('getLanguageAbbr')->with(1)->willReturn('de');

$languageService = new LanguageService($languageMock);
$languageService = new LanguageService($languageWrapperMock);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData);

$this->assertSame($actualResult, $expectedResult);
Expand All @@ -43,11 +43,11 @@ public function testFilterByLanguageAbbreviationWrongLangValue()
'en' => 'Title in en language',
];

$languageMock = $this->createPartialMock(Language::class, ['getBaseLanguage','getLanguageAbbr']);
$languageMock->method('getBaseLanguage')->willReturn(2);
$languageMock->method('getLanguageAbbr')->with(2)->willReturn('fr');
$languageWrapperMock = $this->createMock(LanguageWrapperInterface::class);
$languageWrapperMock->method('getBaseLanguage')->willReturn(2);
$languageWrapperMock->method('getLanguageAbbr')->with(2)->willReturn('fr');

$languageService = new LanguageService($languageMock);
$languageService = new LanguageService($languageWrapperMock);
$actualResult = $languageService->filterByLanguageAbbreviation($titlesData);

$this->assertNull($actualResult);
Expand Down

0 comments on commit deab496

Please sign in to comment.