forked from phly/phly-event-dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyListenerTest.php
78 lines (61 loc) · 2.89 KB
/
LazyListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
* @copyright Copyright (c) 2018-2019 Matthew Weier O'Phinney (https:/mwop.net)
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace PhlyTest\EventDispatcher;
use Phly\EventDispatcher\Exception\InvalidListenerException;
use Phly\EventDispatcher\LazyListener;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Container\ContainerInterface;
class LazyListenerTest extends TestCase
{
use ProphecyTrait;
public function setUp(): void
{
$this->event = new TestAsset\TestEvent();
$this->container = $this->prophesize(ContainerInterface::class);
}
public function testRaisesExceptionIfServiceReturnedIsNeitherAnObjectNorCallable(): void
{
$this->container->get('LazyService')->willReturn('not-callable');
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService');
$this->expectException(InvalidListenerException::class);
$lazyListener($this->event);
}
public function testInvokesNonObjectCallableListenerReturnedByContainer(): void
{
$this->container->get('LazyService')->willReturn(__NAMESPACE__ . '\TestAsset\listenerFunction');
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService');
$this->assertNull($lazyListener($this->event));
}
public function testRaisesExceptionIfObjectServiceReturnedIsNotCallableAndNoMethodProvided(): void
{
$this->container->get('LazyService')->willReturn((object) []);
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService');
$this->expectException(InvalidListenerException::class);
$lazyListener($this->event);
}
public function testInvokesCallableListenerReturnedByContainerWhenNoMethodProvided(): void
{
$this->container->get('LazyService')->willReturn(new TestAsset\Listener());
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService');
$this->assertNull($lazyListener($this->event));
}
public function testRaisesExceptionIfObjectServiceReturnedIsNotCallableViaMethodProvided(): void
{
$this->container->get('LazyService')->willReturn(new TestAsset\Listener());
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService', 'not-a-real-method');
$this->expectException(InvalidListenerException::class);
$lazyListener($this->event);
}
public function testInvokesMethodOnObjectListenerReturnedByContainer(): void
{
$this->container->get('LazyService')->willReturn(new TestAsset\Listener());
$lazyListener = new LazyListener($this->container->reveal(), 'LazyService', 'onTest');
$this->assertNull($lazyListener($this->event));
}
}