1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Event\Test\Unit;
38
39use Hoa\Event as LUT;
40use Hoa\Event\Listener as SUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Event\Test\Unit\Listener.
45 *
46 * Test suite of the listener.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Listener extends Test\Unit\Suite
52{
53    public function case_constructor()
54    {
55        $this
56            ->given(
57                $source = new \Mock\Hoa\Event\Listenable(),
58                $ids    = ['foo', 'bar', 'baz']
59            )
60            ->when($result = new SUT($source, $ids))
61            ->then
62                ->object($result)
63                    ->isInstanceOf('Hoa\Event\Listener')
64                ->boolean($result->listenerExists('foo'))
65                    ->isTrue()
66                ->boolean($result->listenerExists('bar'))
67                    ->isTrue()
68                ->boolean($result->listenerExists('baz'))
69                    ->isTrue();
70    }
71
72    public function case_attach()
73    {
74        $this
75            ->given(
76                $source     = new \Mock\Hoa\Event\Listenable(),
77                $listenerId = 'foo',
78                $listener   = new SUT($source, ['foo', 'bar']),
79                $callable   = function () {
80                    return 42;
81                }
82            )
83            ->when($result = $listener->attach($listenerId, $callable))
84            ->then
85                ->object($result)
86                    ->isIdenticalTo($listener)
87                ->array($listener->fire($listenerId, new LUT\Bucket()))
88                    ->isEqualTo([42]);
89    }
90
91    public function case_attach_to_an_undefined_listener()
92    {
93        $this
94            ->given(
95                $source     = new \Mock\Hoa\Event\Listenable(),
96                $listenerId = 'bar',
97                $listener   = new SUT($source, ['foo', 'baz']),
98                $callable   = function () { }
99            )
100            ->exception(function () use ($listener, $listenerId, $callable) {
101                $listener->attach($listenerId, $callable);
102            })
103                ->isInstanceOf('Hoa\Event\Exception');
104    }
105
106    public function case_detach()
107    {
108        $this
109            ->given(
110                $source     = new \Mock\Hoa\Event\Listenable(),
111                $listenerId = 'foo',
112                $listener   = new SUT($source, ['foo', 'bar']),
113                $callable   = function () {
114                    return 42;
115                },
116                $listener->attach($listenerId, $callable)
117            )
118            ->when($result = $listener->detach($listenerId, $callable))
119            ->then
120                ->object($result)
121                    ->isIdenticalTo($listener)
122                ->array($listener->fire($listenerId, new LUT\Bucket()))
123                    ->isEmpty();
124    }
125
126    public function case_detach_an_undefined_listener()
127    {
128        $this
129            ->given(
130                $source     = new \Mock\Hoa\Event\Listenable(),
131                $listenerId = 'bar',
132                $listener   = new SUT($source, ['foo', 'baz']),
133                $callable   = function () { }
134            )
135            ->when($result = $listener->detach($listenerId, $callable))
136            ->then
137                ->object($result)
138                    ->isIdenticalTo($listener);
139    }
140
141    public function case_detach_all()
142    {
143        $this
144            ->given(
145                $source     = new \Mock\Hoa\Event\Listenable(),
146                $listenerId = 'foo',
147                $listener   = new SUT($source, ['foo', 'bar'])
148            )
149            ->when($result = $listener->detachAll($listenerId))
150            ->then
151                ->object($result)
152                    ->isIdenticalTo($listener)
153                ->boolean($listener->listenerExists($listenerId))
154                    ->isFalse();
155    }
156
157    public function case_detach_all_with_an_undefined_listener()
158    {
159        $this
160            ->given(
161                $source     = new \Mock\Hoa\Event\Listenable(),
162                $listenerId = 'bar',
163                $listener   = new SUT($source, ['foo', 'baz'])
164            )
165            ->when($result = $listener->detachAll($listenerId))
166            ->then
167                ->object($result)
168                    ->isIdenticalTo($listener);
169    }
170
171    public function case_listener_exists()
172    {
173        $this
174            ->given(
175                $source   = new \Mock\Hoa\Event\Listenable(),
176                $ids      = [],
177                $listener = new SUT($source, $ids)
178            )
179            ->when($listener->addIds(['foo']))
180            ->then
181                ->boolean($listener->listenerExists('foo'))
182                    ->isTrue()
183                ->boolean($listener->listenerExists('bar'))
184                    ->isFalse()
185
186            ->when($listener->addIds(['bar']))
187            ->then
188                ->boolean($listener->listenerExists('bar'))
189                    ->isTrue();
190    }
191
192    public function case_fire()
193    {
194        $self = $this;
195
196        $this
197            ->given(
198                $source   = new \Mock\Hoa\Event\Listenable(),
199                $ids      = ['foo', 'bar'],
200                $listener = new SUT($source, $ids),
201
202                $listenerId = 'foo',
203                $bucket     = new LUT\Bucket(),
204                $listener->attach(
205                    $listenerId,
206                    function (LUT\Bucket $receivedBucket) use ($self, $bucket, $source, &$called) {
207                        $called = true;
208
209                        $self
210                            ->object($receivedBucket)
211                                ->isIdenticalTo($bucket)
212                            ->object($receivedBucket->getSource())
213                                ->isIdenticalTo($source);
214
215                        return 42;
216                    }
217                )
218            )
219            ->when($result = $listener->fire($listenerId, $bucket))
220            ->then
221                ->array($result)
222                    ->isEqualTo([42])
223                ->boolean($called)
224                    ->isTrue();
225    }
226
227    public function case_fire_an_undefined_listenerId()
228    {
229        $this
230            ->given(
231                $source   = new \Mock\Hoa\Event\Listenable(),
232                $ids      = [],
233                $listener = new SUT($source, $ids)
234            )
235            ->exception(function () use ($listener) {
236                $listener->fire('foo', new LUT\Bucket());
237            })
238                ->isInstanceOf('Hoa\Event\Exception');
239    }
240}
241