1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KoineTests; |
4
|
|
|
|
5
|
|
|
use Koine\Parameters; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Marcelo Jacobus <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class ParametersTest extends PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
Parameters::$throwExceptions = true; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getUserExample() |
19
|
|
|
{ |
20
|
|
|
return new Parameters(array( |
21
|
|
|
'empty' => array(), |
22
|
|
|
'user' => array( |
23
|
|
|
'name' => 'Jon', |
24
|
|
|
'lastName' => 'Doe', |
25
|
|
|
) |
26
|
|
|
)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function tearDown() |
30
|
|
|
{ |
31
|
|
|
Parameters::$throwExceptions = true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
*/ |
37
|
|
|
public function inheritsFromKoineHash() |
38
|
|
|
{ |
39
|
|
|
$this->assertInstanceOf('Koine\Hash', $this->getUserExample()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function throwExceptionsIsGlobalConfiguration() |
46
|
|
|
{ |
47
|
|
|
$params = $this->getUserExample(); |
48
|
|
|
$this->assertTrue($params->getThrowExceptions()); |
49
|
|
|
|
50
|
|
|
Parameters::$throwExceptions = false; |
51
|
|
|
|
52
|
|
|
$this->assertFalse($params->getThrowExceptions()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
* @expectedException \Koine\Parameters\ParameterMissingException |
58
|
|
|
* @expectedExceptionMessage Missing param 'person' |
59
|
|
|
*/ |
60
|
|
|
public function throwExceptionWhenRequiredParamDoesNotExistAndConfigIsToThrow() |
61
|
|
|
{ |
62
|
|
|
$params = $this->getUserExample(); |
63
|
|
|
$params->requireParam('person'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
* @expectedException \Koine\Parameters\ParameterMissingException |
69
|
|
|
* @expectedExceptionMessage Missing param 'empty' |
70
|
|
|
*/ |
71
|
|
|
public function throwExceptionWhenRequiredParamIsEmpty() |
72
|
|
|
{ |
73
|
|
|
$params = $this->getUserExample(); |
74
|
|
|
$params['empty'] = new Parameters(); |
75
|
|
|
$params->requireParam('empty'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
* @expectedException \Koine\Parameters\ParameterMissingException |
81
|
|
|
* @expectedExceptionMessage Missing param 'empty' |
82
|
|
|
*/ |
83
|
|
|
public function throwExceptionWhenRequiredParamIsEmptyArray() |
84
|
|
|
{ |
85
|
|
|
$params = $this->getUserExample(); |
86
|
|
|
$params['empty'] = array(); |
87
|
|
|
$params->requireParam('empty'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function requireParamReturnsOnlyTheRequiredParam() |
94
|
|
|
{ |
95
|
|
|
$expected = array( |
96
|
|
|
'name' => 'Jon', |
97
|
|
|
'lastName' => 'Doe', |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$params = $this->getUserExample()->requireParam('user'); |
101
|
|
|
|
102
|
|
|
$this->assertEquals($expected, $params->toArray()); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf('Koine\Parameters', $params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @expectedException Koine\Parameters\UnpermittedParameterException |
110
|
|
|
* @expectedExceptionMessage Parameter 'admin' is not allowed |
111
|
|
|
*/ |
112
|
|
|
public function permitThrowsExeptionWhenUnpermitedParamIsPassedIn() |
113
|
|
|
{ |
114
|
|
|
$params = $this->getUserExample(); |
115
|
|
|
|
116
|
|
|
$this->assertTrue($this->getUserExample()->getThrowExceptions()); |
117
|
|
|
$params['user']['admin'] = true; |
118
|
|
|
$params->requireParam('user')->permit(array('name', 'lastName')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
*/ |
124
|
|
|
public function permitReturnsReturnsAllowedParameters() |
125
|
|
|
{ |
126
|
|
|
$params = $this->getUserExample(); |
127
|
|
|
|
128
|
|
|
$permitted = $params->requireParam('user') |
129
|
|
|
->permit(array('name', 'lastName')); |
130
|
|
|
|
131
|
|
|
$this->assertInstanceOf(get_class($params), $permitted); |
132
|
|
|
|
133
|
|
|
$expected = array( |
134
|
|
|
'name' => 'Jon', |
135
|
|
|
'lastName' => 'Doe', |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$this->assertEquals($expected, $permitted->toArray()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function permitFiltersOutParamsThatAreNotAllowed() |
145
|
|
|
{ |
146
|
|
|
Parameters::$throwExceptions = false; |
147
|
|
|
|
148
|
|
|
$params = $this->getUserExample(); |
149
|
|
|
|
150
|
|
|
$this->assertFalse($params->getThrowExceptions()); |
151
|
|
|
|
152
|
|
|
$permitted = $params->requireParam('user')->permit(array( |
153
|
|
|
'name', |
154
|
|
|
)); |
155
|
|
|
|
156
|
|
|
$this->assertInstanceOf(get_class($params), $permitted); |
157
|
|
|
|
158
|
|
|
$expected = array('name' => 'Jon'); |
159
|
|
|
|
160
|
|
|
$this->assertEquals($expected, $permitted->toArray()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @test |
165
|
|
|
*/ |
166
|
|
|
public function permitReturnsArrayAyValueIfAnEmptyArrayPassedInThePermitParam() |
167
|
|
|
{ |
168
|
|
|
$dataCollection = array( |
169
|
|
|
array('id' => array()), |
170
|
|
|
array('id' => array('foo')), |
171
|
|
|
array('id' => array(1, 2, 3)), |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
foreach ($dataCollection as $data) { |
175
|
|
|
$params = new Parameters($data); |
176
|
|
|
|
177
|
|
|
$actual = $params->permit(array('id' => array()))->toArray(); |
178
|
|
|
|
179
|
|
|
$this->assertEquals($data, $actual); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @test |
185
|
|
|
*/ |
186
|
|
|
public function permitHandlesSimpleArrayPermissions() |
187
|
|
|
{ |
188
|
|
|
Parameters::$throwExceptions = false; |
189
|
|
|
|
190
|
|
|
$data = array( |
191
|
|
|
'title' => 'Title', |
192
|
|
|
'edition' => 'third', |
193
|
|
|
'authors' => array( |
194
|
|
|
array( |
195
|
|
|
'name' => 'Jon', |
196
|
|
|
'birthday' => '1960-01-02', |
197
|
|
|
), |
198
|
|
|
array( |
199
|
|
|
'name' => 'Daniel', |
200
|
|
|
'birthday' => '1960-01-02', |
201
|
|
|
), |
202
|
|
|
) |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$params = new Parameters($data); |
206
|
|
|
|
207
|
|
|
$actual = $params->permit(array( |
208
|
|
|
'authors' => array('name'), |
209
|
|
|
'title' => 'Title' |
210
|
|
|
))->toArray(); |
211
|
|
|
|
212
|
|
|
$expected = array( |
213
|
|
|
'title' => 'Title', |
214
|
|
|
'authors' => array( |
215
|
|
|
array('name' => 'Jon'), |
216
|
|
|
array('name' => 'Daniel'), |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$this->assertEquals($expected, $actual); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function getPublicationExample() |
224
|
|
|
{ |
225
|
|
|
$data = array( |
226
|
|
|
'book' => array( |
227
|
|
|
'title' => 'Some Title', |
228
|
|
|
'edition' => '3', |
229
|
|
|
'authors' => array( |
230
|
|
|
array( |
231
|
|
|
'name' => 'Jon', |
232
|
|
|
'birthday' => '1960-01-02', |
233
|
|
|
), |
234
|
|
|
array( |
235
|
|
|
'name' => 'Daniel', |
236
|
|
|
'birthday' => '1960-01-02', |
237
|
|
|
), |
238
|
|
|
) |
239
|
|
|
), |
240
|
|
|
'foo' => 'bar', |
241
|
|
|
'bar' => 'foo' |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
return new Parameters($data); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @test |
249
|
|
|
*/ |
250
|
|
|
public function permitHandlesNestedPermissions() |
251
|
|
|
{ |
252
|
|
|
Parameters::$throwExceptions = false; |
253
|
|
|
|
254
|
|
|
$params = $this->getPublicationExample(); |
255
|
|
|
|
256
|
|
|
$actual = $params->permit(array( |
257
|
|
|
'book' => array( |
258
|
|
|
'authors' => array('name'), |
259
|
|
|
'title' |
260
|
|
|
), |
261
|
|
|
'foo' |
262
|
|
|
))->toArray(); |
263
|
|
|
|
264
|
|
|
$expected = array( |
265
|
|
|
'book' => array( |
266
|
|
|
'title' => 'Some Title', |
267
|
|
|
'authors' => array( |
268
|
|
|
array('name' => 'Jon'), |
269
|
|
|
array('name' => 'Daniel'), |
270
|
|
|
) |
271
|
|
|
), |
272
|
|
|
'foo' => 'bar' |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
$this->assertEquals($expected, $actual); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @test |
280
|
|
|
*/ |
281
|
|
|
public function permitHandlesNestedEmptyArrayOptions() |
282
|
|
|
{ |
283
|
|
|
Parameters::$throwExceptions = false; |
284
|
|
|
|
285
|
|
|
$params = $this->getPublicationExample(); |
286
|
|
|
|
287
|
|
|
$actual = $params->permit(array( |
288
|
|
|
'book' => array( |
289
|
|
|
'authors' => array(), |
290
|
|
|
'title' |
291
|
|
|
), |
292
|
|
|
'foo' |
293
|
|
|
))->toArray(); |
294
|
|
|
|
295
|
|
|
$expected = array( |
296
|
|
|
'book' => array( |
297
|
|
|
'title' => 'Some Title', |
298
|
|
|
'authors' => array( |
299
|
|
|
array('name' => 'Jon', 'birthday' => '1960-01-02'), |
300
|
|
|
array('name' => 'Daniel', 'birthday' => '1960-01-02'), |
301
|
|
|
) |
302
|
|
|
), |
303
|
|
|
'foo' => 'bar' |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
$this->assertEquals($expected, $actual); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function providerForNestedPermissions() |
310
|
|
|
{ |
311
|
|
|
return array( |
312
|
|
|
# data set |
313
|
|
|
array( |
314
|
|
|
# data |
315
|
|
|
array('tags' => array( |
316
|
|
|
array('name' => 'php', 'order' => 1), |
317
|
|
|
array('name' => 'ruby', 'order' => 2), |
318
|
|
|
)), |
319
|
|
|
|
320
|
|
|
# permit |
321
|
|
|
array('tags' => array('name')), |
322
|
|
|
|
323
|
|
|
# expected |
324
|
|
|
array('tags' => array( |
325
|
|
|
array('name' => 'php'), |
326
|
|
|
array('name' => 'ruby'), |
327
|
|
|
)), |
328
|
|
|
), |
329
|
|
|
|
330
|
|
|
# data set |
331
|
|
|
array( |
332
|
|
|
# data |
333
|
|
|
array('tags' => array('php', 'ruby')), |
334
|
|
|
|
335
|
|
|
# permit |
336
|
|
|
array('tags' => array()), |
337
|
|
|
|
338
|
|
|
# expected |
339
|
|
|
array('tags' => array('php', 'ruby')), |
340
|
|
|
), |
341
|
|
|
|
342
|
|
|
# data set |
343
|
|
|
array( |
344
|
|
|
# data |
345
|
|
|
array('tags' => 'abc'), |
346
|
|
|
|
347
|
|
|
# permit |
348
|
|
|
array('tags' => array()), |
349
|
|
|
|
350
|
|
|
# expected |
351
|
|
|
array(), |
352
|
|
|
), |
353
|
|
|
|
354
|
|
|
# data set |
355
|
|
|
array( |
356
|
|
|
# data |
357
|
|
|
array('tags' => array('abc')), |
358
|
|
|
|
359
|
|
|
# permit |
360
|
|
|
array('tags'), |
361
|
|
|
|
362
|
|
|
# expected |
363
|
|
|
array(), |
364
|
|
|
), |
365
|
|
|
); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @test |
370
|
|
|
* @dataProvider providerForNestedPermissions |
371
|
|
|
*/ |
372
|
|
|
public function permitNestedPermissionsAreHandledCorrectly($data, $permitted, $expected) |
373
|
|
|
{ |
374
|
|
|
Parameters::$throwExceptions = false; |
375
|
|
|
$params = new Parameters($data); |
376
|
|
|
$actual = $params->permit($permitted)->toArray(); |
377
|
|
|
$this->assertEquals($expected, $actual); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|