Completed
Pull Request — master (#123)
by Simone
32:20 queued 29:11
created

testExceptionMessageInCaseOfEmptyPropertyName()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
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
        $configurator = new Configurator(
40
            'empty_resource',
41
            new Container([
42
                'resources' => [
43
                    'empty_resource' => [
44
                        'constraints' => [],
45
                    ],
46
                ],
47
            ])
48
        );
49
50
        $resource = Resource::box([], $configurator);
51
52
        $resource->notYetImplementedMethod();
53
    }
54
55
    /**
56
     * @expectedException              Sensorario\Resources\Exceptions\NotAllowedKeyException
57
     * @expectedExceptionMessageRegExp #Key `.*::.*` is not allowed#
58
     */
59 View Code Duplication
    public function testNotAllowedFieldThroghRuntimeException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
    {
61
        $configurator = new Configurator(
62
            'empty_resource',
63
            new Container([
64
                'resources' => [
65
                    'empty_resource' => [
66
                        'constraints' => [
67
                            'allowedValues' => [
68
                                'name',
69
                                'surname',
70
                            ],
71
                        ],
72
                    ],
73
                ],
74
            ])
75
        );
76
77
        $resource = Resource::box([
0 ignored issues
show
Unused Code introduced by
$resource is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
            'name' => 'Simone',
79
            'surname' => 'Gentili',
80
            'not allowed' => 'foo',
81
        ], $configurator);
82
    }
83
84
    /**
85
     * @expectedException              Sensorario\Resources\Exceptions\PropertyException
86
     * @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set#
87
     */
88
    public function testMissingMandatoryFieldThroghRuntimeException()
89
    {
90
        $configurator = new Configurator(
91
            'empty_resource',
92
            new Container([
93
                'resources' => [
94
                    'empty_resource' => [
95
                        'constraints' => [
96
                            'mandatory' => [
97
                                'name',
98
                            ],
99
                        ],
100
                    ],
101
                ],
102
            ])
103
        );
104
105
        $resource = Resource::box([
0 ignored issues
show
Unused Code introduced by
$resource is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
            'surname' => 'Gentili',
107
        ], $configurator);
108
    }
109
110
    public function testMandatoryFieldsAreAuthomaticallyAllowed()
111
    {
112
        $configurator = new Configurator(
113
            'empty_resource',
114
            new Container([
115
                'resources' => [
116
                    'empty_resource' => [
117
                        'constraints' => [
118
                            'mandatory' => [
119
                                'name',
120
                                'surname',
121
                            ],
122
                        ],
123
                    ],
124
                ],
125
            ])
126
        );
127
128
        $resource = Resource::box([
129
            'name'    => 'Simone',
130
            'surname' => 'Gentili',
131
        ], $configurator);
132
133
        $this->assertEquals(
134
            'Simone',
135
            $resource->name()
136
        );
137
    }
138
139
    /**
140
     * @expectedException        Sensorario\Resources\Exceptions\PropertyNameEmptyException
141
     * @expectedExceptionMessage Oops! Property name requested is empty string!!
142
     */
143
    public function testExceptionMessageInCaseOfEmptyPropertyName()
144
    {
145
        $configurator = new Configurator(
146
            'empty_resource',
147
            new Container([
148
                'resources' => [
149
                    'empty_resource' => [
150
                        'constraints' => [
151
                            'mandatory' => [
152
                                'name',
153
                                'surname',
154
                            ],
155
                        ],
156
                    ],
157
                ],
158
            ])
159
        );
160
161
        $resource = Resource::box([
162
            'name'    => 'Simone',
163
            'surname' => 'Gentili',
164
        ], $configurator);
165
166
        $this->assertEquals(
167
            'Simone',
168
            $resource->get('')
169
        );
170
    }
171
172
    /**
173
     * @expectedException              Sensorario\Resources\Exceptions\FactoryMethodException
174
     * @expectedExceptionMessageRegExp #Invalid factory method#
175
     */
