14
14
class ComposedOneOfTest extends AbstractPHPModelGeneratorTest
15
15
{
16
16
/**
17
- * @dataProvider oneOfSchemaFileDataProvider
17
+ * @dataProvider propertyLevelOneOfSchemaFileDataProvider
18
18
*
19
19
* @param string $schema
20
20
*/
21
- public function testNotProvidedOneOfTypePropertyThrowsAnException (string $ schema ): void
21
+ public function testNotProvidedPropertyLevelOneOfIsValid (string $ schema ): void
22
22
{
23
- $ this ->expectException (InvalidArgumentException::class);
24
-
25
23
$ className = $ this ->generateObjectFromFile ($ schema );
26
24
27
- new $ className ([]);
25
+ $ object = new $ className ([]);
26
+ $ this ->assertNull ($ object ->getProperty ());
28
27
}
29
28
30
- public function oneOfSchemaFileDataProvider (): array
29
+ public function propertyLevelOneOfSchemaFileDataProvider (): array
31
30
{
32
31
return [
33
32
'OneOfType.json ' => ['OneOfType.json ' ],
34
33
'ExtendedPropertyDefinition.json ' => ['ExtendedPropertyDefinition.json ' ],
35
34
'ReferencedObjectSchema.json ' => ['ReferencedObjectSchema.json ' ],
35
+ ];
36
+ }
37
+
38
+ /**
39
+ * @dataProvider objectLevelOneOfSchemaFileDataProvider
40
+ *
41
+ * @param string $schema
42
+ */
43
+ public function testNotProvidedObjectLevelOneOfThrowsAnException (string $ schema ): void
44
+ {
45
+ $ this ->expectException (InvalidArgumentException::class);
46
+ $ this ->expectExceptionMessageRegExp ('/^Invalid value for (.*?) declined by composition constraint$/ ' );
47
+
48
+ $ className = $ this ->generateObjectFromFile ($ schema );
49
+
50
+ new $ className ([]);
51
+ }
52
+
53
+ public function objectLevelOneOfSchemaFileDataProvider (): array
54
+ {
55
+ return [
36
56
'ObjectLevelComposition.json ' => ['ObjectLevelComposition.json ' ],
57
+ 'ObjectLevelCompositionRequired.json ' => ['ObjectLevelCompositionRequired.json ' ],
37
58
];
38
59
}
39
60
40
61
/**
41
62
* @dataProvider validPropertyTypeDataProvider
63
+ * @dataProvider nullDataProvider
42
64
*
43
65
* @param $propertyValue
44
66
*/
@@ -84,10 +106,44 @@ public function invalidPropertyTypeDataProvider(): array
84
106
'float ' => [0.92 ],
85
107
'array ' => [[]],
86
108
'object ' => [new stdClass ()],
87
- 'null ' => [null ],
88
109
];
89
110
}
90
111
112
+
113
+ /**
114
+ * @dataProvider validPropertyTypeDataProvider
115
+ *
116
+ * @param $propertyValue
117
+ */
118
+ public function testValidProvidedRequiredOneOfTypePropertyIsValid ($ propertyValue ): void
119
+ {
120
+ $ className = $ this ->generateObjectFromFile ('OneOfTypeRequired.json ' );
121
+
122
+ $ object = new $ className (['property ' => $ propertyValue ]);
123
+ $ this ->assertSame ($ propertyValue , $ object ->getProperty ());
124
+ }
125
+
126
+ /**
127
+ * @dataProvider invalidPropertyTypeDataProvider
128
+ * @dataProvider nullDataProvider
129
+ *
130
+ * @param $propertyValue
131
+ */
132
+ public function testInvalidProvidedRequiredOneOfTypePropertyThrowsAnException ($ propertyValue ): void
133
+ {
134
+ $ this ->expectException (InvalidArgumentException::class);
135
+ $ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
136
+
137
+ $ className = $ this ->generateObjectFromFile ('OneOfTypeRequired.json ' );
138
+
139
+ new $ className (['property ' => $ propertyValue ]);
140
+ }
141
+
142
+ public function nullDataProvider (): array
143
+ {
144
+ return ['null ' => [null ]];
145
+ }
146
+
91
147
/**
92
148
* @dataProvider validExtendedPropertyDataProvider
93
149
*
@@ -99,7 +155,7 @@ public function testExtendedPropertyDefinitionWithValidValues($propertyValue): v
99
155
100
156
$ object = new $ className (['property ' => $ propertyValue ]);
101
157
// cast expected to float as an int is casted to an float internally for a number property
102
- $ this ->assertSame (( float ) $ propertyValue , $ object ->getProperty ());
158
+ $ this ->assertSame (is_int ( $ propertyValue ) ? ( float ) $ propertyValue : $ propertyValue , $ object ->getProperty ());
103
159
}
104
160
105
161
public function validExtendedPropertyDataProvider (): array
@@ -108,7 +164,7 @@ public function validExtendedPropertyDataProvider(): array
108
164
'int 12 ' => [12 ],
109
165
'float 12. ' => [12. ],
110
166
'int 15 ' => [15 ],
111
- // TODO 'null' => [null],
167
+ 'null ' => [null ],
112
168
];
113
169
}
114
170
@@ -155,13 +211,14 @@ public function testMatchingObjectPropertyWithReferencedSchemaIsValid($propertyV
155
211
{
156
212
$ className = $ this ->generateObjectFromFile ('ReferencedObjectSchema.json ' );
157
213
158
- $ object = new $ className (['person ' => $ propertyValue ]);
159
- $ this ->assertSame ($ propertyValue , $ object ->getPerson ());
214
+ $ object = new $ className (['property ' => $ propertyValue ]);
215
+ $ this ->assertSame ($ propertyValue , $ object ->getProperty ());
160
216
}
161
217
162
218
public function objectPropertyWithReferencedSchemaDataProvider (): array
163
219
{
164
220
return [
221
+ 'null ' => [null ],
165
222
'string matching required length ' => ['Hanne ' ],
166
223
'Matching object ' => [['name ' => 'Ha ' , 'age ' => 42 ]],
167
224
];
@@ -175,17 +232,16 @@ public function objectPropertyWithReferencedSchemaDataProvider(): array
175
232
public function testNotMatchingObjectPropertyWithReferencedSchemaThrowsAnException ($ propertyValue ): void
176
233
{
177
234
$ this ->expectException (InvalidArgumentException::class);
178
- $ this ->expectExceptionMessage ('Invalid value for person declined by composition constraint ' );
235
+ $ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
179
236
180
237
$ className = $ this ->generateObjectFromFile ('ReferencedObjectSchema.json ' );
181
238
182
- new $ className (['person ' => $ propertyValue ]);
239
+ new $ className (['property ' => $ propertyValue ]);
183
240
}
184
241
185
242
public function invalidObjectPropertyWithReferencedSchemaDataProvider (): array
186
243
{
187
244
return [
188
- 'null ' => [null ],
189
245
'int ' => [0 ],
190
246
'float ' => [0.92 ],
191
247
'bool ' => [true ],
0 commit comments