|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fichier de test pour une class |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace BFW\test\unit; |
|
7
|
|
|
use \atoum; |
|
8
|
|
|
|
|
9
|
|
|
require_once(__DIR__.'/../common.php'); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Test de la class Kernel |
|
13
|
|
|
*/ |
|
14
|
|
|
class Kernel extends atoum |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var $class : Instance de la class Kernel |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $class; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var $mock : Instance du mock pour la class Kernel |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $mock; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Instanciation de la class avant chaque méthode de test |
|
28
|
|
|
*/ |
|
29
|
|
|
public function beforeTestMethod($testMethod) |
|
30
|
|
|
{ |
|
31
|
|
|
//$this->class = new \BFW\Kernel(); |
|
32
|
|
|
$this->mock = new MockKernel(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Test de la méthode attach($observer) |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testAttach() |
|
39
|
|
|
{ |
|
40
|
|
|
$observer = new MockKernelSplObserver; |
|
41
|
|
|
|
|
42
|
|
|
$this->mock->attach($observer); |
|
43
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observer); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Test de la méthode attachOther($observer) |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testAttachOther() |
|
50
|
|
|
{ |
|
51
|
|
|
$observer = new MockKernelObserver; |
|
52
|
|
|
|
|
53
|
|
|
$this->mock->attachOther($observer); |
|
54
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observer); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Test de la méthode detach($observer) |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function testDetach() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$observerA = new MockKernelSplObserver; |
|
63
|
|
|
$observerB = new MockKernelSplObserver; |
|
64
|
|
|
$observerC = new MockKernelSplObserver; |
|
65
|
|
|
|
|
66
|
|
|
$this->mock->attach($observerA); |
|
67
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
|
68
|
|
|
$this->mock->attach($observerB); |
|
69
|
|
|
$this->object($this->mock->observers[1])->isIdenticalTo($observerB); |
|
70
|
|
|
$this->mock->attach($observerC); |
|
71
|
|
|
$this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
|
72
|
|
|
|
|
73
|
|
|
$this->mock->detach($observerB); |
|
74
|
|
|
|
|
75
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
|
76
|
|
|
$this->array($this->mock->observers)->notHasKey(1); |
|
77
|
|
|
$this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Test de la méthode detachOther($observer) |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
public function testDetachOther() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$observerA = new MockKernelObserver; |
|
86
|
|
|
$observerB = new MockKernelObserver; |
|
87
|
|
|
$observerC = new MockKernelObserver; |
|
88
|
|
|
|
|
89
|
|
|
$this->mock->attachOther($observerA); |
|
90
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
|
91
|
|
|
$this->mock->attachOther($observerB); |
|
92
|
|
|
$this->object($this->mock->observers[1])->isIdenticalTo($observerB); |
|
93
|
|
|
$this->mock->attachOther($observerC); |
|
94
|
|
|
$this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
|
95
|
|
|
|
|
96
|
|
|
$this->mock->detachOther($observerB); |
|
97
|
|
|
|
|
98
|
|
|
$this->object($this->mock->observers[0])->isIdenticalTo($observerA); |
|
99
|
|
|
$this->array($this->mock->observers)->notHasKey(1); |
|
100
|
|
|
$this->object($this->mock->observers[2])->isIdenticalTo($observerC); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Test de la méthode notifyObserver($action) |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testNotifyObserver() |
|
107
|
|
|
{ |
|
108
|
|
|
$observer = new MockKernelObserver; |
|
109
|
|
|
$this->mock->attachOther($observer); |
|
110
|
|
|
|
|
111
|
|
|
$this->mock->notifyObserver('test'); |
|
112
|
|
|
$this->variable($this->mock->notify_action)->isNull(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Test de la méthode notifyAction($action) |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testNotifyAction() |
|
119
|
|
|
{ |
|
120
|
|
|
$this->object($this->mock->notifyAction('test'))->isIdenticalTo($this->mock); |
|
121
|
|
|
$this->string($this->mock->notify_action)->isEqualTo('test'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Test de la méthode notify() |
|
126
|
|
|
*/ |
|
127
|
|
|
public function testNotify() |
|
128
|
|
|
{ |
|
129
|
|
|
//Test avec une action à envoyer : appel updateWithAction |
|
130
|
|
|
$this->mock->notifyAction('test'); |
|
131
|
|
|
$this->mock->notify(); |
|
132
|
|
|
|
|
133
|
|
|
//Test sans action : appel update() |
|
134
|
|
|
$observer = new MockKernelSplObserver; |
|
135
|
|
|
$this->mock->attach($observer); |
|
136
|
|
|
$this->mock->notify(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Test de la méthode setDebug($debug) |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testSetDebug() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->mock->setDebug(true); |
|
145
|
|
|
|
|
146
|
|
|
$this->boolean($this->mock->debug)->isTrue(); |
|
147
|
|
|
$this->integer(error_reporting())->isEqualTo(E_ALL); |
|
148
|
|
|
$this->string(ini_get('display_errors'))->isEqualTo('On'); |
|
149
|
|
|
$this->string(ini_get('html_errors'))->isEqualTo('1'); |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
$this->mock->setDebug(false); |
|
153
|
|
|
|
|
154
|
|
|
$this->boolean($this->mock->debug)->isFalse(); |
|
155
|
|
|
$this->integer(error_reporting())->isEqualTo(0); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Test de la méthode getDebug() |
|
160
|
|
|
*/ |
|
161
|
|
|
public function testGetDebug() |
|
162
|
|
|
{ |
|
163
|
|
|
$this->boolean($this->mock->getDebug())->isFalse(); |
|
164
|
|
|
|
|
165
|
|
|
$this->mock->setDebug(false); |
|
166
|
|
|
$this->boolean($this->mock->getDebug())->isFalse(); |
|
167
|
|
|
|
|
168
|
|
|
$this->mock->setDebug(true); |
|
169
|
|
|
$this->boolean($this->mock->debug)->isTrue(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Mock pour les observers |
|
176
|
|
|
*/ |
|
177
|
|
|
class MockKernelSplObserver implements \SplObserver |
|
178
|
|
|
{ |
|
179
|
|
|
/** |
|
180
|
|
|
* Méthode par défaut appelé lorsque l'observer se déclanche |
|
181
|
|
|
* |
|
182
|
|
|
* @param \SplSubject $subject Le sujet déclanchant l'observer |
|
183
|
|
|
*/ |
|
184
|
|
|
public function update(\SplSubject $subject) |
|
185
|
|
|
{ |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Mock pour les observers |
|
192
|
|
|
*/ |
|
193
|
|
|
class MockKernelObserver implements \SplObserver |
|
194
|
|
|
{ |
|
195
|
|
|
/** |
|
196
|
|
|
* Méthode par défaut appelé lorsque l'observer se déclanche |
|
197
|
|
|
* |
|
198
|
|
|
* @param \SplSubject $subject Le sujet déclanchant l'observer |
|
199
|
|
|
*/ |
|
200
|
|
|
public function update(\SplSubject $subject) |
|
201
|
|
|
{ |
|
202
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* L'action à effectuer quand l'observer est déclanché |
|
207
|
|
|
* |
|
208
|
|
|
* @param \BFW\Kernel $subject Le sujet observant |
|
209
|
|
|
* @param array $action Les actions à effectuer |
|
210
|
|
|
*/ |
|
211
|
|
|
public function updateWithAction($subject, $action) |
|
|
|
|
|
|
212
|
|
|
{ |
|
213
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Mock pour la classe Kernel |
|
219
|
|
|
*/ |
|
220
|
|
|
class MockKernel extends \BFW\Kernel |
|
221
|
|
|
{ |
|
222
|
|
|
/** |
|
223
|
|
|
* Accesseur get |
|
224
|
|
|
*/ |
|
225
|
|
|
public function __get($name) {return $this->$name;} |
|
226
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
|
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.