Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Oct 5, 2023
1 parent f233dca commit e1a3a7b
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions tests/Doctrine/EventListener/PublishMercureUpdatesListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Tests\Fixtures\NotAResource;
Expand All @@ -30,6 +31,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyMercure;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyOffer;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MercureWithTopics;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\UnitOfWork;
Expand Down Expand Up @@ -169,6 +171,123 @@ public function testPublishUpdate(): void
$this->assertEquals([null, null, null, null, null, 10, null], $retry);
}

public function testPublishUpdateMultipleTopicsUsingExpressionLanguage(): void
{
$mercure = [
'topics' => [
'@=iri(object)',
'@=iri(object, '.UrlGeneratorInterface::ABS_PATH.')',
'@=iri(object, '.UrlGeneratorInterface::ABS_URL.', getOperation(object, "/custom_resource/mercure_with_topics/{id}{._format}"))',
],
];

$toInsert = new MercureWithTopics();
$toInsert->id = 1;
$toInsert->name = 'Hello World!';

$toUpdate = new MercureWithTopics();
$toUpdate->id = 2;
$toUpdate->name = 'Hello World!';

$toDelete = new MercureWithTopics();
$toDelete->id = 3;
$toDelete->name = 'Hello World!';

// Even if it's the Post operation which sends Updates to Mercure,
// the `mercure` configuration is retrieved from the first operation
// of the resource because the Doctrine Listener doesn't have a
// reference to the operation.
$getOperation = (new Get())->withMercure($mercure)->withShortName('MercureWithTopics');
$customGetOperation = (new Get(uriTemplate: '/custom_resource/mercure_with_topics/{id}{._format}'));
$postOperation = (new Post());

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(Argument::type(MercureWithTopics::class))->willReturn(MercureWithTopics::class);
$resourceClassResolverProphecy->isResourceClass(MercureWithTopics::class)->willReturn(true);

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$iriConverterProphecy->getIriFromResource($toInsert, UrlGeneratorInterface::ABS_URL, null)->willReturn('http://example.com/mercure_with_topics/1')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toInsert, UrlGeneratorInterface::ABS_PATH, null)->willReturn('/mercure_with_topics/1')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toInsert, UrlGeneratorInterface::ABS_URL, Argument::exact($customGetOperation))->willReturn('http://example.com/custom_resource/mercure_with_topics/1')->shouldBeCalled();

$iriConverterProphecy->getIriFromResource($toUpdate, UrlGeneratorInterface::ABS_URL, null)->willReturn('http://example.com/mercure_with_topics/2')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toUpdate, UrlGeneratorInterface::ABS_PATH, null)->willReturn('/mercure_with_topics/2')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toUpdate, UrlGeneratorInterface::ABS_URL, Argument::exact($customGetOperation))->willReturn('http://example.com/custom_resource/mercure_with_topics/2')->shouldBeCalled();

$iriConverterProphecy->getIriFromResource($toDelete)->willReturn('/mercure_with_topics/3')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toDelete, UrlGeneratorInterface::ABS_URL, null)->willReturn('http://example.com/mercure_with_topics/3')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toDelete, UrlGeneratorInterface::ABS_PATH, null)->willReturn('/mercure_with_topics/3')->shouldBeCalled();
$iriConverterProphecy->getIriFromResource($toDelete, UrlGeneratorInterface::ABS_URL, Argument::exact($customGetOperation))->willReturn('http://example.com/custom_resource/mercure_with_topics/3')->shouldBeCalled();

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$resourceMetadataFactoryProphecy->create(MercureWithTopics::class)->willReturn(new ResourceMetadataCollection(MercureWithTopics::class, [
(new ApiResource())->withOperations(new Operations([
'get' => $getOperation,
'custom_get' => $customGetOperation,
'post' => $postOperation,
])),
]))->shouldBeCalled();

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize($toInsert, 'jsonld', [])->willReturn('{"@type":"MercureWithTopics","@id":"/mercure_with_topics/1","id":1,"name":"Hello World!"}')->shouldBeCalled();
$serializerProphecy->serialize($toUpdate, 'jsonld', [])->willReturn('{"@type":"MercureWithTopics","@id":"/mercure_with_topics/2","id":2,"name":"Hello World!"}')->shouldBeCalled();

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

$topics = [];
$private = [];
$retry = [];
$data = [];

$defaultHub = $this->createMockHub(function (Update $update) use (&$topics, &$private, &$retry, &$data): string {
$topics = array_merge($topics, $update->getTopics());
$private[] = $update->isPrivate();
$retry[] = $update->getRetry();
$data[] = $update->getData();

return 'id';
});

$listener = new PublishMercureUpdatesListener(
$resourceClassResolverProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceMetadataFactoryProphecy->reveal(),
$serializerProphecy->reveal(),
$formats,
null,
new HubRegistry($defaultHub, ['default' => $defaultHub]),
null,
null,
null,
true,
);

$uowProphecy = $this->prophesize(UnitOfWork::class);
$uowProphecy->getScheduledEntityInsertions()->willReturn([$toInsert])->shouldBeCalled();
$uowProphecy->getScheduledEntityUpdates()->willReturn([$toUpdate])->shouldBeCalled();
$uowProphecy->getScheduledEntityDeletions()->willReturn([$toDelete])->shouldBeCalled();

$emProphecy = $this->prophesize(EntityManagerInterface::class);
$emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal())->shouldBeCalled();
$eventArgs = new OnFlushEventArgs($emProphecy->reveal());

$listener->onFlush($eventArgs);
$listener->postFlush();

$this->assertEquals([
'{"@type":"MercureWithTopics","@id":"/mercure_with_topics/1","id":1,"name":"Hello World!"}',
'{"@type":"MercureWithTopics","@id":"/mercure_with_topics/2","id":2,"name":"Hello World!"}',
'{"@id":"\/mercure_with_topics\/3","@type":"MercureWithTopics"}',
], $data);
$this->assertEquals([
'http://example.com/mercure_with_topics/1', '/mercure_with_topics/1', 'http://example.com/custom_resource/mercure_with_topics/1',
'http://example.com/mercure_with_topics/2', '/mercure_with_topics/2', 'http://example.com/custom_resource/mercure_with_topics/2',
'http://example.com/mercure_with_topics/3', '/mercure_with_topics/3', 'http://example.com/custom_resource/mercure_with_topics/3',
], $topics);
}

public function testPublishGraphQlUpdates(): void
{
$toUpdate = new Dummy();
Expand Down

0 comments on commit e1a3a7b

Please sign in to comment.