CommandBusTest::testHandleCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCommandBus\Tests\Unit;
6
7
use Psr\EventDispatcher;
8
use RemotelyLiving\PHPCommandBus\Interfaces;
9
use RemotelyLiving\PHPCommandBus\CommandBus;
10
11
class CommandBusTest extends AbstractTestCase
12
{
13
    /**
14
     * @var \PHPUnit\Framework\MockObject\MockObject|\RemotelyLiving\PHPCommandBus\Interfaces\Command
15
     */
16
    private $command;
17
18
    /**
19
     * @var \PHPUnit\Framework\MockObject\MockObject|\RemotelyLiving\PHPCommandBus\Interfaces\Handler
20
     */
21
    private $handler;
22
23
    /**
24
     * @var \PHPUnit\Framework\MockObject\MockObject|\RemotelyLiving\PHPCommandBus\Interfaces\Resolver
25
     */
26
    private $resolver;
27
28
    /**
29
     * @var \PHPUnit\Framework\MockObject\MockObject|\Psr\EventDispatcher\EventDispatcherInterface
30
     */
31
    private $dispatcher;
32
33
    private CommandBus $bus;
34
35
    protected function setUp(): void
36
    {
37
        $this->command = $this->createMock(\stdClass::class);
38
        $this->handler = $this->createMock(Interfaces\Handler::class);
39
        $this->resolver = $this->createMock(Interfaces\Resolver::class);
40
        $this->dispatcher = $this->createMock(EventDispatcher\EventDispatcherInterface::class);
41
        $this->resolver->method('resolve')
42
            ->with($this->command)
43
            ->willReturn($this->handler);
44
45
        $this->bus = CommandBus::create($this->resolver, $this->dispatcher);
46
    }
47
48
    public function testHandleCommand(): void
49
    {
50
        $this->handler->expects($this->once())
51
            ->method('handle')
52
            ->with($this->command);
53
54
        $this->bus->handle($this->command);
55
    }
56
57
    public function testPushesMiddlewareLIFO(): void
58
    {
59
        $calledMiddleware = [];
60
61
        $middleware1 = function (object $command, callable $next) use (&$calledMiddleware) {
62
            $calledMiddleware[] = 'middleware1';
63
            $next($command);
64
        };
65
66
        $middleware2 = function (object $command, callable $next) use (&$calledMiddleware) {
67
            $calledMiddleware[] = 'middleware2';
68
            $next($command);
69
        };
70
71
        $middleware3 = function (object $command, callable $next) use (&$calledMiddleware) {
72
            $calledMiddleware[] = 'middleware3';
73
            $next($command);
74
        };
75
76
        $this->bus->pushMiddleware($middleware2)
77
            ->pushMiddleware($middleware3)
78
            ->pushMiddleware($middleware1);
79
80
        $this->bus->handle($this->command);
81
82
        $this->assertSame(['middleware1', 'middleware3', 'middleware2'], $calledMiddleware);
83
    }
84
85
86
    public function testNotDispatchEventsIfNoneReturnedFromHandler(): void
87
    {
88
        $this->dispatcher->expects($this->never())
89
            ->method('dispatch');
90
91
        $this->bus->handle($this->command);
92
    }
93
94
    public function testDispatchesEventsIfReturnedFromHandler(): void
95
    {
96
        $event1 = new class {
97
        };
98
        $event2 = new class {
99
        };
100
101
        $this->handler->method('handle')
0 ignored issues
show
Bug introduced by
The method method() does not exist on RemotelyLiving\PHPCommandBus\Interfaces\Handler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        $this->handler->/** @scrutinizer ignore-call */ 
102
                        method('handle')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            ->with($this->command)
103
            ->willReturn([$event1, $event2]);
104
105
        $this->dispatcher->expects($this->exactly(2))
106
            ->method('dispatch')
107
            ->withConsecutive([$event1], [$event2]);
108
109
        $this->bus->handle($this->command);
110
    }
111
}
112