Skip to content

Commit 340b3ff

Browse files
committed
qa: provide tests for LazyListener
Creates LazyListenerTest to validate all behaviors of LazyListener. In the process, found an issue with `getListener()` where I was accessing an undeclared variable instead of an instance property.
1 parent a0dd26d commit 340b3ff

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/LazyListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function getListener($service) : callable
7676

7777
// Object, method present, but method is not callable: invalid
7878
if (! is_callable($callback)) {
79-
throw Exception\InvalidListenerException::forNonCallableInstanceMethod($service, $method);
79+
throw Exception\InvalidListenerException::forNonCallableInstanceMethod($service, $this->method);
8080
}
8181

8282
// Object with method as callback

test/LazyListenerTest.php

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

0 commit comments

Comments
 (0)