Skip to content

Commit

Permalink
OXDEV-7557 Fix possible collection encoding error
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 99f5a62 commit 5b37c60
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/Setting/Exception/CollectionEncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

declare(strict_types=1);

namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception;

final class CollectionEncodingException extends \Exception
{
}
24 changes: 20 additions & 4 deletions src/Setting/Service/ThemeSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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 Down Expand Up @@ -57,14 +58,29 @@ public function getSelectSetting(ID $name, string $themeId): StringSetting

public function getCollectionSetting(ID $name, string $themeId): StringSetting
{
$collectionString = $this->themeSettingRepository->getCollection($name, $themeId);
return new StringSetting($name, json_encode($collectionString));
$collection = $this->themeSettingRepository->getCollection($name, $themeId);

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

public function getAssocCollectionSetting(ID $name, string $themeId): StringSetting
{
$assocCollectionString = $this->themeSettingRepository->getAssocCollection($name, $themeId);
return new StringSetting($name, json_encode($assocCollectionString));
$assocCollection = $this->themeSettingRepository->getAssocCollection($name, $themeId);

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;
}

/**
Expand Down
39 changes: 35 additions & 4 deletions tests/Unit/Service/ThemeSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Service;

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\ThemeSettingService;
Expand Down Expand Up @@ -99,37 +100,67 @@ public function testGetThemeSettingSelect(): void
public function testGetThemeSettingCollection(): void
{
$serviceCollectionSetting = $this->getCollectionSetting();
$nameID = new ID('arraySetting');
$themeId = 'awesomeTheme';

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

$settingService = new ThemeSettingService($repository);

$nameID = new ID('arraySetting');
$collectionSetting = $settingService->getCollectionSetting($nameID, 'awesomeTheme');
$collectionSetting = $settingService->getCollectionSetting($nameID, $themeId);

$this->assertEquals($serviceCollectionSetting, $collectionSetting);
}

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

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

$settingService = new ThemeSettingService($repository);

$nameID = new ID('aarraySetting');
$assocCollectionSetting = $settingService->getAssocCollectionSetting($nameID, 'awesomeTheme');
$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'];
}

public function testListThemeSettings(): void
{
$themeId = 'awesomeTheme';
Expand Down

0 comments on commit 5b37c60

Please sign in to comment.