Skip to content

Commit b5c72b7

Browse files
authored
[Mercure] Add a normalization_context option in mercure attribute (#3772)
* chore: bump PHPUnit and phpDocumentor * feat: add a normalization_context option for mercure attribute
1 parent 1c7c26d commit b5c72b7

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jobs:
122122
composer update --no-progress --no-suggest --ansi
123123
- name: Install PHPUnit
124124
env:
125-
SYMFONY_PHPUNIT_VERSION: '9.2'
125+
SYMFONY_PHPUNIT_VERSION: '9.4'
126126
run: vendor/bin/simple-phpunit --version
127127
- name: Clear test app cache
128128
run: |
@@ -140,7 +140,7 @@ jobs:
140140
continue-on-error: true
141141
- name: Run PHPStan analysis
142142
env:
143-
SYMFONY_PHPUNIT_VERSION: '9.2'
143+
SYMFONY_PHPUNIT_VERSION: '9.4'
144144
run: vendor/bin/phpstan analyse --no-progress --no-interaction --ansi
145145

146146
phpunit:

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"jangregor/phpstan-prophecy": "^0.6",
4848
"justinrainbow/json-schema": "^5.2.1",
4949
"nelmio/api-doc-bundle": "^2.13.4",
50-
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0",
51-
"phpdocumentor/type-resolver": "^0.3 || ^0.4",
50+
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.0",
51+
"phpdocumentor/type-resolver": "^0.3 || ^0.4 || ^0.5 || ^0.6 || ^0.7 || ^1.0",
5252
"phpstan/extension-installer": "^1.0",
5353
"phpstan/phpstan": "^0.12.4",
5454
"phpstan/phpstan-doctrine": "^0.12.7",

src/Bridge/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ final class PublishMercureUpdatesListener
4848
'id' => true,
4949
'type' => true,
5050
'retry' => true,
51+
'normalization_context' => true,
5152
];
5253

5354
use DispatchTrait;
@@ -215,7 +216,7 @@ private function publishUpdate($object, array $options, string $type): void
215216
$data = $options['data'] ?? json_encode(['@id' => $object->id]);
216217
} else {
217218
$resourceClass = $this->getObjectClass($object);
218-
$context = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('normalization_context', []);
219+
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getAttribute('normalization_context', []);
219220

220221
$iri = $options['topics'] ?? $this->iriConverter->getIriFromItem($object, UrlGeneratorInterface::ABS_URL);
221222
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);

tests/Bridge/Doctrine/EventListener/PublishMercureUpdatesListenerTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2727
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
2828
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
29+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
2930
use ApiPlatform\Core\Tests\ProphecyTrait;
3031
use Doctrine\ORM\EntityManagerInterface;
3132
use Doctrine\ORM\Event\OnFlushEventArgs;
@@ -137,6 +138,7 @@ public function testPublishUpdate(): void
137138
$toUpdate = new Dummy();
138139
$toUpdate->setId(2);
139140
$toUpdateNoMercureAttribute = new DummyCar();
141+
$toUpdateMercureOptions = new DummyOffer();
140142

141143
$toDelete = new Dummy();
142144
$toDelete->setId(3);
@@ -147,10 +149,12 @@ public function testPublishUpdate(): void
147149
$resourceClassResolverProphecy->getResourceClass(Argument::type(Dummy::class))->willReturn(Dummy::class);
148150
$resourceClassResolverProphecy->getResourceClass(Argument::type(DummyCar::class))->willReturn(DummyCar::class);
149151
$resourceClassResolverProphecy->getResourceClass(Argument::type(DummyFriend::class))->willReturn(DummyFriend::class);
152+
$resourceClassResolverProphecy->getResourceClass(Argument::type(DummyOffer::class))->willReturn(DummyOffer::class);
150153
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
151154
$resourceClassResolverProphecy->isResourceClass(NotAResource::class)->willReturn(false);
152155
$resourceClassResolverProphecy->isResourceClass(DummyCar::class)->willReturn(true);
153156
$resourceClassResolverProphecy->isResourceClass(DummyFriend::class)->willReturn(true);
157+
$resourceClassResolverProphecy->isResourceClass(DummyOffer::class)->willReturn(true);
154158

155159
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
156160
$iriConverterProphecy->getIriFromItem($toInsert, UrlGeneratorInterface::ABS_URL)->willReturn('http://example.com/dummies/1')->shouldBeCalled();
@@ -164,10 +168,12 @@ public function testPublishUpdate(): void
164168
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['mercure' => true, 'normalization_context' => ['groups' => ['foo', 'bar']]]));
165169
$resourceMetadataFactoryProphecy->create(DummyCar::class)->willReturn(new ResourceMetadata());
166170
$resourceMetadataFactoryProphecy->create(DummyFriend::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['mercure' => ['private' => true, 'retry' => 10]]));
171+
$resourceMetadataFactoryProphecy->create(DummyOffer::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['mercure' => ['topics' => 'http://example.com/custom_topics/1', 'normalization_context' => ['groups' => ['baz']]]]));
167172

168173
$serializerProphecy = $this->prophesize(SerializerInterface::class);
169174
$serializerProphecy->serialize($toInsert, 'jsonld', ['groups' => ['foo', 'bar']])->willReturn('1');
170175
$serializerProphecy->serialize($toUpdate, 'jsonld', ['groups' => ['foo', 'bar']])->willReturn('2');
176+
$serializerProphecy->serialize($toUpdateMercureOptions, 'jsonld', ['groups' => ['baz']])->willReturn('mercure_options');
171177

172178
$formats = ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']];
173179

@@ -194,7 +200,7 @@ public function testPublishUpdate(): void
194200

195201
$uowProphecy = $this->prophesize(UnitOfWork::class);
196202
$uowProphecy->getScheduledEntityInsertions()->willReturn([$toInsert, $toInsertNotResource])->shouldBeCalled();
197-
$uowProphecy->getScheduledEntityUpdates()->willReturn([$toUpdate, $toUpdateNoMercureAttribute])->shouldBeCalled();
203+
$uowProphecy->getScheduledEntityUpdates()->willReturn([$toUpdate, $toUpdateNoMercureAttribute, $toUpdateMercureOptions])->shouldBeCalled();
198204
$uowProphecy->getScheduledEntityDeletions()->willReturn([$toDelete, $toDeleteExpressionLanguage])->shouldBeCalled();
199205

200206
$emProphecy = $this->prophesize(EntityManagerInterface::class);
@@ -204,9 +210,9 @@ public function testPublishUpdate(): void
204210
$listener->onFlush($eventArgs);
205211
$listener->postFlush();
206212

207-
$this->assertSame(['http://example.com/dummies/1', 'http://example.com/dummies/2', 'http://example.com/dummies/3', 'http://example.com/dummy_friends/4'], $topics);
208-
$this->assertSame([false, false, false, true], $private);
209-
$this->assertSame([null, null, null, 10], $retry);
213+
$this->assertSame(['http://example.com/dummies/1', 'http://example.com/dummies/2', 'http://example.com/custom_topics/1', 'http://example.com/dummies/3', 'http://example.com/dummy_friends/4'], $topics);
214+
$this->assertSame([false, false, false, false, true], $private);
215+
$this->assertSame([null, null, null, null, 10], $retry);
210216
}
211217

212218
public function testPublishGraphQlUpdates(): void

0 commit comments

Comments
 (0)