Skip to content

Commit

Permalink
OXDEV-7557 extract Json service from ThemeSettingService
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 14, 2023
1 parent 5b37c60 commit 4fc105d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 72 deletions.
21 changes: 21 additions & 0 deletions src/Setting/Service/JsonService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Service;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\CollectionEncodingException;

class JsonService implements JsonServiceInterface
{
/**
* @throws CollectionEncodingException
*/
public function jsonEncodeArray(array $collection): string
{
$jsonValue = json_encode($collection);

if ($jsonValue === false) {
throw new CollectionEncodingException();
}
return $jsonValue;
}
}
8 changes: 8 additions & 0 deletions src/Setting/Service/JsonServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Service;

interface JsonServiceInterface
{
public function jsonEncodeArray(array $collection): string;
}
23 changes: 6 additions & 17 deletions src/Setting/Service/ThemeSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Service;

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\CollectionEncodingException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\InvalidCollection;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\IntegerSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\SettingType;
Expand All @@ -22,7 +21,8 @@
final class ThemeSettingService implements ThemeSettingServiceInterface
{
public function __construct(
private ThemeSettingRepositoryInterface $themeSettingRepository
private ThemeSettingRepositoryInterface $themeSettingRepository,
private JsonServiceInterface $jsonService,
) {
}

Expand Down Expand Up @@ -59,28 +59,17 @@ public function getSelectSetting(ID $name, string $themeId): StringSetting
public function getCollectionSetting(ID $name, string $themeId): StringSetting
{
$collection = $this->themeSettingRepository->getCollection($name, $themeId);
$collectionEncodingResult = $this->jsonService->jsonEncodeArray($collection);

return new StringSetting($name, $this->jsonEncodeCollection($collection));
return new StringSetting($name, $collectionEncodingResult);
}

public function getAssocCollectionSetting(ID $name, string $themeId): StringSetting
{
$assocCollection = $this->themeSettingRepository->getAssocCollection($name, $themeId);
$collectionEncodingResult = $this->jsonService->jsonEncodeArray($assocCollection);

return new StringSetting($name, $this->jsonEncodeCollection($assocCollection));
}

/**
* @throws CollectionEncodingException
*/
private function jsonEncodeCollection(array $collection): string
{
$jsonValue = json_encode($collection);

if ($jsonValue === false) {
throw new CollectionEncodingException();
}
return $jsonValue;
return new StringSetting($name, $collectionEncodingResult);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Setting/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ services:

OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepositoryInterface:
class: OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository

OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonServiceInterface:
class: OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonService
27 changes: 27 additions & 0 deletions tests/Unit/Service/JsonServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\CollectionEncodingException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonService;
use PHPUnit\Framework\TestCase;

class JsonServiceTest extends TestCase
{
public function testJsonEncodeArray(): void
{
$sut = new JsonService();
$value = ['some' => 'value'];

$this->assertSame('{"some":"value"}', $sut->jsonEncodeArray($value));
}

public function testJsonEncodeArrayException(): void
{
$sut = new JsonService();
$value = [&$value];

$this->expectException(CollectionEncodingException::class);
$sut->jsonEncodeArray($value);
}
}
115 changes: 60 additions & 55 deletions tests/Unit/Service/ThemeSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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

use OxidEsales\GraphQL\ConfigurationAccess\Setting\DataType\StringSetting;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\CollectionEncodingException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\InvalidCollection;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ThemeSettingRepositoryInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonServiceInterface;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\ThemeSettingService;
use OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\UnitTestCase;
use TheCodingMachine\GraphQLite\Types\ID;
Expand All @@ -21,7 +22,7 @@ public function testGetThemeSettingInteger(): void
->method('getInteger')
->willReturn(123);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('integerSetting');
$integerSetting = $settingService->getIntegerSetting($nameID, 'awesomeTheme');
Expand All @@ -38,7 +39,7 @@ public function testGetThemeSettingFloat(): void
->method('getFloat')
->willReturn(1.23);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('floatSetting');
$floatSetting = $settingService->getFloatSetting($nameID, 'awesomeTheme');
Expand All @@ -55,7 +56,7 @@ public function testGetThemeSettingBoolean(): void
->method('getBoolean')
->willReturn(false);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('booleanSetting');
$booleanSetting = $settingService->getBooleanSetting($nameID, 'awesomeTheme');
Expand All @@ -72,7 +73,7 @@ public function testGetThemeSettingString(): void
->method('getString')
->willReturn('default');

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('stringSetting');
$stringSetting = $settingService->getStringSetting($nameID, 'awesomeTheme');
Expand All @@ -89,7 +90,7 @@ public function testGetThemeSettingSelect(): void
->method('getSelect')
->willReturn('select');

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('selectSetting');
$selectSetting = $settingService->getSelectSetting($nameID, 'awesomeTheme');
Expand All @@ -99,66 +100,60 @@ public function testGetThemeSettingSelect(): void

public function testGetThemeSettingCollection(): void
{
$serviceCollectionSetting = $this->getCollectionSetting();
$nameID = new ID('arraySetting');
$themeId = 'awesomeTheme';

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repositoryResult = ['nice', 'values'];
$repository->expects($this->once())
->method('getCollection')
->with($nameID, $themeId)
->willReturn(['nice', 'values']);

$settingService = new ThemeSettingService($repository);

$collectionSetting = $settingService->getCollectionSetting($nameID, $themeId);

$this->assertEquals($serviceCollectionSetting, $collectionSetting);
->willReturn($repositoryResult);

$jsonService = $this->createMock(JsonServiceInterface::class);
$collectionEncodingResult = 'someEncodedResult';
$jsonService->method('jsonEncodeArray')
->with($repositoryResult)
->willReturn($collectionEncodingResult);

$sut = $this->getSut(
themeSettingRepository: $repository,
jsonService: $jsonService,
);

$this->assertEquals(
new StringSetting($nameID, $collectionEncodingResult),
$sut->getCollectionSetting($nameID, $themeId)
);
}

public function testGetThemeSettingAssocCollection(): void
{
$serviceAssocCollectionSetting = $this->getAssocCollectionSetting();
$nameID = new ID('aarraySetting');
$themeId = 'awesomeTheme';

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repositoryResult = ['first' => '10', 'second' => '20', 'third' => '50'];
$repository->expects($this->once())
->method('getAssocCollection')
->with($nameID, $themeId)
->willReturn(['first' => '10', 'second' => '20', 'third' => '50']);

$settingService = new ThemeSettingService($repository);

$assocCollectionSetting = $settingService->getAssocCollectionSetting($nameID, $themeId);

$this->assertEquals($serviceAssocCollectionSetting, $assocCollectionSetting);
}

/** @dataProvider collectionEncodingExceptionDataProvider */
public function testCollectionEncodingExceptionThrown(string $repositoryMethod, string $serviceMethod): void
{
$nameID = new ID('arraySetting');
$themeId = 'awesomeTheme';
$repositoryResponse = [&$repositoryResponse];

$repository = $this->createMock(ThemeSettingRepositoryInterface::class);
$repository->expects($this->once())
->method($repositoryMethod)
->with($nameID, $themeId)
->willReturn($repositoryResponse);

$sut = new ThemeSettingService($repository);

$this->expectException(CollectionEncodingException::class);

$sut->$serviceMethod($nameID, $themeId);
}

public function collectionEncodingExceptionDataProvider(): \Generator
{
yield ['repositoryMethod' => 'getCollection', 'serviceMethod' => 'getCollectionSetting'];
yield ['repositoryMethod' => 'getAssocCollection', 'serviceMethod' => 'getAssocCollectionSetting'];
->willReturn($repositoryResult);

$jsonService = $this->createMock(JsonServiceInterface::class);
$collectionEncodingResult = 'someEncodedResult';
$jsonService->method('jsonEncodeArray')
->with($repositoryResult)
->willReturn($collectionEncodingResult);

$sut = $this->getSut(
themeSettingRepository: $repository,
jsonService: $jsonService
);

$this->assertEquals(
new StringSetting($nameID, $collectionEncodingResult),
$sut->getAssocCollectionSetting($nameID, $themeId)
);
}

public function testListThemeSettings(): void
Expand All @@ -176,7 +171,7 @@ public function testListThemeSettings(): void
->with($themeId)
->willReturn($repositorySettingsList);

$sut = new ThemeSettingService($repository);
$sut = $this->getSut(themeSettingRepository: $repository);
$this->assertEquals($this->getSettingTypeList(), $sut->getSettingsList($themeId));
}

Expand All @@ -189,7 +184,7 @@ public function testChangeThemeSettingInteger(): void
->with($nameID, 'awesomeTheme')
->willReturn(123);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$integerSetting = $settingService->changeIntegerSetting($nameID, 123, 'awesomeTheme');

Expand All @@ -201,7 +196,7 @@ public function testChangeThemeSettingFloat(): void
{
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('floatSetting');
$floatSetting = $settingService->changeFloatSetting($nameID, 1.23, 'awesomeTheme');
Expand All @@ -214,7 +209,7 @@ public function testChangeThemeSettingBoolean(): void
{
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('boolSetting');
$value = false;
Expand All @@ -228,7 +223,7 @@ public function testChangeThemeSettingString(): void
{
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('stringSetting');
$value = 'default';
Expand All @@ -245,7 +240,7 @@ public function testChangeThemeSettingInvalidCollection($value): void
{
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('collectionSetting');

Expand All @@ -271,7 +266,7 @@ public function testChangeThemeSettingCollection(): void
{
$repository = $this->createMock(ThemeSettingRepositoryInterface::class);

$settingService = new ThemeSettingService($repository);
$settingService = $this->getSut(themeSettingRepository: $repository);

$nameID = new ID('collectionSetting');
$value = '[2, "values"]';
Expand All @@ -280,4 +275,14 @@ public function testChangeThemeSettingCollection(): void
$this->assertSame($nameID, $collectionSetting->getName());
$this->assertSame($value, $collectionSetting->getValue());
}

public function getSut(
?ThemeSettingRepositoryInterface $themeSettingRepository = null,
?JsonServiceInterface $jsonService = null,
): ThemeSettingService {
return new ThemeSettingService(
themeSettingRepository: $themeSettingRepository ?? $this->createStub(ThemeSettingRepositoryInterface::class),
jsonService: $jsonService ?? $this->createStub(JsonServiceInterface::class)
);
}
}

0 comments on commit 4fc105d

Please sign in to comment.