mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-08 22:13:45 +00:00
Add StoredObjectLockNormalizer with corresponding test suite
- Implemented `StoredObjectLockNormalizer` to handle JSON normalization for `StoredObjectLock` entities. - Added `StoredObjectLockNormalizerTest` to verify normalization logic, format support, and edge cases.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectLock;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
final class StoredObjectLockNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
public function normalize($object, ?string $format = null, array $context = []): array
|
||||
{
|
||||
assert($object instanceof StoredObjectLock);
|
||||
|
||||
return [
|
||||
'uuid' => $object->getUuid(),
|
||||
'method' => $object->getMethod(),
|
||||
'expiresAt' => $this->normalizer->normalize($object->getExpireAt(), $format, $context),
|
||||
'users' => $this->normalizer->normalize($object->getUsers()->getValues(), $format, $context),
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, ?string $format = null): bool
|
||||
{
|
||||
return 'json' === $format && $data instanceof StoredObjectLock;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\Tests\Serializer\Normalizer;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectLock;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectLockMethodEnum;
|
||||
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectLockNormalizer;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @covers \Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectLockNormalizer
|
||||
*/
|
||||
class StoredObjectLockNormalizerTest extends TestCase
|
||||
{
|
||||
private StoredObjectLockNormalizer $normalizer;
|
||||
private NormalizerInterface $childNormalizer;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->normalizer = new StoredObjectLockNormalizer();
|
||||
$this->childNormalizer = $this->createMock(NormalizerInterface::class);
|
||||
$this->normalizer->setNormalizer($this->childNormalizer);
|
||||
}
|
||||
|
||||
public function testSupportsNormalization(): void
|
||||
{
|
||||
$storedObject = new StoredObject('pending');
|
||||
$lock = new StoredObjectLock(
|
||||
$storedObject,
|
||||
StoredObjectLockMethodEnum::WEBDAV,
|
||||
new \DateTimeImmutable()
|
||||
);
|
||||
|
||||
$this->assertTrue($this->normalizer->supportsNormalization($lock, 'json'));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization($lock, 'xml'));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization($storedObject, 'json'));
|
||||
}
|
||||
|
||||
public function testNormalize(): void
|
||||
{
|
||||
$storedObject = new StoredObject('pending');
|
||||
$user = $this->createMock(User::class);
|
||||
$expireAt = new \DateTimeImmutable('2026-04-08 23:00:00');
|
||||
$createdAt = new \DateTimeImmutable('2026-04-08 22:00:00');
|
||||
|
||||
$lock = new StoredObjectLock(
|
||||
$storedObject,
|
||||
StoredObjectLockMethodEnum::WEBDAV,
|
||||
$createdAt,
|
||||
'some-token',
|
||||
$expireAt,
|
||||
[$user]
|
||||
);
|
||||
|
||||
$this->childNormalizer->expects($this->exactly(2))
|
||||
->method('normalize')
|
||||
->withAnyParameters()
|
||||
->willReturnCallback(function ($data, $format, $context) use ($expireAt, $user) {
|
||||
if ($data === $expireAt) {
|
||||
return '2026-04-08T23:00:00+00:00';
|
||||
}
|
||||
if ($data === [$user]) {
|
||||
return [['username' => 'testuser']];
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
$result = $this->normalizer->normalize($lock, 'json');
|
||||
|
||||
$this->assertEquals([
|
||||
'uuid' => $lock->getUuid(),
|
||||
'method' => StoredObjectLockMethodEnum::WEBDAV,
|
||||
'expiresAt' => '2026-04-08T23:00:00+00:00',
|
||||
'users' => [['username' => 'testuser']],
|
||||
], $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user