diff --git a/src/Bundle/ChillDocStoreBundle/Serializer/Normalizer/StoredObjectLockNormalizer.php b/src/Bundle/ChillDocStoreBundle/Serializer/Normalizer/StoredObjectLockNormalizer.php new file mode 100644 index 000000000..c454c236e --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Serializer/Normalizer/StoredObjectLockNormalizer.php @@ -0,0 +1,39 @@ + $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; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/StoredObjectLockNormalizerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/StoredObjectLockNormalizerTest.php new file mode 100644 index 000000000..037773a6d --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Serializer/Normalizer/StoredObjectLockNormalizerTest.php @@ -0,0 +1,92 @@ +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); + } +}