176
    public function testFactoryMethods()
177
    {
178
        Resource::invalidFactoryName();
179
    }
180
181
    public function testCanHaveDefaultValues()
182
    {
183
        $configurator = new Configurator(
184
            'empty_resource',
185
            new Container([
186
                'resources' => [
187
                    'empty_resource' => [
188
                        'constraints' => [
189
                            'mandatory' => [
190
                                'name',
191
                            ],
192
                            'defaults' => [
193
                                'name' => 'Firefox',
194
                            ],
195
                        ],
196
                    ],
197
                ],
198
            ])
199
        );
200
201
        $resource = Resource::box([], $configurator);
202
203
        $this->assertEquals(
204
            'Firefox',
205
            $resource->name()
206
        );
207
    }
208
209
    public function testPropertyExists()
210
    {
211
        $foo = Bar::box();
212
213
        $this->assertFalse(
214
            $foo->hasProperty('nonExistentProperty')
215
        );
216
    }
217
218
    /** @dataProvider propertiesProvider */
219
    public function testHasProperties($result, $properties)
220
    {
221
        $foo = UserCreationEvent::box([
222
            'type' => 'human',
223
            'username' => 'Sensorario',
224
        ]);
225
226
        $this->assertSame(
227
            $result,
228
            $foo->hasProperties($properties)
229
        );
230
    }
231
232
    public function propertiesProvider()
233
    {
234
        return [
235
            [false, ['buppa']],
236
            [true, ['type', 'username']],
237
        ];
238
    }
239
240
    public function testAllowAccessToProperties()
241
    {
242
        $foo = Bar::box([
243
            'name' => 'Firefox'
244
        ]);
245
246
        $this->assertEquals(
247
            'Firefox',
248
            $foo->get('name')
249
        );
250
    }
251
252
    public function testAllowAccessToPropertiesThroughDefaultValue()
253
    {
254
        $foo = Bar::box();
255
256
        $this->assertEquals(
257
            'Firefox',
258
            $foo->get('name')
259
        );
260
    }
261
262
    /**
263
     * @expectedException Sensorario\Resources\Exceptions\NoValuesException
264
     */
265
    public function testThroughExceptionWhenNoValuesProvided()
266
    {
267
        $foo = Bar::box();
268
        $foo->get('foo');
269
    }
270
271
    /**
272
     * @expectedException              Sensorario\Resources\Exceptions\AttributeTypeException
273
     * @expectedExceptionMessageRegExp #Attribute `.*` must be of type `array`#
274
     */
275
    public function testPropertyCouldBeAScalar()
276
    {
277
        SomeApiRequest::box([
278
            'fields' => 'not a scalar',
279
        ]);
280
    }
281
282
    /**
283
     * @expectedException              Sensorario\Resources\Exceptions\NotObjectTypeFoundException
284
     * @expectedExceptionMessageRegExp #Attribute `.*` must be an object of type DateTime#
285
     */
286
    public function testPropertyCouldBeTheRightnObject()
287
    {
288
        BirthDay::box([
289
            'date' => new DateInterval('P1D'),
290
        ]);
291
    }
292
293
    public function testPropertiesAccessor()
294
    {
295
        $aSpecificRule = [ 'custom-validator' => 'email' ];
0 ignored issues
show
Unused Code introduced by
$aSpecificRule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
296
297
        $configurator = new Configurator(
298
            'email-resource',
299
            new Container([
300
                'resources' => [
301
                    'email-resource' => [
302
                        'constraints' => [
303
                            'mandatory' => [
304
                                'name',
305
                            ],
306
                        ]
307
                    ],
308
                ],
309
            ])
310
        );
311
312
        $properties = [
313
            'name' => 'Simone',
314
        ];
315
316
        $resource = Resource::box($properties, $configurator);
317
318
        $this->assertEquals(
319
            $properties,
320
            $resource->properties()
321
        );
322
    }
323
324
    /**
325
     * @expectedException              Sensorario\Resources\Exceptions\PropertyNotSetException
326
     * @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set#
327
     */
