1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\HandlerMapping; |
5
|
|
|
|
6
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\ClassNameMapping; |
7
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing; |
8
|
|
|
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId; |
9
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
10
|
|
|
use League\Tactician\Bundle\Tests\Fake\OtherFakeCommand; |
11
|
|
|
use League\Tactician\Bundle\Tests\Fake\SomeHandler; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
|
16
|
|
|
final class ClassNameMappingTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function test_will_skip_definitions_without_command_tag() |
19
|
|
|
{ |
20
|
|
|
$builder = new ContainerBuilder(); |
21
|
|
|
$builder |
22
|
|
|
->setDefinition('some.handler', new Definition(SomeHandler::class)) |
23
|
|
|
->addTag('tactician.handler', ['foo' => 'bar']); |
24
|
|
|
|
25
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['default'])); |
26
|
|
|
|
27
|
|
|
$this->assertEquals([], $routing->commandToServiceMapping('default')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_will_resolve_parameters_in_command_attribute() |
31
|
|
|
{ |
32
|
|
|
$builder = new ContainerBuilder(); |
33
|
|
|
$builder->setParameter('fake_command_class', FakeCommand::class); |
34
|
|
|
$builder |
35
|
|
|
->setDefinition('some.handler', new Definition(SomeHandler::class)) |
36
|
|
|
->addTag('tactician.handler', ['command' => '%fake_command_class%']); |
37
|
|
|
|
38
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['default'])); |
39
|
|
|
|
40
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_will_find_handler_for_defined_command() |
44
|
|
|
{ |
45
|
|
|
$builder = new ContainerBuilder(); |
46
|
|
|
$builder |
47
|
|
|
->setDefinition('some.handler', new Definition(SomeHandler::class)) |
48
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class]); |
49
|
|
|
|
50
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['default'])); |
51
|
|
|
|
52
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_can_bind_to_specific_bus() |
56
|
|
|
{ |
57
|
|
|
$builder = new ContainerBuilder(); |
58
|
|
|
$builder |
59
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
60
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.a']); |
61
|
|
|
|
62
|
|
|
$builder |
63
|
|
|
->setDefinition('second.handler', new Definition(SomeHandler::class)) |
64
|
|
|
->addTag('tactician.handler', ['command' => OtherFakeCommand::class, 'bus' => 'bus.b']); |
65
|
|
|
|
66
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
67
|
|
|
|
68
|
|
|
$this->assertEquals( |
69
|
|
|
[ |
70
|
|
|
FakeCommand::class => 'first.handler', |
71
|
|
|
], |
72
|
|
|
$routing->commandToServiceMapping('bus.a') |
73
|
|
|
); |
74
|
|
|
$this->assertEquals( |
75
|
|
|
[OtherFakeCommand::class => 'second.handler'], |
76
|
|
|
$routing->commandToServiceMapping('bus.b') |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function test_can_bind_to_multiple_buses() |
81
|
|
|
{ |
82
|
|
|
$builder = new ContainerBuilder(); |
83
|
|
|
$builder |
84
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
85
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.a']) |
86
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.b']); |
87
|
|
|
|
88
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
89
|
|
|
|
90
|
|
|
$this->assertEquals([FakeCommand::class => 'first.handler'], $routing->commandToServiceMapping('bus.a')); |
91
|
|
|
$this->assertEquals([FakeCommand::class => 'first.handler'], $routing->commandToServiceMapping('bus.b')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function test_will_error_when_given_invalid_bus() |
95
|
|
|
{ |
96
|
|
|
$this->expectException(InvalidCommandBusId::class); |
97
|
|
|
|
98
|
|
|
$builder = new ContainerBuilder(); |
99
|
|
|
$builder |
100
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
101
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.does.not.exist.mwhahahaha']); |
102
|
|
|
|
103
|
|
|
(new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|