Passed
Pull Request — master (#417)
by Alejandro
06:44
created

EventListenerTaskTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toStringReturnsTheStringRepresentation() 0 5 1
A runInvokesContainerAndListenerWithEvent() 0 15 1
A setUp() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\EventDispatcher\Listener;
5
6
use PHPUnit\Framework\Assert;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerInterface;
9
use Shlinkio\Shlink\EventDispatcher\Listener\EventListenerTask;
10
use stdClass;
11
12
use function get_class;
13
use function sprintf;
14
15
class EventListenerTaskTest extends TestCase
16
{
17
    /** @var EventListenerTask */
18
    private $task;
19
    /** @var object */
20
    private $event;
21
    /** @var string */
22
    private $listenerName;
23
24
    public function setUp(): void
25
    {
26
        $this->event = new stdClass();
27
        $this->listenerName = 'the_listener';
28
29
        $this->task = new EventListenerTask($this->listenerName, $this->event);
30
    }
31
32
    /** @test */
33
    public function toStringReturnsTheStringRepresentation(): void
34
    {
35
        $this->assertEquals(
36
            sprintf('Listener -> "%s", Event -> "%s"', $this->listenerName, get_class($this->event)),
37
            $this->task->toString()
38
        );
39
    }
40
41
    /** @test */
42
    public function runInvokesContainerAndListenerWithEvent(): void
43
    {
44
        $invoked = false;
45
        $container = $this->prophesize(ContainerInterface::class);
46
        $listener = function (object $event) use (&$invoked) {
47
            $invoked = true;
48
            Assert::assertSame($event, $this->event);
49
        };
50
51
        $getListener = $container->get($this->listenerName)->willReturn($listener);
52
53
        $this->task->run($container->reveal());
54
55
        $this->assertTrue($invoked);
56
        $getListener->shouldHaveBeenCalledOnce();
57
    }
58
}
59