328
    public function testWhenCondition()
329
    {
330
        MandatoryDependency::box([
331
            'foo' => 'bar',
332
            'mandatory_mello' => 'bar',
333
        ]);
334
    }
335
336
    public function testShouldNotFail()
337
    {
338
        MandatoryDependency::box([
339
            'foo' => 'bar',
340
        ]);
341
    }
342
343
    public function testResourcesComposition()
344
    {
345
        $composition = ComposedResource::box([
346
            'credentials' => Foo::box([
347
                'name' => 'Sam'
348
            ]),
349
        ]);
350
351
        $this->assertEquals([
352
            'credentials' => [
353
                'name' => 'Sam',
354
            ]
355
        ],
356
        $composition->properties()
357
    );
358
    }
359
360
    /**
361
     * @expectedException              Sensorario\Resources\Exceptions\PropertyException
362
     * @expectedExceptionMessageRegExp #When property `.*` has value `.*` also `.*` is mandatory#
363
     */
364
    public function testMandatoryValuesWhenPropertyAssumeAValue()
365
    {
366
        UserCreationEvent::box([
367
            'type' => 'guest',
368
        ]);
369
    }
370
371
    public function test()
372
    {
373
        UserCreationEvent::box([
374
            'type' => 'human',
375
            'username' => 'Sensorario',
376
        ]);
377
    }
378
379
    /**
380
     * @expectedException              Sensorario\Resources\Exceptions\PropertyWithoutRuleException
381
     * @expectedExceptionMessageRegExp #When property `.*` is an object class, must be defined in Resources::rules()#
382
     */
383
    public function testAnExceptionIsThrownIfAPropertyIsAnObjectButClassInNotDefinedInRuleMethod()
384
    {
385
        ResourceWithoutRules::box([
386
            'datetime' => new DateTime(),
387
        ]);
388
    }
389
390
    public function testDefaultValuesTwo()
391
    {
392
        $resource = new Resource(
393
            [],
394
            new ResourcesValidator()
395
        );
396
397
        $this->assertEquals(
398
            [],
399
            $resource->allowed()
400
        );
401
402
        $resource->applyConfiguration(
403
            new Configurator(
404
                'bar',
405
                new Container([
406
                    'resources' => [
407
                        'bar' => [
408
                            'constraints' => [
409
                                'allowed' => [ 'allowed_property_name' ],
410
                            ]
411
                        ],
412
                    ],
413
                ])
414
            )
415
        );
416
417
        $this->assertEquals(
418
            [ 'allowed_property_name' ],
419
            $resource->allowed('bar')
0 ignored issues
show
Unused Code introduced by
The call to Resource::allowed() has too many arguments starting with 'bar'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
420
        );
421
    }
422
423
    public function testDefaultValues()
424
    {
425
        $configurator = new Configurator(
426
            'foo',
427
            new Container([
428
                'resources' => [
429
                    'foo' => [
430
                        'constraints' => [
431
                            'mandatory' => [
432
                                'ciambella',
433
                            ],
434
                            'defaults' => [
435
                                'ciambella' => '42',
436
                            ],
437
                        ]
438
                    ],
439
                ],
440
            ])
441
        );
442
443
        $resource = Resource::box(
444
            [],
445
            $configurator
446
        );
447
448
        $this->assertEquals(
449
            '42',
450
            $resource->get('ciambella')
451
        );
452
    }
453
454
    public function testResourceShouldBeCreatedViaContainer()
455
    {
456
        $container = new Container([
457
            'resources' => [
458
                'foo' => [
459
                    'constraints' => [
460
                        'allowed' => [ 'allowed_property_name' ],
461
                    ]
462
                ],
463
                'unused_resource' => [
464
                    'constraints' => [
465
                        'allowed' => [ 'bar' ],
466
                    ]
467
                ],
468
            ],
469
        ]);
470
471
        $this->assertEquals(
472
            [ 'allowed_property_name' ],
473
            $container->allowed('foo')
474
        );
475
    }
