1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of sensorario/resources repository |
5
|
|
|
* |
6
|
|
|
* (c) Simone Gentili <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sensorario\Resources\Test\Resources; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use DateTime; |
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
use Resources\Bar; |
18
|
|
|
use Resources\BirthDay; |
19
|
|
|
use Resources\UndefinedObject; |
20
|
|
|
use Resources\ComposedResource; |
21
|
|
|
use Resources\Foo; |
22
|
|
|
use Resources\MandatoryDependency; |
23
|
|
|
use Resources\ResourceWithoutRules; |
24
|
|
|
use Resources\SomeApiRequest; |
25
|
|
|
use Resources\UserCreationEvent; |
26
|
|
|
use Sensorario\Resources\Configurator; |
27
|
|
|
use Sensorario\Resources\Container; |
28
|
|
|
use Sensorario\Resources\Resource; |
29
|
|
|
use Sensorario\Resources\Validators\ResourcesValidator; |
30
|
|
|
|
31
|
|
|
final class ResourceTest extends PHPUnit_Framework_TestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @expectedException \Sensorario\Resources\Exceptions\UndefinedMethodException |
35
|
|
|
* @expectedExceptionMessageRegExp #Method `.*::.*()` is not yet implemented# |
36
|
|
|
*/ |
37
|
|
|
public function testExceptionIsThrownWhenNotYetImplementedMethodIsCalled() |
38
|
|
|
{ |
39
|
|
|
$foo = Foo::box([ |
40
|
|
|
'name' => 'Simone', |
41
|
|
|
'surname' => 'Gentili', |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$foo->notYetImplementedMethod(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @expectedException Sensorario\Resources\Exceptions\NotAllowedKeyException |
49
|
|
|
* @expectedExceptionMessageRegExp #Key `.*::.*` is not allowed# |
50
|
|
|
*/ |
51
|
|
|
public function testNotAllowedFieldThroghRuntimeException() |
52
|
|
|
{ |
53
|
|
|
Foo::box([ |
54
|
|
|
'name' => 'Simone', |
55
|
|
|
'surname' => 'Gentili', |
56
|
|
|
'not' => 'allowed', |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyException |
62
|
|
|
* @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set# |
63
|
|
|
*/ |
64
|
|
|
public function testMissingMandatoryFieldThroghRuntimeException() |
65
|
|
|
{ |
66
|
|
|
Foo::box([]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testMandatoryFieldsAreAuthomaticallyAllowed() |
70
|
|
|
{ |
71
|
|
|
Foo::box([ |
72
|
|
|
'name' => 'Simone', |
73
|
|
|
'surname' => 'Gentili', |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testResourcesHasMagicMethod() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$foo = Foo::box([ |
80
|
|
|
'name' => 'Simone', |
81
|
|
|
'surname' => 'Gentili', |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( |
85
|
|
|
'Simone', |
86
|
|
|
$foo->name() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyNameEmptyException |
92
|
|
|
* @expectedExceptionMessage Oops! Property name requested is empty string!! |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testExceptionMessageInCaseOfEmptyPropertyName() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$foo = Foo::box([ |
97
|
|
|
'name' => 'Simone', |
98
|
|
|
'surname' => 'Gentili', |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->assertEquals( |
102
|
|
|
'Simone', |
103
|
|
|
$foo->get('') |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @expectedException Sensorario\Resources\Exceptions\FactoryMethodException |
109
|
|
|
* @expectedExceptionMessageRegExp #Invalid factory method# |
110
|
|
|
*/ |
111
|
|
|
public function testFactoryMethods() |
112
|
|
|
{ |
113
|
|
|
Bar::invalidFactoryName(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testCanHaveDefaultValues() |
117
|
|
|
{ |
118
|
|
|
$foo = Bar::box(); |
119
|
|
|
|
120
|
|
|
$this->assertEquals( |
121
|
|
|
'Firefox', |
122
|
|
|
$foo->name() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testPropertyExists() |
127
|
|
|
{ |
128
|
|
|
$foo = Bar::box(); |
129
|
|
|
|
130
|
|
|
$this->assertFalse( |
131
|
|
|
$foo->hasProperty('nonExistentProperty') |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @dataProvider propertiesProvider */ |
136
|
|
|
public function testHasProperties($result, $properties) |
137
|
|
|
{ |
138
|
|
|
$foo = UserCreationEvent::box([ |
139
|
|
|
'type' => 'human', |
140
|
|
|
'username' => 'Sensorario', |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$this->assertSame( |
144
|
|
|
$result, |
145
|
|
|
$foo->hasProperties($properties) |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function propertiesProvider() |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
[false, ['buppa']], |
153
|
|
|
[true, ['type', 'username']], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testAllowAccessToProperties() |
158
|
|
|
{ |
159
|
|
|
$foo = Bar::box([ |
160
|
|
|
'name' => 'Firefox' |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$this->assertEquals( |
164
|
|
|
'Firefox', |
165
|
|
|
$foo->get('name') |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testAllowAccessToPropertiesThroughDefaultValue() |
170
|
|
|
{ |
171
|
|
|
$foo = Bar::box(); |
172
|
|
|
|
173
|
|
|
$this->assertEquals( |
174
|
|
|
'Firefox', |
175
|
|
|
$foo->get('name') |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @expectedException Sensorario\Resources\Exceptions\NoValuesException |
181
|
|
|
*/ |
182
|
|
|
public function testThroughExceptionWhenNoValuesProvided() |
183
|
|
|
{ |
184
|
|
|
$foo = Bar::box(); |
185
|
|
|
$foo->get('foo'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @expectedException Sensorario\Resources\Exceptions\AttributeTypeException |
190
|
|
|
* @expectedExceptionMessageRegExp #Attribute `.*` must be of type `array`# |
191
|
|
|
*/ |
192
|
|
|
public function testPropertyCouldBeAScalar() |
193
|
|
|
{ |
194
|
|
|
SomeApiRequest::box([ |
195
|
|
|
'fields' => 'not a scalar', |
196
|
|
|
]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @expectedException Sensorario\Resources\Exceptions\NotObjectTypeFoundException |
201
|
|
|
* @expectedExceptionMessageRegExp #Attribute `.*` must be an object of type DateTime# |
202
|
|
|
*/ |
203
|
|
|
public function testPropertyCouldBeTheRightnObject() |
204
|
|
|
{ |
205
|
|
|
BirthDay::box([ |
206
|
|
|
'date' => new DateInterval('P1D'), |
207
|
|
|
]); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testPropertiesAccessor() |
211
|
|
|
{ |
212
|
|
|
$foo = Foo::box([ |
213
|
|
|
'name' => 'Sam', |
214
|
|
|
]); |
215
|
|
|
|
216
|
|
|
$this->assertEquals([ |
217
|
|
|
'name' => 'Sam', |
218
|
|
|
], |
219
|
|
|
$foo->properties() |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyNotSetException |
225
|
|
|
* @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set# |
226
|
|
|
*/ |
227
|
|
|
public function testWhenCondition() |
228
|
|
|
{ |
229
|
|
|
MandatoryDependency::box([ |
230
|
|
|
'foo' => 'bar', |
231
|
|
|
'mandatory_mello' => 'bar', |
232
|
|
|
]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testShouldNotFail() |
236
|
|
|
{ |
237
|
|
|
MandatoryDependency::box([ |
238
|
|
|
'foo' => 'bar', |
239
|
|
|
]); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testResourcesComposition() |
243
|
|
|
{ |
244
|
|
|
$composition = ComposedResource::box([ |
245
|
|
|
'credentials' => Foo::box([ |
246
|
|
|
'name' => 'Sam' |
247
|
|
|
]), |
248
|
|
|
]); |
249
|
|
|
|
250
|
|
|
$this->assertEquals([ |
251
|
|
|
'credentials' => [ |
252
|
|
|
'name' => 'Sam', |
253
|
|
|
] |
254
|
|
|
], |
255
|
|
|
$composition->properties() |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @expectedException RuntimeException |
261
|
|
|
* @expectedExceptionMessageRegExp #When property `.*` has value `.*` also `.*` is mandatory# |
262
|
|
|
*/ |
263
|
|
|
public function testMandatoryValuesWhenPropertyAssumeAValue() |
264
|
|
|
{ |
265
|
|
|
UserCreationEvent::box([ |
266
|
|
|
'type' => 'guest', |
267
|
|
|
]); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function test() |
271
|
|
|
{ |
272
|
|
|
UserCreationEvent::box([ |
273
|
|
|
'type' => 'human', |
274
|
|
|
'username' => 'Sensorario', |
275
|
|
|
]); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyWithoutRuleException |
280
|
|
|
* @expectedExceptionMessageRegExp #When property `.*` is an object class, must be defined in Resources::rules()# |
281
|
|
|
*/ |
282
|
|
|
public function testAnExceptionIsThrownIfAPropertyIsAnObjectButClassInNotDefinedInRuleMethod() |
283
|
|
|
{ |
284
|
|
|
ResourceWithoutRules::box([ |
285
|
|
|
'datetime' => new DateTime(), |
286
|
|
|
]); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function testDefaultValuesTwo() |
290
|
|
|
{ |
291
|
|
|
$resource = new Resource( |
292
|
|
|
[], |
293
|
|
|
new ResourcesValidator() |
294
|
|
|
); |
295
|
|
|
|
296
|
|
|
$this->assertEquals( |
297
|
|
|
[], |
298
|
|
|
$resource->allowed() |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
$resource->applyConfiguration( |
302
|
|
|
new Configurator( |
303
|
|
|
'bar', |
304
|
|
|
new Container([ |
305
|
|
|
'resources' => [ |
306
|
|
|
'bar' => [ |
307
|
|
|
'constraints' => [ |
308
|
|
|
'allowed' => [ 'allowed_property_name' ], |
309
|
|
|
] |
310
|
|
|
], |
311
|
|
|
], |
312
|
|
|
]) |
313
|
|
|
) |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$this->assertEquals( |
317
|
|
|
[ 'allowed_property_name' ], |
318
|
|
|
$resource->allowed('bar') |
|
|
|
|
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testDefaultValues() |
323
|
|
|
{ |
324
|
|
|
$configurator = new Configurator( |
325
|
|
|
'foo', |
326
|
|
|
new Container([ |
327
|
|
|
'resources' => [ |
328
|
|
|
'foo' => [ |
329
|
|
|
'constraints' => [ |
330
|
|
|
'mandatory' => [ |
331
|
|
|
'ciambella', |
332
|
|
|
], |
333
|
|
|
'defaults' => [ |
334
|
|
|
'ciambella' => '42', |
335
|
|
|
], |
336
|
|
|
] |
337
|
|
|
], |
338
|
|
|
], |
339
|
|
|
]) |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
$resource = Resource::box( |
343
|
|
|
[], |
344
|
|
|
$configurator |
345
|
|
|
); |
346
|
|
|
|
347
|
|
|
$this->assertEquals( |
348
|
|
|
'42', |
349
|
|
|
$resource->get('ciambella') |
350
|
|
|
); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testResourceShouldBeCreatedViaContainer() |
354
|
|
|
{ |
355
|
|
|
$container = new Container([ |
356
|
|
|
'resources' => [ |
357
|
|
|
'foo' => [ |
358
|
|
|
'constraints' => [ |
359
|
|
|
'allowed' => [ 'allowed_property_name' ], |
360
|
|
|
] |
361
|
|
|
], |
362
|
|
|
'unused_resource' => [ |
363
|
|
|
'constraints' => [ |
364
|
|
|
'allowed' => [ 'bar' ], |
365
|
|
|
] |
366
|
|
|
], |
367
|
|
|
], |
368
|
|
|
]); |
369
|
|
|
|
370
|
|
|
$this->assertEquals( |
371
|
|
|
[ 'allowed_property_name' ], |
372
|
|
|
$container->allowed('foo') |
373
|
|
|
); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyNotSetException |
378
|
|
|
* @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set# |
379
|
|
|
*/ |
380
|
|
View Code Duplication |
public function testDependentMandatoryProperties() |
|
|
|
|
381
|
|
|
{ |
382
|
|
|
$configurator = new Configurator( |
383
|
|
|
'foo', |
384
|
|
|
new Container([ |
385
|
|
|
'resources' => [ |
386
|
|
|
'foo' => [ |
387
|
|
|
'constraints' => [ |
388
|
|
|
'allowed' => [ |
389
|
|
|
'bar', |
390
|
|
|
], |
391
|
|
|
'mandatory' => [ |
392
|
|
|
'mandatory_property_name', |
393
|
|
|
'foo' => [ |
394
|
|
|
'when' => [ |
395
|
|
|
'property' => 'bar', |
396
|
|
|
'condition' => 'is_present', |
397
|
|
|
] |
398
|
|
|
], |
399
|
|
|
], |
400
|
|
|
] |
401
|
|
|
], |
402
|
|
|
], |
403
|
|
|
]) |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
$properties = [ |
407
|
|
|
'mandatory_property_name' => '42', |
408
|
|
|
'bar' => 'beer', |
409
|
|
|
]; |
410
|
|
|
|
411
|
|
|
Resource::box( |
412
|
|
|
$properties, |
413
|
|
|
$configurator |
414
|
|
|
); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
View Code Duplication |
public function testMandatoryConstraintsAreAutomaticallyAllowed() |
|
|
|
|
418
|
|
|
{ |
419
|
|
|
$container = new Container([ |
420
|
|
|
'resources' => [ |
421
|
|
|
'foo' => [ |
422
|
|
|
'constraints' => [ |
423
|
|
|
'mandatory' => [ 'mandatory_property' ], |
424
|
|
|
] |
425
|
|
|
], |
426
|
|
|
], |
427
|
|
|
]); |
428
|
|
|
|
429
|
|
|
$this->assertEquals( |
430
|
|
|
[ 'mandatory_property' ], |
431
|
|
|
$container->allowed('foo') |
432
|
|
|
); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
public function testPropertyType() |
436
|
|
|
{ |
437
|
|
|
$configurator = new Configurator( |
438
|
|
|
'foo', |
439
|
|
|
new Container([ |
440
|
|
|
'resources' => [ |
441
|
|
|
'foo' => [ |
442
|
|
|
'constraints' => [ |
443
|
|
|
'mandatory' => [ 'date' ], |
444
|
|
|
'rules' => [ 'date' => [ 'object' => 'DateTime' ] ], |
445
|
|
|
] |
446
|
|
|
], |
447
|
|
|
], |
448
|
|
|
]) |
449
|
|
|
); |
450
|
|
|
|
451
|
|
|
$properties = [ |
452
|
|
|
'date' => new \DateTime(), |
453
|
|
|
]; |
454
|
|
|
|
455
|
|
|
Resource::box($properties, $configurator); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @expectedException Sensorario\Resources\Exceptions\UnexpectedValueException |
460
|
|
|
* @expectedExceptionMessageRegExp #Value `.*` is not allowed for key `.*`. Allowed values are:# |
461
|
|
|
*/ |
462
|
|
View Code Duplication |
public function testAllowedValues() |
|
|
|
|
463
|
|
|
{ |
464
|
|
|
$configurator = new Configurator( |
465
|
|
|
'foo', |
466
|
|
|
new Container([ |
467
|
|
|
'resources' => [ |
468
|
|
|
'foo' => [ |
469
|
|
|
'constraints' => [ |
470
|
|
|
'allowed' => [ 'user_type' ], |
471
|
|
|
'allowedValues' => [ |
472
|
|
|
'user_type' => [ |
473
|
|
|
4, |
474
|
|
|
7, |
475
|
|
|
], |
476
|
|
|
], |
477
|
|
|
], |
478
|
|
|
], |
479
|
|
|
], |
480
|
|
|
]) |
481
|
|
|
); |
482
|
|
|
|
483
|
|
|
$properties = [ 'user_type' => 3 ]; |
484
|
|
|
|
485
|
|
|
Resource::box( |
486
|
|
|
$properties, |
487
|
|
|
$configurator |
488
|
|
|
); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
public function testRewriteRulesWithCondition() |
492
|
|
|
{ |
493
|
|
|
$configurator = new Configurator( |
494
|
|
|
'foo', |
495
|
|
|
new Container([ |
496
|
|
|
'resources' => [ |
497
|
|
|
'foo' => [ |
498
|
|
|
'rewrite' => [ |
499
|
|
|
'width' => [ |
500
|
|
|
'set' => [ |
501
|
|
|
'equals_to' => 'height', |
502
|
|
|
], |
503
|
|
|
'when' => [ |
504
|
|
|
'greater_than' => 'height', |
505
|
|
|
], |
506
|
|
|
], |
507
|
|
|
], |
508
|
|
|
'constraints' => [ |
509
|
|
|
'allowed' => [ |
510
|
|
|
'width', |
511
|
|
|
'height', |
512
|
|
|
], |
513
|
|
|
], |
514
|
|
|
], |
515
|
|
|
], |
516
|
|
|
]) |
517
|
|
|
); |
518
|
|
|
$properties = [ |
519
|
|
|
'width' => 3000, |
520
|
|
|
'height' => 400, |
521
|
|
|
]; |
522
|
|
|
|
523
|
|
|
$box = Resource::box( |
524
|
|
|
$properties, |
525
|
|
|
$configurator |
526
|
|
|
); |
527
|
|
|
|
528
|
|
|
$overwritternProperties = [ |
529
|
|
|
'width' => 400, |
530
|
|
|
'height' => 400, |
531
|
|
|
]; |
532
|
|
|
|
533
|
|
|
$overWrittenBox = Resource::box( |
534
|
|
|
$overwritternProperties, |
535
|
|
|
$configurator |
536
|
|
|
); |
537
|
|
|
|
538
|
|
|
$this->assertEquals( |
539
|
|
|
$overWrittenBox, |
540
|
|
|
$box |
541
|
|
|
); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* @expectedException Sensorario\Resources\Exceptions\OutOfRangeException |
546
|
|
|
* @expectedExceptionMessageRegExp #Value `.*` is out of range: `.*`.# |
547
|
|
|
*/ |
548
|
|
View Code Duplication |
public function testAcceptRangeOfValues() |
|
|
|
|
549
|
|
|
{ |
550
|
|
|
$configurator = new Configurator( |
551
|
|
|
'foo', |
552
|
|
|
new Container([ |
553
|
|
|
'resources' => [ |
554
|
|
|
'foo' => [ |
555
|
|
|
'constraints' => [ |
556
|
|
|
'allowedRanges' => [ |
557
|
|
|
'age' => [ |
558
|
|
|
'more_than' => 3, |
559
|
|
|
'less_than' => 42, |
560
|
|
|
], |
561
|
|
|
], |
562
|
|
|
'allowed' => [ |
563
|
|
|
'age' |
564
|
|
|
], |
565
|
|
|
], |
566
|
|
|
], |
567
|
|
|
], |
568
|
|
|
]) |
569
|
|
|
); |
570
|
|
|
|
571
|
|
|
Resource::box( |
572
|
|
|
[ 'age' => 2 ], |
573
|
|
|
$configurator |
574
|
|
|
); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
public function testAllResourcesInheritGlobalAllowingConfiguration() |
578
|
|
|
{ |
579
|
|
|
$configurator = new Configurator( |
580
|
|
|
'foo', |
581
|
|
|
new Container([ |
582
|
|
|
'globals' => [ |
583
|
|
|
'allowed' => [ |
584
|
|
|
'width', |
585
|
|
|
'height', |
586
|
|
|
], |
587
|
|
|
], |
588
|
|
|
'resources' => [ |
589
|
|
|
'foo' => [ |
590
|
|
|
'constraints' => [ |
591
|
|
|
'allowed' => [ |
592
|
|
|
'foo_size', |
593
|
|
|
], |
594
|
|
|
], |
595
|
|
|
], |
596
|
|
|
'bar' => [ |
597
|
|
|
'constraints' => [ |
598
|
|
|
'allowed' => [ |
599
|
|
|
'bar_size', |
600
|
|
|
], |
601
|
|
|
], |
602
|
|
|
], |
603
|
|
|
], |
604
|
|
|
]) |
605
|
|
|
); |
606
|
|
|
|
607
|
|
|
$resource = Resource::box( |
608
|
|
|
[], |
609
|
|
|
$configurator |
610
|
|
|
); |
611
|
|
|
|
612
|
|
|
$this->assertEquals( |
613
|
|
|
['foo_size', 'width', 'height'], |
614
|
|
|
$resource->allowed() |
615
|
|
|
); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* @expectedException RuntimeException |
620
|
|
|
*/ |
621
|
|
View Code Duplication |
public function testHasMandatoryPropertiesWhenAnotherOneHasAParticularValue() |
|
|
|
|
622
|
|
|
{ |
623
|
|
|
$configurator = new Configurator( |
624
|
|
|
'foo', |
625
|
|
|
new Container([ |
626
|
|
|
'resources' => [ |
627
|
|
|
'foo' => [ |
628
|
|
|
'constraints' => [ |
629
|
|
|
'allowed' => [ |
630
|
|
|
'bar', |
631
|
|
|
], |
632
|
|
|
'mandatory' => [ |
633
|
|
|
'mandatory_property_name', |
634
|
|
|
'foo' => [ |
635
|
|
|
'when' => [ |
636
|
|
|
'property' => 'bar', |
637
|
|
|
'has_value' => '42', |
638
|
|
|
] |
639
|
|
|
], |
640
|
|
|
], |
641
|
|
|
] |
642
|
|
|
], |
643
|
|
|
], |
644
|
|
|
]) |
645
|
|
|
); |
646
|
|
|
|
647
|
|
|
$properties = [ |
648
|
|
|
'bar' => '42', |
649
|
|
|
]; |
650
|
|
|
|
651
|
|
|
Resource::box( |
652
|
|
|
$properties, |
653
|
|
|
$configurator |
654
|
|
|
); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* @expectedException Sensorario\Resources\Exceptions\EmailException |
659
|
|
|
*/ |
660
|
|
|
public function testEmailValidationFails() |
661
|
|
|
{ |
662
|
|
|
$configurator = new Configurator( |
663
|
|
|
'email-resource', |
664
|
|
|
new Container([ |
665
|
|
|
'resources' => [ |
666
|
|
|
'email-resource' => [ |
667
|
|
|
'constraints' => [ |
668
|
|
|
'mandatory' => [ |
669
|
|
|
'first-email', |
670
|
|
|
], |
671
|
|
|
'rules' => [ |
672
|
|
|
'first-email' => [ 'custom-validator' => 'email' ], |
673
|
|
|
], |
674
|
|
|
] |
675
|
|
|
], |
676
|
|
|
], |
677
|
|
|
]) |
678
|
|
|
); |
679
|
|
|
|
680
|
|
|
$properties = [ |
681
|
|
|
'first-email' => 'invalid email', |
682
|
|
|
]; |
683
|
|
|
|
684
|
|
|
Resource::box($properties, $configurator); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* @expectedException Sensorario\Resources\Exceptions\WrongPropertyValueException |
689
|
|
|
* @expectedExceptionMessageRegExp #Property .* must be an integer!# |
690
|
|
|
*/ |
691
|
|
|
public function testIntegersCanBeDefinedWithNumberRule() |
692
|
|
|
{ |
693
|
|
|
$configurator = new Configurator( |
694
|
|
|
'type-with-number', |
695
|
|
|
new Container([ |
696
|
|
|
'resources' => [ |
697
|
|
|
'type-with-number' => [ |
698
|
|
|
'constraints' => [ |
699
|
|
|
'mandatory' => [ 'a-magic-number' ], |
700
|
|
|
'rules' => [ 'a-magic-number' => [ 'scalar' => 'integer' ] ], |
701
|
|
|
] |
702
|
|
|
], |
703
|
|
|
], |
704
|
|
|
]) |
705
|
|
|
); |
706
|
|
|
|
707
|
|
|
$properties = [ |
708
|
|
|
'a-magic-number' => '42', |
709
|
|
|
]; |
710
|
|
|
|
711
|
|
|
$resource = Resource::box($properties, $configurator); |
|
|
|
|
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
public function testEmailValidation() |
715
|
|
|
{ |
716
|
|
|
$configurator = new Configurator( |
717
|
|
|
'email-resource', |
718
|
|
|
new Container([ |
719
|
|
|
'resources' => [ |
720
|
|
|
'email-resource' => [ |
721
|
|
|
'constraints' => [ |
722
|
|
|
'mandatory' => [ |
723
|
|
|
'first-email', |
724
|
|
|
], |
725
|
|
|
'rules' => [ |
726
|
|
|
'first-email' => [ 'custom-validator' => 'email' ], |
727
|
|
|
], |
728
|
|
|
] |
729
|
|
|
], |
730
|
|
|
], |
731
|
|
|
]) |
732
|
|
|
); |
733
|
|
|
|
734
|
|
|
$properties = [ |
735
|
|
|
'first-email' => '[email protected]', |
736
|
|
|
]; |
737
|
|
|
|
738
|
|
|
$resource = Resource::box($properties, $configurator); |
739
|
|
|
|
740
|
|
|
$this->assertEquals( |
741
|
|
|
'[email protected]', |
742
|
|
|
$resource->get('first-email') |
743
|
|
|
); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** @dataProvider rules */ |
747
|
|
View Code Duplication |
public function testRulesKnowsIfRuleIsDefinedOrNot($expectation, $ruleName) |
|
|
|
|
748
|
|
|
{ |
749
|
|
|
$configurator = new Configurator( |
750
|
|
|
'email-resource', |
751
|
|
|
new Container([ |
752
|
|
|
'resources' => [ |
753
|
|
|
'email-resource' => [ |
754
|
|
|
'constraints' => [ |
755
|
|
|
'mandatory' => [ |
756
|
|
|
'first-email', |
757
|
|
|
], |
758
|
|
|
'rules' => [ |
759
|
|
|
'first-email' => [ 'custom-validator' => 'email' ], |
760
|
|
|
], |
761
|
|
|
] |
762
|
|
|
], |
763
|
|
|
], |
764
|
|
|
]) |
765
|
|
|
); |
766
|
|
|
|
767
|
|
|
$properties = [ |
768
|
|
|
'first-email' => '[email protected]', |
769
|
|
|
]; |
770
|
|
|
|
771
|
|
|
$resource = Resource::box($properties, $configurator); |
772
|
|
|
|
773
|
|
|
$this->assertEquals( |
774
|
|
|
$expectation, |
775
|
|
|
$resource->isRuleDefinedFor($ruleName) |
776
|
|
|
); |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
public function rules() |
780
|
|
|
{ |
781
|
|
|
return [ |
782
|
|
|
[true, 'first-email'], |
783
|
|
|
[false, 'non-existent-field-name'], |
784
|
|
|
]; |
785
|
|
|
} |
786
|
|
|
|
787
|
|
View Code Duplication |
public function testProvideRule() |
|
|
|
|
788
|
|
|
{ |
789
|
|
|
$aSpecificRule = [ 'custom-validator' => 'email' ]; |
790
|
|
|
|
791
|
|
|
$configurator = new Configurator( |
792
|
|
|
'email-resource', |
793
|
|
|
new Container([ |
794
|
|
|
'resources' => [ |
795
|
|
|
'email-resource' => [ |
796
|
|
|
'constraints' => [ |
797
|
|
|
'mandatory' => [ |
798
|
|
|
'first-email', |
799
|
|
|
], |
800
|
|
|
'rules' => [ |
801
|
|
|
'first-email' => $aSpecificRule, |
802
|
|
|
], |
803
|
|
|
] |
804
|
|
|
], |
805
|
|
|
], |
806
|
|
|
]) |
807
|
|
|
); |
808
|
|
|
|
809
|
|
|
$properties = [ |
810
|
|
|
'first-email' => '[email protected]', |
811
|
|
|
]; |
812
|
|
|
|
813
|
|
|
$resource = Resource::box($properties, $configurator); |
814
|
|
|
|
815
|
|
|
$this->assertEquals( |
816
|
|
|
$aSpecificRule, |
817
|
|
|
$resource->getRule('first-email')->asArray() |
818
|
|
|
); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* @expectedException Sensorario\Resources\Exceptions\PropertyWithoutRuleException |
823
|
|
|
* @expectedExceptionMessage Property date is an object but is not defined in rules |
824
|
|
|
*/ |
825
|
|
|
public function testUndefinedObject() |
826
|
|
|
{ |
827
|
|
|
UndefinedObject::box([ |
828
|
|
|
'date' => new \stdClass(), |
829
|
|
|
]); |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
|
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.