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 PropertiesTest extends ConstraintTestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @dataProvider absentKeywordProvider |
19
|
|
|
* |
20
|
|
|
* @param $schemaName |
21
|
|
|
*/ |
22
|
|
|
public function testNormalizeSetsAbsentKeywordsToEmptySchema($schemaName) |
23
|
|
|
{ |
24
|
|
|
$schema = $this->loadSchema($schemaName); |
25
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
26
|
|
|
$this->assertObjectHasAttribute('properties', $schema); |
27
|
|
|
$this->assertObjectHasAttribute('additionalProperties', $schema); |
28
|
|
|
$this->assertObjectHasAttribute('patternProperties', $schema); |
29
|
|
|
$this->assertEquals(new \stdClass(), $schema->properties); |
30
|
|
|
$this->assertEquals(new \stdClass(), $schema->additionalProperties); |
31
|
|
|
$this->assertEquals(new \stdClass(), $schema->patternProperties); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testNormalizeSetsAdditionalPropertiesToEmptySchemaIfSetToTrue() |
35
|
|
|
{ |
36
|
|
|
$schema = $this->loadSchema('valid/additionalProperties-set-to-true'); |
37
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
38
|
|
|
$this->assertEquals(new \stdClass(), $schema->additionalProperties); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testNormalizeThrowsIfPropertiesIsNotAnObject() |
42
|
|
|
{ |
43
|
|
|
$this->expectConstraintException('InvalidTypeException', '/properties'); |
44
|
|
|
$schema = $this->loadSchema('invalid/properties-not-object'); |
45
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testNormalizeThrowsIfPropertiesPropertyValueIsNotAnObject() |
49
|
|
|
{ |
50
|
|
|
$this->expectConstraintException('InvalidTypeException', '/properties/foo'); |
51
|
|
|
$schema = $this->loadSchema('invalid/properties-property-value-not-object'); |
52
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testNormalizeEnsuresPropertiesPropertyIsAValidSchema() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$schema = $this->loadSchema('valid/properties-not-empty'); |
58
|
|
|
$walker = $this->mockWalker(); |
59
|
|
|
$walker->expects($this->at(0)) |
60
|
|
|
->method('parseSchema') |
61
|
|
|
->with($schema->properties->foo); |
62
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testNormalizeThrowsIfAdditionalPropertiesIsNotBooleanOrObject() |
66
|
|
|
{ |
67
|
|
|
$this->expectConstraintException('InvalidTypeException', '/additionalProperties'); |
68
|
|
|
$schema = $this->loadSchema('invalid/additionalProperties-not-object-or-boolean'); |
69
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testNormalizeEnsuresAdditionalPropertiesAsObjectIsAValidSchema() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$schema = $this->loadSchema('valid/additionalProperties-as-object'); |
75
|
|
|
$walker = $this->mockWalker(); |
76
|
|
|
$walker->expects($this->at(0)) |
77
|
|
|
->method('parseSchema') |
78
|
|
|
->with($schema->additionalProperties); |
79
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testNormalizeThrowsIfPatternPropertiesIsNotAnObject() |
83
|
|
|
{ |
84
|
|
|
$this->expectConstraintException('InvalidTypeException', '/patternProperties'); |
85
|
|
|
$schema = $this->loadSchema('invalid/patternProperties-not-object'); |
86
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testNormalizeThrowsIfPatternPropertiesPropertyNameIsNotAValidRegex() |
90
|
|
|
{ |
91
|
|
|
$this->expectConstraintException('InvalidRegexException', '/patternProperties//#this**not---a][valid[regex'); |
92
|
|
|
$schema = $this->loadSchema('invalid/patternProperties-invalid-regex'); |
93
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testNormalizeThrowsIfPatternPropertiesPropertyValueIsNotAnObject() |
97
|
|
|
{ |
98
|
|
|
$this->expectConstraintException('InvalidTypeException', '/patternProperties/regex'); |
99
|
|
|
$schema = $this->loadSchema('invalid/patternProperties-property-value-not-object'); |
100
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $this->mockWalker()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function testNormalizeEnsuresPatternPropertiesPropertyValueIsAValidSchema() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$schema = $this->loadSchema('valid/patternProperties-not-empty'); |
106
|
|
|
$walker = $this->mockWalker(); |
107
|
|
|
$walker->expects($this->at(1)) |
108
|
|
|
->method('parseSchema') |
109
|
|
|
->with($schema->patternProperties->regex); |
110
|
|
|
$this->getConstraint()->normalize($schema, new Context(), $walker); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function testDelimitersInPatternPropertiesAreEscapedBeforeTestingRegex() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$schema = $this->loadSchema('valid/patternProperties-with-slash'); |
116
|
|
|
$context = new Context(); |
117
|
|
|
$this->getConstraint()->normalize($schema, $context, $this->mockWalker()); |
118
|
|
|
$this->assertEquals(0, $context->countViolations()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getConstraint() |
122
|
|
|
{ |
123
|
|
|
return new PropertiesConstraint(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getCaseFileNames() |
127
|
|
|
{ |
128
|
|
|
return ['properties']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function absentKeywordProvider() |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
['valid/properties-not-present'], |
135
|
|
|
['valid/additionalProperties-not-present'], |
136
|
|
|
['valid/patternProperties-not-present'], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.