476
477
    /**
478
     * @expectedException              Sensorario\Resources\Exceptions\PropertyNotSetException
479
     * @expectedExceptionMessageRegExp #Property `.*::.*` is mandatory but not set#
480
     */
481 View Code Duplication
    public function testDependentMandatoryProperties()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
482
    {
483
        $configurator = new Configurator(
484
            'foo',
485
            new Container([
486
                'resources' => [
487
                    'foo' => [
488
                        'constraints' => [
489
                            'allowed' => [
490
                                'bar',
491
                            ],
492
                            'mandatory' => [
493
                                'mandatory_property_name',
494
                                'foo' => [
495
                                    'when' => [
496
                                        'property' => 'bar',
497
                                        'condition' => 'is_present',
498
                                    ]
499
                                ],
500
                            ],
501
                        ]
502
                    ],
503
                ],
504
            ])
505
        );
506
507
        $properties = [
508
            'mandatory_property_name' => '42',
509
            'bar' => 'beer',
510
        ];
511
512
        Resource::box(
513
            $properties,
514
            $configurator
515
        );
516
    }
517
518 View Code Duplication
    public function testMandatoryConstraintsAreAutomaticallyAllowed()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
519
    {
520
        $container = new Container([
521
            'resources' => [
522
                'foo' => [
523
                    'constraints' => [
524
                        'mandatory' => [ 'mandatory_property' ],
525
                    ]
526
                ],
527
            ],
528
        ]);
529
530
        $this->assertEquals(
531
            [ 'mandatory_property' ],
532
            $container->allowed('foo')
533
        );
534
    }
535
536
    public function testPropertyType()
537
    {
538
        $configurator = new Configurator(
539
            'foo',
540
            new Container([
541
                'resources' => [
542
                    'foo' => [
543
                        'constraints' => [
544
                            'mandatory' => [ 'date' ],
545
                            'rules' => [ 'date' => [ 'object' => 'DateTime' ] ],
546
                        ]
547
                    ],
548
                ],
549
            ])
550
        );
551
552
        $properties = [
553
            'date' => new \DateTime(),
554
        ];
555
556
        Resource::box($properties, $configurator);
557
    }
558
559
    /**
560
     * @expectedException              Sensorario\Resources\Exceptions\UnexpectedValueException
561
     * @expectedExceptionMessageRegExp #Value `.*` is not allowed for key `.*`. Allowed values are:#
562
     */
563 View Code Duplication
    public function testAllowedValues()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
564
    {
565
        $configurator = new Configurator(
566
            'foo',
567
            new Container([
568
                'resources' => [
569
                    'foo' => [
570
                        'constraints' => [
571
                            'allowed' => [ 'user_type' ],
572
                            'allowedValues' => [
573
                                'user_type' => [
574
                                    4, 
575
                                    7,
576
                                ],
577
                            ],
578
                        ],
579
                    ],
580
                ],
581
            ])
582
        );
583
584
        $properties = [ 'user_type' => 3 ];
585
586
        Resource::box(
587
            $properties,
588
            $configurator
589
        );
590
    }
591
592
    public function testRewriteRulesWithCondition()
593
    {
594
        $configurator = new Configurator(
595
            'foo',
596
            new Container([
597
                'resources' => [
598
                    'foo' => [
599
                        'rewrite' => [
600
                            'width' => [
601
                                'set' => [
602
                                    'equals_to' => 'height',
603
                                ],
604
                                'when' => [
605
                                    'greater_than' => 'height',
606
                                ],
607
                            ],
608
                        ],
609
                        'constraints' => [
610
                            'allowed' => [
611
                                'width',
612
                                'height',
613
                            ],
614
                        ],
615
                    ], 
616
                ],
617
            ])
618
        );
619
        $properties = [
620
            'width'  => 3000,
621
            'height' => 400,
622
        ];
623
624
        $box = Resource::box(
625
            $properties,
626
            $configurator
627
        );
628
629
        $overwritternProperties = [
630
            'width'  => 400,
631
            'height' => 400,
632
        ];
633
634
        $overWrittenBox = Resource::box(
635
            $overwritternProperties,
636
            $configurator
637
        );
638
639
        $this->assertEquals(
640
            $overWrittenBox,
641
            $box
642
        );
643
    }
