|
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_find_handler_for_defined_command() |
|
31
|
|
|
{ |
|
32
|
|
|
$builder = new ContainerBuilder(); |
|
33
|
|
|
$builder |
|
34
|
|
|
->setDefinition('some.handler', new Definition(SomeHandler::class)) |
|
35
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class]); |
|
36
|
|
|
|
|
37
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['default'])); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_can_bind_to_specific_bus() |
|
43
|
|
|
{ |
|
44
|
|
|
$builder = new ContainerBuilder(); |
|
45
|
|
|
$builder |
|
46
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
|
47
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.a']); |
|
48
|
|
|
|
|
49
|
|
|
$builder |
|
50
|
|
|
->setDefinition('second.handler', new Definition(SomeHandler::class)) |
|
51
|
|
|
->addTag('tactician.handler', ['command' => OtherFakeCommand::class, 'bus' => 'bus.b']); |
|
52
|
|
|
|
|
53
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals( |
|
56
|
|
|
[ |
|
57
|
|
|
FakeCommand::class => 'first.handler', |
|
58
|
|
|
], |
|
59
|
|
|
$routing->commandToServiceMapping('bus.a') |
|
60
|
|
|
); |
|
61
|
|
|
$this->assertEquals( |
|
62
|
|
|
[OtherFakeCommand::class => 'second.handler'], |
|
63
|
|
|
$routing->commandToServiceMapping('bus.b') |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_can_bind_to_multiple_buses() |
|
68
|
|
|
{ |
|
69
|
|
|
$builder = new ContainerBuilder(); |
|
70
|
|
|
$builder |
|
71
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
|
72
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.a']) |
|
73
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.b']); |
|
74
|
|
|
|
|
75
|
|
|
$routing = (new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals([FakeCommand::class => 'first.handler'], $routing->commandToServiceMapping('bus.a')); |
|
78
|
|
|
$this->assertEquals([FakeCommand::class => 'first.handler'], $routing->commandToServiceMapping('bus.b')); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function test_will_error_when_given_invalid_bus() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->expectException(InvalidCommandBusId::class); |
|
84
|
|
|
|
|
85
|
|
|
$builder = new ContainerBuilder(); |
|
86
|
|
|
$builder |
|
87
|
|
|
->setDefinition('first.handler', new Definition(SomeHandler::class)) |
|
88
|
|
|
->addTag('tactician.handler', ['command' => FakeCommand::class, 'bus' => 'bus.does.not.exist.mwhahahaha']); |
|
89
|
|
|
|
|
90
|
|
|
(new ClassNameMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|