Skip to content

Commit

Permalink
OXDEV-8728 Implement json serialization Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
RahatHameed committed Sep 24, 2024
1 parent 3699ee8 commit 7a7d9c4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/UserData/Exception/JsonSerializationException.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\GdprOptinModule\UserData\Exception;

class JsonSerializationException extends \Exception
{
}
4 changes: 4 additions & 0 deletions src/UserData/Service/CollectionSerializerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

use OxidEsales\GdprOptinModule\UserData\DataType\ResultFileInterface;
use OxidEsales\GdprOptinModule\UserData\DataType\TableCollectionInterface;
use OxidEsales\GdprOptinModule\UserData\Exception\JsonSerializationException;

interface CollectionSerializerServiceInterface
{
/**
* @throws JsonSerializationException
*/
public function serializeCollection(TableCollectionInterface $data): ResultFileInterface;
}
9 changes: 8 additions & 1 deletion src/UserData/Service/JsonCollectionSerializerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
use OxidEsales\GdprOptinModule\UserData\DataType\ResultFile;
use OxidEsales\GdprOptinModule\UserData\DataType\ResultFileInterface;
use OxidEsales\GdprOptinModule\UserData\DataType\TableCollectionInterface;
use OxidEsales\GdprOptinModule\UserData\Exception\JsonSerializationException;

class JsonCollectionSerializerService implements CollectionSerializerServiceInterface
{
public function serializeCollection(TableCollectionInterface $data): ResultFileInterface
{
return new ResultFile($data->getCollectionName(), json_encode($data->getCollection()));
$jsonData = json_encode($data->getCollection());

if ($jsonData === false) {
throw new JsonSerializationException();
}

return new ResultFile($data->getCollectionName(), $jsonData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OxidEsales\GdprOptinModule\Tests\Unit\UserData\Service;

use OxidEsales\GdprOptinModule\UserData\DataType\TableCollectionInterface;
use OxidEsales\GdprOptinModule\UserData\Exception\JsonSerializationException;
use OxidEsales\GdprOptinModule\UserData\Service\JsonCollectionSerializerService;
use PHPUnit\Framework\TestCase;

Expand All @@ -35,4 +36,23 @@ public function testSerializeCollection(): void
$this->assertSame($expectedCollectionName, $actualResult->getFileName());
$this->assertSame(json_encode($expectedCollection), $actualResult->getContent());
}

public function testSerializeCollectionThrowsExceptionOnJsonError()
{
$invalidString = "\xB1\x31\x32";
$invalidCollection = [
['someColum1' => uniqid(), 'someColum2' => $invalidString]
];

$tableCollectionMock = $this->createConfiguredMock(TableCollectionInterface::class, [
'getCollection' => $invalidCollection,
'getCollectionName' => uniqid(),
]);

$sut = new JsonCollectionSerializerService();

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

$sut->serializeCollection($tableCollectionMock);
}
}

0 comments on commit 7a7d9c4

Please sign in to comment.