1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/event package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace spec\Slick\Event\Application; |
11
|
|
|
|
12
|
|
|
use Prophecy\Exception\Prediction\FailedPredictionException; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Slick\Event\Application\ListenerClassFile; |
15
|
|
|
use PhpSpec\ObjectBehavior; |
16
|
|
|
use Slick\Event\Event; |
17
|
|
|
use Slick\Event\Test\Application\Listener\ClassListener; |
18
|
|
|
use Slick\Event\Test\Application\Listener\InvokerListener; |
19
|
|
|
use Slick\Event\Test\Application\Listener\SimpleMethodListener; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ListenerClassFileSpec specs |
23
|
|
|
* |
24
|
|
|
* @package spec\Slick\Event\Application |
25
|
|
|
*/ |
26
|
|
|
class ListenerClassFileSpec extends ObjectBehavior |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
public function let(ContainerInterface $container) |
30
|
|
|
{ |
31
|
|
|
$this->beConstructedWith(__DIR__. '/Listener/ClassListener.php', $container); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function it_is_initializable() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->shouldHaveType(ListenerClassFile::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function it_have_a_listener_checker() |
40
|
|
|
{ |
41
|
|
|
$this->isListener()->shouldBe(true); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function is_has_a_class_name() |
45
|
|
|
{ |
46
|
|
|
$this->className()->shouldBe(ClassListener::class); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function it_creates_the_listener_instance(ContainerInterface $container) |
50
|
|
|
{ |
51
|
|
|
$listener = new ClassListener(); |
52
|
|
|
$container->get(ClassListener::class)->shouldBeCalled()->willReturn($listener); |
53
|
|
|
$this->resolveListeners()[0]['listener']->shouldBe($listener); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function it_can_run_class_with_invoke_method(ContainerInterface $container, Event $event) |
57
|
|
|
{ |
58
|
|
|
$this->beConstructedWith(__DIR__. '/Listener/InvokerListener.php', $container); |
59
|
|
|
$container->get(InvokerListener::class)->shouldBeCalled()->willReturn(new InvokerListener()); |
60
|
|
|
$this->resolveListeners()[0]['listener']->handle($event)->shouldBe($event); |
61
|
|
|
$this->resolveListeners()[1]['listener']->handle($event)->shouldBe($event); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function it_can_run_a_method_as_listener(ContainerInterface $container, Event $event) |
65
|
|
|
{ |
66
|
|
|
$this->beConstructedWith(__DIR__. '/Listener/SimpleMethodListener.php', $container); |
67
|
|
|
$container->get(SimpleMethodListener::class)->shouldBeCalled()->willReturn(new SimpleMethodListener()); |
68
|
|
|
$this->resolveListeners()[0]['listener']->handle($event)->shouldBe($event); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function it_has_bindings_information() |
72
|
|
|
{ |
73
|
|
|
$this->bindings()->shouldBe([ |
|
|
|
|
74
|
|
|
[ |
75
|
|
|
'event' => 'test', |
76
|
|
|
'method' => 'handle', |
77
|
|
|
'priority' => 10, |
78
|
|
|
'source' => 'interface' |
79
|
|
|
] |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function it_has_a_file_path() |
84
|
|
|
{ |
85
|
|
|
$this->filePath()->shouldBe(__DIR__. '/Listener/ClassListener.php'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function it_can_be_serialized(ContainerInterface $container) |
89
|
|
|
{ |
90
|
|
|
$listener = new ClassListener(); |
91
|
|
|
$container->get(ClassListener::class)->shouldBeCalled()->willReturn($listener); |
92
|
|
|
$serData = serialize($this->getWrappedObject()); |
93
|
|
|
/** @var ListenerClassFile $unserialized */ |
94
|
|
|
$unserialized = unserialize($serData); |
95
|
|
|
$unserialized->withContainer($container->getWrappedObject()); |
|
|
|
|
96
|
|
|
if (!$unserialized->isListener()) { |
97
|
|
|
throw new FailedPredictionException("Error unserializing listener"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$listenerResult = $unserialized->resolveListeners()[0]['listener']; |
101
|
|
|
if ($listenerResult !== $listener) { |
102
|
|
|
throw new FailedPredictionException( |
103
|
|
|
"Error unserializing listener: couldn't retrieve the listener instance." |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.