|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the bee4/events package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Bee4 2014 |
|
8
|
|
|
* @author Stephane HULARD <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Bee4\Events\Test\Units\Adapters; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Check behaviour of an adapter |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractAdapterTest extends \atoum |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $adapter; |
|
20
|
|
|
|
|
21
|
|
|
public function testAdapter() |
|
22
|
|
|
{ |
|
23
|
|
|
$this |
|
24
|
|
|
->given( |
|
25
|
|
|
$handler = function () { |
|
26
|
|
|
echo "Youpi!"; |
|
27
|
|
|
}, |
|
28
|
|
|
$event = new \Mock\Bee4\Events\EventInterface |
|
29
|
|
|
) |
|
30
|
|
|
->when($this->adapter->on('name', $handler)) |
|
31
|
|
|
->output(function () use ($event) { |
|
32
|
|
|
$this->adapter->dispatch('name', $event); |
|
33
|
|
|
}) |
|
34
|
|
|
->isEqualTo("Youpi!"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testOn() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$this |
|
40
|
|
|
->given($handler = function () { |
|
41
|
|
|
echo "Youpi!"; |
|
42
|
|
|
}) |
|
43
|
|
|
->then |
|
44
|
|
|
->array($this->adapter->get('name')) |
|
45
|
|
|
->hasSize(0) |
|
46
|
|
|
->when($this->adapter->on('name', $handler)) |
|
47
|
|
|
->then |
|
48
|
|
|
->array($this->adapter->get('name')) |
|
49
|
|
|
->hasSize(1); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testOnce() |
|
53
|
|
|
{ |
|
54
|
|
|
$this |
|
55
|
|
|
->given( |
|
56
|
|
|
$handler = function () { |
|
57
|
|
|
echo "Youpi!"; |
|
58
|
|
|
}, |
|
59
|
|
|
$event = new \Mock\Bee4\Events\EventInterface |
|
60
|
|
|
) |
|
61
|
|
|
->when($this->adapter->once('name', $handler)) |
|
62
|
|
|
->then |
|
63
|
|
|
->array($this->adapter->get('name')) |
|
64
|
|
|
->hasSize(1) |
|
65
|
|
|
->output(function () use ($event) { |
|
66
|
|
|
$this->adapter->dispatch('name', $event); |
|
67
|
|
|
}) |
|
68
|
|
|
->isEqualTo("Youpi!") |
|
69
|
|
|
->then |
|
70
|
|
|
->array($this->adapter->get('name')) |
|
71
|
|
|
->hasSize(0); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$this |
|
78
|
|
|
->given( |
|
79
|
|
|
$handler = function () { |
|
80
|
|
|
echo "Youpi!"; |
|
81
|
|
|
}, |
|
82
|
|
|
$this->adapter->on('name', $handler) |
|
83
|
|
|
) |
|
84
|
|
|
->then |
|
85
|
|
|
->array($this->adapter->get('name')) |
|
86
|
|
|
->hasSize(1) |
|
87
|
|
|
->when($this->adapter->remove('name', $handler)) |
|
88
|
|
|
->then |
|
89
|
|
|
->array($this->adapter->get('name')) |
|
90
|
|
|
->hasSize(0); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.