1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\HandlerMapping; |
5
|
|
|
|
6
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing; |
7
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
8
|
|
|
use League\Tactician\Bundle\Tests\Fake\OtherFakeCommand; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class RoutingTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function test_routing_command_to_specific_bus() |
14
|
|
|
{ |
15
|
|
|
$routing = new Routing(['bus1', 'bus2']); |
16
|
|
|
$routing->routeToBus('bus1', FakeCommand::class, 'some.handler.1'); |
17
|
|
|
$routing->routeToBus('bus2', OtherFakeCommand::class, 'some.handler.2'); |
18
|
|
|
|
19
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler.1'], $routing->commandToServiceMapping('bus1')); |
20
|
|
|
$this->assertEquals([OtherFakeCommand::class => 'some.handler.2'], $routing->commandToServiceMapping('bus2')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function test_routing_to_all_buses() |
24
|
|
|
{ |
25
|
|
|
$routing = new Routing(['bus1', 'bus2']); |
26
|
|
|
$routing->routeToAllBuses(FakeCommand::class, 'some.handler'); |
27
|
|
|
|
28
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('bus1')); |
29
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('bus2')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function test_mixture_of_broadcast_and_specific_routing_commands() |
33
|
|
|
{ |
34
|
|
|
$routing = new Routing(['bus1', 'bus2']); |
35
|
|
|
$routing->routeToAllBuses(FakeCommand::class, 'very.broad.handler'); |
36
|
|
|
$routing->routeToBus('bus1', OtherFakeCommand::class, 'some.specific.handler'); |
37
|
|
|
|
38
|
|
|
$this->assertEquals( |
39
|
|
|
[FakeCommand::class => 'very.broad.handler', OtherFakeCommand::class => 'some.specific.handler'], |
40
|
|
|
$routing->commandToServiceMapping('bus1') |
41
|
|
|
); |
42
|
|
|
$this->assertEquals([FakeCommand::class => 'very.broad.handler'], $routing->commandToServiceMapping('bus2')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException \League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId |
47
|
|
|
* @expectedExceptionMessage Could not find a command bus with id 'fake_bus'. Valid buses are: default |
48
|
|
|
*/ |
49
|
|
|
public function test_can_not_get_mapping_for_unknown_bus() |
50
|
|
|
{ |
51
|
|
|
$routing = new Routing(['default']); |
52
|
|
|
$routing->commandToServiceMapping('fake_bus'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
57
|
|
|
* @expectedExceptionMessage Can not route Legit\Class to some.handler.service, class Legit\Class does not exist! |
58
|
|
|
*/ |
59
|
|
|
public function test_will_not_route_unknown_class_name() |
60
|
|
|
{ |
61
|
|
|
$routing = new Routing(['default']); |
62
|
|
|
$routing->routeToBus('default', 'Legit\Class', 'some.handler.service'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @expectedException \League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId |
67
|
|
|
* @expectedExceptionMessage Could not find a command bus with id 'bus3'. Valid buses are: bus1, bus2 |
68
|
|
|
*/ |
69
|
|
|
public function test_will_not_accept_command_on_invalid_bus_id() |
70
|
|
|
{ |
71
|
|
|
$routing = new Routing(['bus1', 'bus2']); |
72
|
|
|
$routing->routeToBus('bus3', FakeCommand::class, 'some.handler.service'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|