-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaggageSchemaMiddlewareTest.php
More file actions
94 lines (79 loc) · 3.36 KB
/
Copy pathBaggageSchemaMiddlewareTest.php
File metadata and controls
94 lines (79 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
namespace Macpaw\SchemaContextBundle\Tests\Messenger\Middleware;
use Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware;
use Macpaw\SchemaContextBundle\Messenger\Stamp\BaggageSchemaStamp;
use Macpaw\SchemaContextBundle\Service\BaggageCodec;
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
class BaggageSchemaMiddlewareTest extends TestCase
{
public function testSchemaIsSetFromStamp(): void
{
$schema = 'tenant1';
$rawBaggage = 'X-Schema=tenant1';
$baggage = [
'X-Schema' => 'tenant1',
];
$resolver = new BaggageSchemaResolver();
$baggageCodec = new BaggageCodec();
$middleware = new BaggageSchemaMiddleware($resolver, $baggageCodec);
$stamp = new BaggageSchemaStamp($schema, $rawBaggage);
$envelope = (new Envelope(new \stdClass()))->with($stamp);
$stack = $this->createMock(StackInterface::class);
$nextMiddleware = new class implements MiddlewareInterface {
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
/** @var BaggageSchemaStamp|null $stamp */
$stamp = $envelope->last(BaggageSchemaStamp::class);
return new Envelope((object) [
'schema' => $stamp?->schema,
'baggage' => $stamp?->baggage,
]);
}
};
$stack->expects($this->once())
->method('next')
->willReturn($nextMiddleware);
$envelope = $middleware->handle($envelope, $stack);
$result = (array) $envelope->getMessage();
$this->assertSame($schema, $result['schema']);
$this->assertSame($baggage, $baggageCodec->decode($result['baggage']));
$this->assertNull($resolver->getSchema());
$this->assertNull($resolver->getBaggage());
}
public function testSchemaStampIsInjectedIfMissing(): void
{
$schema = 'tenant1';
$rawBaggage = 'X-Schema=tenant1';
$baggage = [
'X-Schema' => 'tenant1',
];
$resolver = new BaggageSchemaResolver();
$resolver
->setSchema($schema)
->setBaggage($baggage);
$baggageCodec = new BaggageCodec();
$middleware = new BaggageSchemaMiddleware($resolver, $baggageCodec);
$originalEnvelope = new Envelope(new \stdClass());
$stack = $this->createMock(StackInterface::class);
$stack->expects($this->once())
->method('next')
->willReturnCallback(function () {
return new class implements MiddlewareInterface {
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
return $envelope;
}
};
});
$resultEnvelope = $middleware->handle($originalEnvelope, $stack);
$stamp = $resultEnvelope->last(BaggageSchemaStamp::class);
$this->assertInstanceOf(BaggageSchemaStamp::class, $stamp);
$this->assertSame($schema, $stamp->schema);
$this->assertSame($rawBaggage, $stamp->baggage);
}
}