From 4fc105d87ade7eca13de2c503c041980700f4642 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Tue, 14 Nov 2023 17:36:01 +0200 Subject: [PATCH] OXDEV-7557 extract Json service from ThemeSettingService Signed-off-by: Anton Fedurtsya --- src/Setting/Service/JsonService.php | 21 ++++ src/Setting/Service/JsonServiceInterface.php | 8 ++ src/Setting/Service/ThemeSettingService.php | 23 +--- src/Setting/services.yaml | 3 + tests/Unit/Service/JsonServiceTest.php | 27 ++++ .../Unit/Service/ThemeSettingServiceTest.php | 115 +++++++++--------- 6 files changed, 125 insertions(+), 72 deletions(-) create mode 100644 src/Setting/Service/JsonService.php create mode 100644 src/Setting/Service/JsonServiceInterface.php create mode 100644 tests/Unit/Service/JsonServiceTest.php diff --git a/src/Setting/Service/JsonService.php b/src/Setting/Service/JsonService.php new file mode 100644 index 0000000..252bfaa --- /dev/null +++ b/src/Setting/Service/JsonService.php @@ -0,0 +1,21 @@ +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); } /** diff --git a/src/Setting/services.yaml b/src/Setting/services.yaml index f07fbbd..ea4fe38 100644 --- a/src/Setting/services.yaml +++ b/src/Setting/services.yaml @@ -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 diff --git a/tests/Unit/Service/JsonServiceTest.php b/tests/Unit/Service/JsonServiceTest.php new file mode 100644 index 0000000..3c56bb7 --- /dev/null +++ b/tests/Unit/Service/JsonServiceTest.php @@ -0,0 +1,27 @@ + 'value']; + + $this->assertSame('{"some":"value"}', $sut->jsonEncodeArray($value)); + } + + public function testJsonEncodeArrayException(): void + { + $sut = new JsonService(); + $value = [&$value]; + + $this->expectException(CollectionEncodingException::class); + $sut->jsonEncodeArray($value); + } +} diff --git a/tests/Unit/Service/ThemeSettingServiceTest.php b/tests/Unit/Service/ThemeSettingServiceTest.php index bbd5b6e..931c3c3 100644 --- a/tests/Unit/Service/ThemeSettingServiceTest.php +++ b/tests/Unit/Service/ThemeSettingServiceTest.php @@ -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; @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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 @@ -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)); } @@ -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'); @@ -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'); @@ -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; @@ -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'; @@ -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'); @@ -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"]'; @@ -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) + ); + } }