Skip to content

Commit

Permalink
OXDEV-7557 Add jsonDecodeCollection method to JsonService
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 15, 2023
1 parent 421da94 commit 618fb4c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Setting/Service/JsonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OxidEsales\GraphQL\ConfigurationAccess\Setting\Service;

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

class JsonService implements JsonServiceInterface
{
Expand All @@ -18,4 +19,22 @@ public function jsonEncodeArray(array $collection): string
}
return $jsonValue;
}

/**
* @throws InvalidCollection
*/
public function jsonDecodeCollection(string $value): array
{
if ($value === '') {
return [];
}

$arrayValue = json_decode($value, true);

if (!is_array($arrayValue) || json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidCollection($value);
}

return $arrayValue;
}
}
2 changes: 2 additions & 0 deletions src/Setting/Service/JsonServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
interface JsonServiceInterface
{
public function jsonEncodeArray(array $collection): string;

public function jsonDecodeCollection(string $value): array;
}
39 changes: 39 additions & 0 deletions tests/Unit/Service/JsonServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace OxidEsales\GraphQL\ConfigurationAccess\Tests\Unit\Service;

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

/**
* @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\Service\JsonService
*/
class JsonServiceTest extends TestCase
{
public function testJsonEncodeArray(): void
Expand All @@ -24,4 +28,39 @@ public function testJsonEncodeArrayException(): void
$this->expectException(CollectionEncodingException::class);
$sut->jsonEncodeArray($value);
}

/** @dataProvider jsonDecodeCollectionDataProvider */
public function testJsonDecodeCollection(string $value, array $expectedResult): void
{
$sut = new JsonService();

$this->assertEquals($expectedResult, $sut->jsonDecodeCollection($value));
}

public function jsonDecodeCollectionDataProvider(): \Generator
{
yield [
'value' => '',
'result' => []
];

yield [
'value' => '["apple","banana"]',
'result' => ["apple", "banana"]
];

yield [
'value' => '{"name":"John","age":30}',
'result' => ["name" => "John", "age" => 30]
];
}

public function testJsonDecodeCollectionException(): void
{
$sut = new JsonService();
$value = '[2, "values"';

$this->expectException(InvalidCollection::class);
$sut->jsonDecodeCollection($value);
}
}

0 comments on commit 618fb4c

Please sign in to comment.