1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the JVal package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JVal\Constraint; |
11
|
|
|
|
12
|
|
|
use JVal\Context; |
13
|
|
|
use JVal\Testing\ConstraintTestCase; |
14
|
|
|
|
15
|
|
|
class ItemsConstraintTest extends ConstraintTestCase |
16
|
|
|
{ |
17
|
|
|
public function testNormalizeSetsItemsToEmptySchemaIfNotPresent() |
18
|
|
|
{ |
19
|
|
|
$schema = $this->loadSchema('valid/items-not-present'); |
20
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
21
|
|
|
$this->assertObjectHasAttribute('items', $schema); |
22
|
|
|
$this->assertEquals(new \stdClass(), $schema->items); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testNormalizeSetsAdditionalItemsToEmptySchemaIfNotPresent() |
26
|
|
|
{ |
27
|
|
|
$schema = $this->loadSchema('valid/additionalItems-not-present'); |
28
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
29
|
|
|
$this->assertObjectHasAttribute('additionalItems', $schema); |
30
|
|
|
$this->assertEquals(new \stdClass(), $schema->additionalItems); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testNormalizeSetsAdditionalItemsToEmptySchemaIfSetToTrue() |
34
|
|
|
{ |
35
|
|
|
$schema = $this->loadSchema('valid/additionalItems-set-to-true'); |
36
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
37
|
|
|
$this->assertEquals(new \stdClass(), $schema->additionalItems); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testNormalizeThrowsIfItemsIsNotObjectOrArray() |
41
|
|
|
{ |
42
|
|
|
$this->expectConstraintException('InvalidTypeException', '/items'); |
43
|
|
|
$schema = $this->loadSchema('invalid/items-not-object-or-array'); |
44
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testNormalizeEnsuresItemsObjectIsAValidSchema() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$schema = $this->loadSchema('valid/items-object'); |
50
|
|
|
$walker = $this->mockWalker(); |
51
|
|
|
$walker->expects($this->at(0)) |
52
|
|
|
->method('parseSchema') |
53
|
|
|
->with($schema->items); |
54
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testNormalizeThrowsIfItemsElementIsNotObject() |
58
|
|
|
{ |
59
|
|
|
$this->expectConstraintException('InvalidTypeException', '/items/1'); |
60
|
|
|
$schema = $this->loadSchema('invalid/items-element-not-object'); |
61
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testNormalizeEnsuresItemsElementIsAValidSchema() |
65
|
|
|
{ |
66
|
|
|
$schema = $this->loadSchema('valid/items-array'); |
67
|
|
|
$walker = $this->mockWalker(); |
68
|
|
|
$walker->expects($this->at(1)) |
69
|
|
|
->method('parseSchema') |
70
|
|
|
->with($schema->items[0]); |
71
|
|
|
$walker->expects($this->at(2)) |
72
|
|
|
->method('parseSchema') |
73
|
|
|
->with($schema->items[1]); |
74
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testNormalizeThrowsIfAdditionalItemsIsNotBooleanOrObject() |
78
|
|
|
{ |
79
|
|
|
$this->expectConstraintException('InvalidTypeException', '/additionalItems'); |
80
|
|
|
$schema = $this->loadSchema('invalid/additionalItems-not-object-or-boolean'); |
81
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testNormalizeEnsuresAdditionalItemsAsObjectIsAValidSchema() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$schema = $this->loadSchema('valid/additionalItems-object'); |
87
|
|
|
$walker = $this->mockWalker(); |
88
|
|
|
$walker->expects($this->at(1)) |
89
|
|
|
->method('parseSchema') |
90
|
|
|
->with($schema->additionalItems); |
91
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getConstraint() |
95
|
|
|
{ |
96
|
|
|
return new ItemsConstraint(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function getCaseFileNames() |
100
|
|
|
{ |
101
|
|
|
return ['items']; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.