644
645
    /**
646
     * @expectedException              Sensorario\Resources\Exceptions\OutOfRangeException
647
     * @expectedExceptionMessageRegExp #Value `.*` is out of range: `.*`.#
648
     */
649 View Code Duplication
    public function testAcceptRangeOfValues()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
650
    {
651
        $configurator = new Configurator(
652
            'foo',
653
            new Container([
654
                'resources' => [
655
                    'foo' => [
656
                        'constraints' => [
657
                            'allowedRanges' => [
658
                                'age' => [
659
                                    'more_than' => 3,
660
                                    'less_than' => 42,
661
                                ],
662
                            ],
663
                            'allowed' => [
664
                                'age'
665
                            ],
666
                        ],
667
                    ],
668
                ],
669
            ])
670
        );
671
672
        Resource::box(
673
            [ 'age' => 2 ],
674
            $configurator
675
        );
676
    }
677
678
    public function testAllResourcesInheritGlobalAllowingConfiguration()
679
    {
680
        $configurator = new Configurator(
681
            'foo',
682
            new Container([
683
                'globals' => [
684
                    'allowed' => [
685
                        'width',
686
                        'height',
687
                    ],
688
                ],
689
                'resources' => [
690
                    'foo' => [
691
                        'constraints' => [
692
                            'allowed' => [
693
                                'foo_size',
694
                            ],
695
                        ],
696
                    ], 
697
                    'bar' => [
698
                        'constraints' => [
699
                            'allowed' => [
700
                                'bar_size',
701
                            ],
702
                        ],
703
                    ], 
704
                ],
705
            ])
706
        );
707
708
        $resource = Resource::box(
709
            [],
710
            $configurator
711
        );
712
713
        $this->assertEquals(
714
            ['foo_size', 'width', 'height'],
715
            $resource->allowed()
716
        );
717
    }
718
719
    /**
720
     * @expectedException Sensorario\Resources\Exceptions\PropertyException
721
     */
722 View Code Duplication
    public function testHasMandatoryPropertiesWhenAnotherOneHasAParticularValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
723
    {
724
        $configurator = new Configurator(
725
            'foo',
726
            new Container([
727
                'resources' => [
728
                    'foo' => [
729
                        'constraints' => [
730
                            'allowed' => [
731
                                'bar',
732
                            ],
733
                            'mandatory' => [
734
                                'mandatory_property_name',
735
                                'foo' => [
736
                                    'when' => [
737
                                        'property' => 'bar',
738
                                        'has_value' => '42',
739
                                    ]
740
                                ],
741
                            ],
742
                        ]
743
                    ],
744
                ],
745
            ])
746
        );
747
748
        $properties = [
749
            'bar' => '42',
750
        ];
751
752
        Resource::box(
753
            $properties,
754
            $configurator
755
        );
756
    }
757
758
    /**
759
     * @expectedException Sensorario\Resources\Exceptions\EmailException
760
     */
761
    public function testEmailValidationFails()
762
    {
763
        $configurator = new Configurator(
764
            'email-resource',
765
            new Container([
766
                'resources' => [
767
                    'email-resource' => [
768
                        'constraints' => [
769
                            'mandatory' => [
770
                                'first-email',
771
                            ],
772
                            'rules' => [
773
                                'first-email' => [ 'custom-validator' => 'email' ],
774
                            ],
775
                        ]
776
                    ],
777
                ],
778
            ])
779
        );
780
781
        $properties = [
782
            'first-email' => 'invalid email',
783
        ];
784
785
        Resource::box($properties, $configurator);
786
    }
