1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass; |
6
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\ClassNameMapping; |
7
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\HandlerMapping; |
8
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing; |
9
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
10
|
|
|
use League\Tactician\Bundle\Tests\Fake\OtherFakeCommand; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Prophecy\Argument; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
|
15
|
|
|
class CommandHandlerPassTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var HandlerMapping |
19
|
|
|
*/ |
20
|
|
|
private $mappingStrategy; |
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->mappingStrategy = new ClassNameMapping(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAddingSingleDefaultBus() |
28
|
|
|
{ |
29
|
|
|
$container = $this->containerWithConfig( |
30
|
|
|
[ |
31
|
|
|
'commandbus' => |
32
|
|
|
[ |
33
|
|
|
'default' => ['middleware' => []], |
34
|
|
|
] |
35
|
|
|
] |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
(new CommandHandlerPass($this->mappingStrategy))->process($container); |
39
|
|
|
|
40
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.default')); |
41
|
|
|
|
42
|
|
|
$this->assertDefaultAliasesAreDeclared($container, 'default'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses() |
46
|
|
|
{ |
47
|
|
|
$container = $this->containerWithConfig( |
48
|
|
|
[ |
49
|
|
|
'default_bus' => 'custom_bus', |
50
|
|
|
'commandbus' => |
51
|
|
|
[ |
52
|
|
|
'default' => ['middleware' => ['one']], |
53
|
|
|
'custom_bus' => ['middleware' => ['two']], |
54
|
|
|
'other_bus' => ['middleware' => ['three']] |
55
|
|
|
] |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
(new CommandHandlerPass($this->mappingStrategy))->process($container); |
60
|
|
|
|
61
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.default')); |
62
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.custom_bus')); |
63
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.other_bus')); |
64
|
|
|
|
65
|
|
|
$this->assertDefaultAliasesAreDeclared($container, 'custom_bus'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test_handler_mapping_is_called() |
69
|
|
|
{ |
70
|
|
|
$container = $this->containerWithConfig( |
71
|
|
|
[ |
72
|
|
|
'commandbus' => [ 'default' => ['middleware' => []] ] |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$routing = new Routing(['default']); |
77
|
|
|
$routing->routeToAllBuses(FakeCommand::class, 'some.handler'); |
78
|
|
|
|
79
|
|
|
$mapping = $this->prophesize(HandlerMapping::class); |
80
|
|
|
$mapping->build($container, Argument::type(Routing::class))->willReturn($routing); |
81
|
|
|
|
82
|
|
|
(new CommandHandlerPass($mapping->reveal()))->process($container); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( |
85
|
|
|
[FakeCommand::class => 'some.handler'], |
86
|
|
|
$container->getDefinition('tactician.commandbus.default.handler.locator')->getArgument(1) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test_handler_mapping_is_kept_bus_specific() |
91
|
|
|
{ |
92
|
|
|
$container = $this->containerWithConfig( |
93
|
|
|
[ |
94
|
|
|
'default_bus' => 'bus.a', |
95
|
|
|
'commandbus' => [ |
96
|
|
|
'bus.a' => ['middleware' => []], |
97
|
|
|
'bus.b' => ['middleware' => []] |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$routing = new Routing(['bus.a', 'bus.b']); |
103
|
|
|
$routing->routeToBus('bus.a', FakeCommand::class, 'some.handler.a'); |
104
|
|
|
$routing->routeToBus('bus.b', FakeCommand::class, 'some.handler.b'); |
105
|
|
|
$routing->routeToAllBuses(OtherFakeCommand::class, 'some.other.handler'); |
106
|
|
|
|
107
|
|
|
$mapping = $this->prophesize(HandlerMapping::class); |
108
|
|
|
$mapping->build($container, Argument::type(Routing::class))->willReturn($routing); |
109
|
|
|
|
110
|
|
|
(new CommandHandlerPass($mapping->reveal()))->process($container); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( |
113
|
|
|
[FakeCommand::class => 'some.handler.a', OtherFakeCommand::class => 'some.other.handler'], |
114
|
|
|
$container->getDefinition('tactician.commandbus.bus.a.handler.locator')->getArgument(1) |
115
|
|
|
); |
116
|
|
|
$this->assertEquals( |
117
|
|
|
[FakeCommand::class => 'some.handler.b', OtherFakeCommand::class => 'some.other.handler'], |
118
|
|
|
$container->getDefinition('tactician.commandbus.bus.b.handler.locator')->getArgument(1) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function containerWithConfig($config) |
123
|
|
|
{ |
124
|
|
|
$container = new ContainerBuilder(); |
125
|
|
|
|
126
|
|
|
$container->prependExtensionConfig('tactician', $config); |
127
|
|
|
|
128
|
|
|
return $container; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $container |
133
|
|
|
*/ |
134
|
|
|
protected function assertDefaultAliasesAreDeclared(ContainerBuilder $container, string $defaultBusId) |
135
|
|
|
{ |
136
|
|
|
$this->assertSame( |
137
|
|
|
$container->findDefinition('tactician.commandbus'), |
138
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId") |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$this->assertSame( |
142
|
|
|
$container->findDefinition('tactician.handler.locator.symfony'), |
143
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId.handler.locator") |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$this->assertSame( |
147
|
|
|
$container->findDefinition('tactician.middleware.command_handler'), |
148
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId.middleware.command_handler") |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|