Completed
Pull Request — master (#48)
by Robin
02:38
created

CommandHandlerPassTest::testProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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\TacticianExtension;
7
use League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator;
8
use League\Tactician\Container\ContainerLocator;
9
use Mockery\MockInterface;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\ServiceLocator;
14
15
class CommandHandlerPassTest extends TestCase
16
{
17
    public function testProcess()
18
    {
19
        $container = $this->getContainer(['default'], 'default');
20
        $container->register('handler_1')->addTag('tactician.handler', ['command' => 'my_command']);
21
        $container->register('handler_2')->addTag('tactician.handler', ['command' => 'my_command']);
22
23
        (new CommandHandlerPass())->process($container);
24
25
        $this->busShouldBeCorrectlyRegisteredInContainer(
26
            $container,
27
            'default',
28
            'tactician.handler.method_name_inflector.handle'
29
        );
30
    }
31
32
    /**
33
     * @expectedException        \Exception
34
     * @expectedExceptionMessage The tactician.handler tag must always have a command attribute
35
     */
36
    public function testProcessAbortsOnMissingCommandAttribute()
37
    {
38
        $container = $this->getContainer(['default'], 'default');
39
        $container->register('handler_1')->addTag('tactician.handler', ['command' => 'my_command']);
40
        $container->register('handler_2')->addTag('tactician.handler', ['not_command' => 'my_command']);
41
42
        (new CommandHandlerPass())->process($container);
43
    }
44
45
    /**
46
     * @expectedException        \Exception
47
     * @expectedExceptionMessage Invalid bus id "bad_bus".
48
     */
49
    public function testProcessAbortsOnInvalidBus()
50
    {
51
        $container = $this->getContainer(['default'], 'default');
52
53
        $container
54
            ->register('handler_1')
55
            ->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'bad_bus']);
56
57
        (new CommandHandlerPass())->process($container);
58
    }
59
60
    public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses()
61
    {
62
        $container = $this->getContainer(['default', 'custom_bus', 'other_bus'], 'custom_bus');
63
        $container->setParameter(
64
            'tactician.method_inflector.custom_bus',
65
            'tactician.handler.method_name_inflector.handle'
66
        );
67
        $container->setParameter(
68
            'tactician.method_inflector.other_bus',
69
            'tactician.handler.method_name_inflector.handle'
70
        );
71
72
        $container
73
            ->register('handler_1')
74
            ->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'custom_bus'])
75
            ->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'other_bus']);
76
77
78
        (new CommandHandlerPass())->process($container);
79
80
        $this->busShouldBeCorrectlyRegisteredInContainer(
81
            $container,
82
            'default',
83
            'tactician.handler.method_name_inflector.handle'
84
        );
85
86
        $this->busShouldBeCorrectlyRegisteredInContainer(
87
            $container,
88
            'custom_bus',
89
            'tactician.handler.method_name_inflector.handle'
90
        );
91
92
        $this->busShouldBeCorrectlyRegisteredInContainer(
93
            $container,
94
            'other_bus',
95
            'tactician.handler.method_name_inflector.handle'
96
        );
97
    }
98
99
    public function testProcessAddsHandlerDefinitionWithNonDefaultMethodNameInflector()
100
    {
101
        $container = $this->getContainer(['default', 'custom_bus'], 'custom_bus');
102
        $container->setParameter(
103
            'tactician.method_inflector.custom_bus',
104
            'tactician.handler.method_name_inflector.handle_class_name'
105
        );
106
107
        $container
108
            ->register('handler_1')
109
            ->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'custom_bus']);
110
111
        (new CommandHandlerPass())->process($container);
112
113
        $this->busShouldBeCorrectlyRegisteredInContainer(
114
            $container,
115
            'custom_bus',
116
            'tactician.handler.method_name_inflector.handle_class_name'
117
        );
118
    }
119
120
    private function busShouldBeCorrectlyRegisteredInContainer(ContainerBuilder $container, $busId, $methodInflector)
121
    {
122
        $handlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $busId);
123
        $handlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $busId);
124
        $defaultHandlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $container->getParameter('tactician.commandbus.default'));
125
        $defaultHandlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $container->getParameter('tactician.commandbus.default'));
126
127
        if (class_exists(ServiceLocator::class)) {
128
            $this->assertSame(
129
                ServiceLocator::class,
130
                $container ->getDefinition(
131
                    sprintf('tactician.commandbus.%s.handler.service_locator', $busId)
132
                )->getClass()
133
            );
134
        }
135
136
        $this->assertSame(
137
            class_exists(ServiceLocator::class)
138
                ? ContainerLocator::class
139
                : ContainerBasedHandlerLocator::class,
140
            $container->getDefinition($handlerLocatorId)->getClass()
141
        );
142
143
        $this->assertSame(
144
            $methodInflector,
145
            (string) $container
146
                ->getDefinition($handlerId)
147
                ->getArgument(2)
148
        );
149
150
        $this->assertSame(
151
            $defaultHandlerLocatorId,
152
            (string) $container->getAlias('tactician.handler.locator.symfony')
153
        );
154
155
        $this->assertSame(
156
            $defaultHandlerId,
157
            (string) $container->getAlias('tactician.middleware.command_handler')
158
        );
159
    }
160
161
    private function getContainer(array $busIds, $defaultBus)
162
    {
163
        $container = new ContainerBuilder();
164
        $container->setParameter('tactician.commandbus.ids', $busIds);
165
        $container->setParameter('tactician.commandbus.default', $defaultBus);
166
        $container->setParameter(
167
            'tactician.method_inflector.default',
168
            'tactician.handler.method_name_inflector.handle'
169
        );
170
171
        foreach ($busIds as $busId) {
172
            $container->register('tactician.commandbus.'.$busId)->addArgument(array());
173
        }
174
175
        return $container;
176
    }
177
}
178