Skip to content

Context stamp #3157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Bridge/Symfony/Messenger/ContextStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Messenger;

use Symfony\Component\Messenger\Stamp\StampInterface;

/**
* An envelope stamp with context which related to a message.
*
* @experimental
*
* @author Sergii Pavlenko <[email protected]>
*/
final class ContextStamp implements StampInterface
{
private $context;

public function __construct(array $context = [])
{
$this->context = $context;
}

/**
* Get the context related to a message.
*/
public function getContext(): array
{
return $this->context;
}
}
5 changes: 4 additions & 1 deletion src/Bridge/Symfony/Messenger/DataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function supports($data, array $context = []): bool
*/
public function persist($data, array $context = [])
{
$envelope = $this->dispatch($data);
$envelope = $this->dispatch(
(new Envelope($data))
->with(new ContextStamp($context))
);

$handledStamp = $envelope->last(HandledStamp::class);
if (!$handledStamp instanceof HandledStamp) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Bridge/Symfony/Messenger/ContextStampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Bridge\Symfony\Messenger;

use ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
* @author Sergii Pavlenko <[email protected]>
*/
class ContextStampTest extends TestCase
{
public function testConstruct()
{
$this->assertInstanceOf(StampInterface::class, new ContextStamp());
}

public function testGetContext()
{
$contextStamp = new ContextStamp();
$this->assertIsArray($contextStamp->getContext());
}
}
10 changes: 8 additions & 2 deletions tests/Bridge/Symfony/Messenger/DataPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Tests\Bridge\Symfony\Messenger;

use ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp;
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister;
use ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
Expand Down Expand Up @@ -56,7 +57,9 @@ public function testPersist()
$dummy = new Dummy();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch($dummy)->willReturn(new Envelope($dummy))->shouldBeCalled();
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change necessary?

Copy link
Contributor Author

@sergepavle sergepavle Oct 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes is consistent with code from testRemove().
By this changes we make sure that have envelop with ContextStamp.

}))->willReturn(new Envelope($dummy))->shouldBeCalled();

$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
$this->assertSame($dummy, $dataPersister->persist($dummy));
Expand All @@ -67,6 +70,7 @@ public function testRemove()
$dummy = new Dummy();

$messageBus = $this->prophesize(MessageBusInterface::class);

$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(RemoveStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();
Expand All @@ -80,7 +84,9 @@ public function testHandle()
$dummy = new Dummy();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch($dummy)->willReturn((new Envelope($dummy))->with(new HandledStamp($dummy, 'DummyHandler::__invoke')))->shouldBeCalled();
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
}))->willReturn((new Envelope($dummy))->with(new HandledStamp($dummy, 'DummyHandler::__invoke')))->shouldBeCalled();

$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
$this->assertSame($dummy, $dataPersister->persist($dummy));
Expand Down