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

CompositeDispatcherTest::testPropagationStops()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 31
rs 9.552
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