787
788
    /**
789
     * @expectedException              Sensorario\Resources\Exceptions\WrongPropertyValueException
790
     * @expectedExceptionMessageRegExp #Property .* must be an integer!#
791
     */
792
    public function testIntegersCanBeDefinedWithNumberRule()
793
    {
794
        $configurator = new Configurator(
795
            'type-with-number',
796
            new Container([
797
                'resources' => [
798
                    'type-with-number' => [
799
                        'constraints' => [
800
                            'mandatory' => [ 'a-magic-number' ],
801
                            'rules' => [ 'a-magic-number' => [ 'scalar' => 'integer' ] ],
802
                        ]
803
                    ],
804
                ],
805
            ])
806
        );
807
808
        $properties = [
809
            'a-magic-number' => '42',
810
        ];
811
812
        $resource = Resource::box($properties, $configurator);
0 ignored issues
show
Unused Code introduced by
$resource is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
813
    }
814
815
    public function testEmailValidation()
816
    {
817
        $configurator = new Configurator(
818
            'email-resource',
819
            new Container([
820
                'resources' => [
821
                    'email-resource' => [
822
                        'constraints' => [
823
                            'mandatory' => [
824
                                'first-email',
825
                            ],
826
                            'rules' => [
827
                                'first-email' => [ 'custom-validator' => 'email' ],
828
                            ],
829
                        ]
830
                    ],
831
                ],
832
            ])
833
        );
834
835
        $properties = [
836
            'first-email' => '[email protected]',
837
        ];
838
839
        $resource = Resource::box($properties, $configurator);
840
841
        $this->assertEquals(
842
            '[email protected]',
843
            $resource->get('first-email')
844
        );
845
    }
846
847
    /** @dataProvider rules */
848 View Code Duplication
    public function testRulesKnowsIfRuleIsDefinedOrNot($expectation, $ruleName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
849
    {
850
        $configurator = new Configurator(
851
            'email-resource',
852
            new Container([
853
                'resources' => [
854
                    'email-resource' => [
855
                        'constraints' => [
856
                            'mandatory' => [
857
                                'first-email',
858
                            ],
859
                            'rules' => [
860
                                'first-email' => [ 'custom-validator' => 'email' ],
861
                            ],
862
                        ]
863
                    ],
864
                ],
865
            ])
866
        );
867
868
        $properties = [
869
            'first-email' => '[email protected]',
870
        ];
871
872
        $resource = Resource::box($properties, $configurator);
873
874
        $this->assertEquals(
875
            $expectation,
876
            $resource->isRuleDefinedFor($ruleName)
877
        );
878
    }
879
880
    public function rules()
881
    {
882
        return [
883
            [true, 'first-email'],
884
            [false, 'non-existent-field-name'],
885
        ];
886
    }
887
888 View Code Duplication
    public function testProvideRule()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
889
    {
890
        $aSpecificRule = [ 'custom-validator' => 'email' ];
891
892
        $configurator = new Configurator(
893
            'email-resource',
894
            new Container([
895
                'resources' => [
896
                    'email-resource' => [
897
                        'constraints' => [
898
                            'mandatory' => [
899
                                'first-email',
900
                            ],
901
                            'rules' => [
902
                                'first-email' => $aSpecificRule,
903
                            ],
904
                        ]
905
                    ],
906
                ],
907
            ])
908
        );
909
910
        $properties = [
911
            'first-email' => '[email protected]',
912
        ];
913
914
        $resource = Resource::box($properties, $configurator);
915
916
        $this->assertEquals(
917
            $aSpecificRule,
918
            $resource->getRule('first-email')->asArray()
919
        );
920
    }
921
922
    /**
923
     * @expectedException Sensorario\Resources\Exceptions\PropertyWithoutRuleException
924
     * @expectedExceptionMessage Property date is an object but is not defined in rules
925
     */
926
    public function testUndefinedObject()
927
    {
928
        UndefinedObject::box([
929
            'date' => new \stdClass(),
930
        ]);
931
    }
932
}
933