Skip to content

Commit

Permalink
Merge pull request #331 from api-platform/feat/event_listeners_bc_layer
Browse files Browse the repository at this point in the history
Disable `event_listeners_backward_compatibility_layer`
  • Loading branch information
vincentchalamon authored Sep 25, 2023
2 parents ad302ab + ea426fd commit 114a4cc
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ api_platform:
extra_properties:
standard_put: true
rfc_7807_compliant_errors: true
event_listeners_backward_compatibility_layer: false
oauth:
enabled: true
clientId: '%env(OIDC_SWAGGER_CLIENT_ID)%'
Expand Down
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;
}
}
2 changes: 1 addition & 1 deletion api/tests/Api/ReviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function testAsAUserICannotAddAReviewWithValidDataOnAnInvalidBook(): void
self::assertJsonContains([
'@type' => 'hydra:Error',
'hydra:title' => 'An error occurred',
'hydra:description' => 'Invalid identifier value or configuration.',
'hydra:description' => 'Invalid uri variables.',
]);
}

Expand Down
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 114a4cc

Please sign in to comment.