From 10fcc4e54316fa0cce89b611a7f01c898c8ea51c Mon Sep 17 00:00:00 2001 From: Vincent Chalamon <407859+vincentchalamon@users.noreply.github.com> Date: Mon, 25 Sep 2023 09:59:02 +0200 Subject: [PATCH] fix: fix remove processors --- api/src/State/Processor/BookRemoveProcessor.php | 6 ++---- api/src/State/Processor/MercureProcessor.php | 8 +++++--- api/src/State/Processor/ReviewRemoveProcessor.php | 6 ++---- api/tests/State/Processor/BookRemoveProcessorTest.php | 5 +++-- api/tests/State/Processor/MercureProcessorTest.php | 2 +- api/tests/State/Processor/ReviewRemoveProcessorTest.php | 5 +++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/api/src/State/Processor/BookRemoveProcessor.php b/api/src/State/Processor/BookRemoveProcessor.php index de79ac002..7b33351b7 100644 --- a/api/src/State/Processor/BookRemoveProcessor.php +++ b/api/src/State/Processor/BookRemoveProcessor.php @@ -28,7 +28,7 @@ public function __construct( /** * @param Book $data */ - public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Book + public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void { $object = clone $data; @@ -48,11 +48,9 @@ public function process(mixed $data, Operation $operation, array $uriVariables = $uriVariables, $context + [ 'item_uri_template' => $uriTemplate, - 'data' => json_encode(['@id' => $iri]), + MercureProcessor::DATA => json_encode(['@id' => $iri]), ] ); } - - return $data; } } diff --git a/api/src/State/Processor/MercureProcessor.php b/api/src/State/Processor/MercureProcessor.php index 20690a6ae..baa693c73 100644 --- a/api/src/State/Processor/MercureProcessor.php +++ b/api/src/State/Processor/MercureProcessor.php @@ -19,6 +19,8 @@ final readonly class MercureProcessor implements ProcessorInterface { + public const DATA = 'mercure_data'; + public function __construct( private SerializerInterface $serializer, private HubRegistry $hubRegistry, @@ -42,8 +44,8 @@ public function process(mixed $data, Operation $operation, array $uriVariables = if (!isset($context['topics'])) { $context['topics'] = [$this->iriConverter->getIriFromResource($data, UrlGeneratorInterface::ABS_URL, $operation)]; } - if (!isset($context['data'])) { - $context['data'] = $this->serializer->serialize( + if (!isset($context[self::DATA])) { + $context[self::DATA] = $this->serializer->serialize( $data, key($this->formats), ($operation->getNormalizationContext() ?? []) + (isset($context['item_uri_template']) ? [ @@ -54,7 +56,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables = $this->hubRegistry->getHub()->publish(new Update( topics: $context['topics'], - data: $context['data'] + data: $context[self::DATA] )); return $data; diff --git a/api/src/State/Processor/ReviewRemoveProcessor.php b/api/src/State/Processor/ReviewRemoveProcessor.php index 05efb2ce9..4f8401938 100644 --- a/api/src/State/Processor/ReviewRemoveProcessor.php +++ b/api/src/State/Processor/ReviewRemoveProcessor.php @@ -28,7 +28,7 @@ public function __construct( /** * @param Review $data */ - public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Review + public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void { $object = clone $data; @@ -48,11 +48,9 @@ public function process(mixed $data, Operation $operation, array $uriVariables = $uriVariables, $context + [ 'item_uri_template' => $uriTemplate, - 'data' => json_encode(['@id' => $iri]), + MercureProcessor::DATA => json_encode(['@id' => $iri]), ] ); } - - return $data; } } diff --git a/api/tests/State/Processor/BookRemoveProcessorTest.php b/api/tests/State/Processor/BookRemoveProcessorTest.php index 8eea20cb4..f0217c6e2 100644 --- a/api/tests/State/Processor/BookRemoveProcessorTest.php +++ b/api/tests/State/Processor/BookRemoveProcessorTest.php @@ -14,6 +14,7 @@ use ApiPlatform\State\ProcessorInterface; use App\Entity\Book; use App\State\Processor\BookRemoveProcessor; +use App\State\Processor\MercureProcessor; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -87,7 +88,7 @@ public function testItRemovesBookAndSendMercureUpdates(): void [], [ 'item_uri_template' => '/admin/books/{id}{._format}', - 'data' => json_encode(['@id' => '/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), + MercureProcessor::DATA => json_encode(['@id' => '/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), ], ], [ @@ -96,7 +97,7 @@ public function testItRemovesBookAndSendMercureUpdates(): void [], [ 'item_uri_template' => '/books/{id}{._format}', - 'data' => json_encode(['@id' => '/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), + MercureProcessor::DATA => json_encode(['@id' => '/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), ], ], ); diff --git a/api/tests/State/Processor/MercureProcessorTest.php b/api/tests/State/Processor/MercureProcessorTest.php index 6192d096a..8b48947ba 100644 --- a/api/tests/State/Processor/MercureProcessorTest.php +++ b/api/tests/State/Processor/MercureProcessorTest.php @@ -104,7 +104,7 @@ public function testItSendsAMercureUpdateWithContextOptions(): void $this->processor->process($this->objectMock, $this->operationMock, [], [ 'item_uri_template' => '/admin/books/{id}{._format}', 'topics' => ['/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'], - 'data' => json_encode(['bar' => 'baz']), + MercureProcessor::DATA => json_encode(['bar' => 'baz']), ]); } } diff --git a/api/tests/State/Processor/ReviewRemoveProcessorTest.php b/api/tests/State/Processor/ReviewRemoveProcessorTest.php index e1f434e70..12dc38928 100644 --- a/api/tests/State/Processor/ReviewRemoveProcessorTest.php +++ b/api/tests/State/Processor/ReviewRemoveProcessorTest.php @@ -13,6 +13,7 @@ use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; use ApiPlatform\State\ProcessorInterface; use App\Entity\Review; +use App\State\Processor\MercureProcessor; use App\State\Processor\ReviewRemoveProcessor; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -87,7 +88,7 @@ public function testItRemovesBookAndSendMercureUpdates(): void [], [ 'item_uri_template' => '/admin/reviews/{id}{._format}', - 'data' => json_encode(['@id' => '/admin/reviews/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), + MercureProcessor::DATA => json_encode(['@id' => '/admin/reviews/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), ], ], [ @@ -96,7 +97,7 @@ public function testItRemovesBookAndSendMercureUpdates(): void [], [ 'item_uri_template' => '/books/{bookId}/reviews/{id}{._format}', - 'data' => json_encode(['@id' => '/books/8ad70d36-abaf-4c9b-aeaa-7ec63e6ca6f3/reviews/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), + MercureProcessor::DATA => json_encode(['@id' => '/books/8ad70d36-abaf-4c9b-aeaa-7ec63e6ca6f3/reviews/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6']), ], ], );