Passed
Push — master ( 83ac3f...15f95c )
by Alexander
11:33 queued 10:26
created

CompositeDispatcherTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testPropagationStops() 0 31 1
A testCallsAllDispatchers() 0 13 1
A createTransparentDispatcher() 0 9 1
1
<?php
2
3
namespace Yiisoft\EventDispatcher\Tests\Dispatcher;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Psr\EventDispatcher\StoppableEventInterface;
8
use Yiisoft\EventDispatcher\Dispatcher\CompositeDispatcher;
9
10
class CompositeDispatcherTest extends TestCase
11
{
12
    public function testCallsAllDispatchers(): void
13
    {
14
        $dispatcher1 = $this->createTransparentDispatcher();
15
        $dispatcher2 = $this->createTransparentDispatcher();
16
17
        $event = new \stdClass();
18
        $compositeDispatcher = new CompositeDispatcher();
19
        $compositeDispatcher->attach($dispatcher1);
20
        $compositeDispatcher->attach($dispatcher2);
21
22
        $result = $compositeDispatcher->dispatch($event);
23
24
        $this->assertSame($event, $result);
25
    }
26
27
    public function testPropagationStops(): void
28
    {
29
        $notStoppableEvent = $this->createMock(StoppableEventInterface::class);
30
        $notStoppableEvent
31
            ->method('isPropagationStopped')
32
            ->willReturn(false);
33
34
        $stoppableEvent = $this->createMock(StoppableEventInterface::class);
35
        $stoppableEvent
36
            ->method('isPropagationStopped')
37
            ->willReturn(true);
38
39
        $dispatcher1 = $this->createMock(EventDispatcherInterface::class);
40
        $dispatcher1
41
            ->expects($this->once())
42
            ->method('dispatch')
43
            ->willReturn($stoppableEvent);
44
45
        $dispatcher2 = $this->createMock(EventDispatcherInterface::class);
46
        $dispatcher2
47
            ->expects($this->never())
48
            ->method('dispatch')
49
            ->willReturnArgument(0);
50
51
        $compositeDispatcher = new CompositeDispatcher();
52
        $compositeDispatcher->attach($dispatcher1);
53
        $compositeDispatcher->attach($dispatcher2);
54
55
        $result = $compositeDispatcher->dispatch($notStoppableEvent);
56
57
        $this->assertSame($stoppableEvent, $result);
58
    }
59
60
    /**
61
     * @return \PHPUnit\Framework\MockObject\MockObject|\Psr\EventDispatcher\EventDispatcherInterface
62
     */
63
    private function createTransparentDispatcher()
64
    {
65
        $dispatcher = $this->createMock(EventDispatcherInterface::class);
66
        $dispatcher
67
            ->expects($this->once())
68
            ->method('dispatch')
69
            ->willReturnArgument(0);
70
71
        return $dispatcher;
72
    }
73
}
74