Skip to content

Commit

Permalink
fix: fix remove processors
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Sep 25, 2023
1 parent 87de340 commit 10fcc4e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
6 changes: 2 additions & 4 deletions api/src/State/Processor/BookRemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
8 changes: 5 additions & 3 deletions api/src/State/Processor/MercureProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

final readonly class MercureProcessor implements ProcessorInterface
{
public const DATA = 'mercure_data';

public function __construct(
private SerializerInterface $serializer,
private HubRegistry $hubRegistry,
Expand All @@ -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']) ? [
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions api/src/State/Processor/ReviewRemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions api/tests/State/Processor/BookRemoveProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']),
],
],
[
Expand All @@ -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']),
],
],
);
Expand Down
2 changes: 1 addition & 1 deletion api/tests/State/Processor/MercureProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
]);
}
}
5 changes: 3 additions & 2 deletions api/tests/State/Processor/ReviewRemoveProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']),
],
],
[
Expand All @@ -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']),
],
],
);
Expand Down

0 comments on commit 10fcc4e

Please sign in to comment.