BasicCommandAndBusMappingTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 123
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandleCommandOnDefaultBus() 0 9 1
A testHandleCommandWithInvalidMiddleware() 0 14 1
A testHandleCommandOnMiddlewareWithDependencies() 0 22 1
A testHandleCommandOnSpecificBus() 0 18 1
A testHandlerOnUnknownBus() 0 17 1
A testInvalidDefaultBus() 0 15 1
A testHandleCommandSpecifiedOnAnotherBus() 0 19 1
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\Integration;
4
5
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId;
6
use League\Tactician\Exception\MissingHandlerException;
7
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
9
10
/**
11
 * @runTestsInSeparateProcesses
12
 */
13
class BasicCommandAndBusMappingTest extends IntegrationTest
14
{
15
    public function testHandleCommandOnDefaultBus()
16
    {
17
        $this->registerService('tactician.test.handler', \League\Tactician\Bundle\Tests\EchoTextHandler::class, [
18
            ['name' => 'tactician.handler', 'command' => 'League\Tactician\Bundle\Tests\EchoText'],
19
        ]);
20
21
        $this->expectOutputString('Hello world');
22
        $this->handleCommand('default', \League\Tactician\Bundle\Tests\EchoText::class, ['Hello world']);
23
    }
24
25
    public function testHandleCommandWithInvalidMiddleware()
26
    {
27
        $this->expectException(ServiceNotFoundException::class);
28
29
        $this->givenConfig('tactician', <<<'EOF'
30
commandbus:
31
    default:
32
        middleware:
33
            - tactician.middleware.whatever
34
            - tactician.middleware.command_handler
35
EOF
36
        );
37
        static::$kernel->boot();
38
    }
39
40
    public function testHandleCommandOnMiddlewareWithDependencies()
41
    {
42
        $this->givenConfig('framework', <<<'EOF'
43
validation:
44
    enabled: true
45
EOF
46
        );
47
        $this->givenConfig('tactician', <<<'EOF'
48
commandbus:
49
    default:
50
        middleware:
51
            - tactician.middleware.validator
52
            - tactician.middleware.command_handler
53
EOF
54
        );
55
        $this->registerService('tactician.test.handler', \League\Tactician\Bundle\Tests\EchoTextHandler::class, [
56
            ['name' => 'tactician.handler', 'command' => 'League\Tactician\Bundle\Tests\EchoText'],
57
        ]);
58
59
        $this->expectOutputString('Hello world');
60
        $this->handleCommand('default', \League\Tactician\Bundle\Tests\EchoText::class, ['Hello world']);
61
    }
62
63
    public function testHandleCommandOnSpecificBus()
64
    {
65
        $this->givenConfig('tactician', <<<'EOF'
66
commandbus:
67
    default:
68
        middleware:
69
            - tactician.middleware.command_handler
70
    other:
71
        middleware:
72
            - tactician.commandbus.other.middleware.command_handler
73
EOF
74
        );
75
        $this->registerService('tactician.test.handler', \League\Tactician\Bundle\Tests\EchoTextHandler::class, [
76
            ['name' => 'tactician.handler', 'command' => 'League\Tactician\Bundle\Tests\EchoText', 'bus' => 'other'],
77
        ]);
78
        $this->expectOutputString('Welcome');
79
        $this->handleCommand('other', \League\Tactician\Bundle\Tests\EchoText::class, ['Welcome']);
80
    }
81
82
     public function testHandlerOnUnknownBus()
83
    {
84
        $this->expectException(InvalidCommandBusId::class);
85
        $this->expectExceptionMessage('Could not find a command bus with id \'other\'. Valid buses are: default');
86
87
        $this->givenConfig('tactician', <<<'EOF'
88
commandbus:
89
    default:
90
        middleware:
91
            - tactician.middleware.command_handler
92
EOF
93
        );
94
        $this->registerService('tactician.test.handler', \League\Tactician\Bundle\Tests\EchoTextHandler::class, [
95
            ['name' => 'tactician.handler', 'command' => 'League\Tactician\Bundle\Tests\EchoText', 'bus' => 'other'],
96
        ]);
97
        static::$kernel->boot();
98
    }
99
100
    public function testInvalidDefaultBus()
101
    {
102
        $this->expectException(InvalidConfigurationException::class);
103
104
        $this->givenConfig('tactician', <<<'EOF'
105
default_bus: some_bus_that_does_not_exist
106
commandbus:
107
    default:
108
        middleware:
109
            - tactician.middleware.command_handler
110
EOF
111
        );
112
113
        static::$kernel->boot();
114
    }
115
116
    public function testHandleCommandSpecifiedOnAnotherBus()
117
    {
118
        $this->expectException(MissingHandlerException::class);
119
120
        $this->givenConfig('tactician', <<<'EOF'
121
commandbus:
122
    default:
123
        middleware:
124
            - tactician.middleware.command_handler
125
    other:
126
        middleware:
127
            - tactician.commandbus.other.middleware.command_handler
128
EOF
129
        );
130
        $this->registerService('tactician.test.handler', \League\Tactician\Bundle\Tests\EchoTextHandler::class, [
131
            ['name' => 'tactician.handler', 'command' => 'League\Tactician\Bundle\Tests\EchoText', 'bus' => 'other'],
132
        ]);
133
        $this->handleCommand('default', \League\Tactician\Bundle\Tests\EchoText::class, ['Welcome']);
134
    }
135
}
136