Skip to content

Commit 1931f16

Browse files
Sergii Pavlenkodunglas
authored andcommitted
Context stamp (#3157)
* Issue #3082: Add and use ContextStamp. * Issue #3082: Add tests. * Issue #3157: Correct passing of context. * Issue #3157: Minor corrections.
1 parent a4c8af6 commit 1931f16

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
15+
16+
use Symfony\Component\Messenger\Stamp\StampInterface;
17+
18+
/**
19+
* An envelope stamp with context which related to a message.
20+
*
21+
* @experimental
22+
*
23+
* @author Sergii Pavlenko <[email protected]>
24+
*/
25+
final class ContextStamp implements StampInterface
26+
{
27+
private $context;
28+
29+
public function __construct(array $context = [])
30+
{
31+
$this->context = $context;
32+
}
33+
34+
/**
35+
* Get the context related to a message.
36+
*/
37+
public function getContext(): array
38+
{
39+
return $this->context;
40+
}
41+
}

src/Bridge/Symfony/Messenger/DataPersister.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public function supports($data, array $context = []): bool
7575
*/
7676
public function persist($data, array $context = [])
7777
{
78-
$envelope = $this->dispatch($data);
78+
$envelope = $this->dispatch(
79+
(new Envelope($data))
80+
->with(new ContextStamp($context))
81+
);
7982

8083
$handledStamp = $envelope->last(HandledStamp::class);
8184
if (!$handledStamp instanceof HandledStamp) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Messenger;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Messenger\Stamp\StampInterface;
19+
20+
/**
21+
* @author Sergii Pavlenko <[email protected]>
22+
*/
23+
class ContextStampTest extends TestCase
24+
{
25+
public function testConstruct()
26+
{
27+
$this->assertInstanceOf(StampInterface::class, new ContextStamp());
28+
}
29+
30+
public function testGetContext()
31+
{
32+
$contextStamp = new ContextStamp();
33+
$this->assertIsArray($contextStamp->getContext());
34+
}
35+
}

tests/Bridge/Symfony/Messenger/DataPersisterTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

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

16+
use ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp;
1617
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister;
1718
use ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp;
1819
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
@@ -56,7 +57,9 @@ public function testPersist()
5657
$dummy = new Dummy();
5758

5859
$messageBus = $this->prophesize(MessageBusInterface::class);
59-
$messageBus->dispatch($dummy)->willReturn(new Envelope($dummy))->shouldBeCalled();
60+
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
61+
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
62+
}))->willReturn(new Envelope($dummy))->shouldBeCalled();
6063

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

6972
$messageBus = $this->prophesize(MessageBusInterface::class);
73+
7074
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
7175
return $dummy === $envelope->getMessage() && null !== $envelope->last(RemoveStamp::class);
7276
}))->willReturn(new Envelope($dummy))->shouldBeCalled();
@@ -80,7 +84,9 @@ public function testHandle()
8084
$dummy = new Dummy();
8185

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

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

0 commit comments

Comments
 (0)