test_will_not_route_unknown_class_name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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