3
3
namespace PHPModelGenerator \Tests \ComposedValue ;
4
4
5
5
use PHPModelGenerator \Exception \FileSystemException ;
6
+ use PHPModelGenerator \Exception \Number \MultipleOfException ;
6
7
use PHPModelGenerator \Exception \RenderException ;
7
8
use PHPModelGenerator \Exception \SchemaException ;
8
9
use PHPModelGenerator \Model \GeneratorConfiguration ;
@@ -39,21 +40,69 @@ public function conditionalKeywordsDataProvider(): array
39
40
];
40
41
}
41
42
43
+ /**
44
+ * @dataProvider validConditionalPropertyDefinitionDataProvider
45
+ * @param int $value
46
+ */
47
+ public function testConditionalPropertyDefinition (int $ value ): void
48
+ {
49
+ $ className = $ this ->generateClassFromFile ('ConditionalPropertyDefinition.json ' );
50
+
51
+ $ object = new $ className (['property ' => $ value ]);
52
+ $ this ->assertSame ($ value , $ object ->getProperty ());
53
+ }
54
+
55
+ public function validConditionalPropertyDefinitionDataProvider (): array
56
+ {
57
+ return [
58
+ 'zero ' => [0 ],
59
+ 'negative multiple of else ' => [-30 ],
60
+ 'positive multiple of else 1 ' => [30 ],
61
+ 'positive multiple of else 2 ' => [60 ],
62
+ 'exactly on minimum ' => [100 ],
63
+ 'positive multiple of then ' => [150 ],
64
+ ];
65
+ }
66
+
67
+ /**
68
+ * @dataProvider invalidConditionalPropertyDefinitionDataProvider
69
+ * @param int $value
70
+ */
71
+ public function testInvalidConditionalPropertyDefinition (int $ value ): void
72
+ {
73
+ $ this ->expectException (MultipleOfException::class);
74
+ $ this ->expectExceptionMessage ("Value for property must be a multiple of " );
75
+
76
+ $ className = $ this ->generateClassFromFile ('ConditionalPropertyDefinition.json ' );
77
+ new $ className (['property ' => $ value ]);
78
+ }
79
+
80
+ public function invalidConditionalPropertyDefinitionDataProvider (): array
81
+ {
82
+ return [
83
+ 'invalid negative ' => [-50 ],
84
+ 'invalid positive else ' => [50 ],
85
+ 'invalid positive then ' => [120 ],
86
+ ];
87
+ }
88
+
42
89
/**
43
90
* @dataProvider validConditionalObjectPropertyDataProvider
44
91
*
92
+ * @param string $schemaFile
45
93
* @param GeneratorConfiguration $configuration
46
- * @param string $streetAddress
47
- * @param string $country
48
- * @param string $postalCode
94
+ * @param string|null $streetAddress
95
+ * @param string|null $country
96
+ * @param string|null $postalCode
49
97
*/
50
98
public function testConditionalObjectProperty (
99
+ string $ schemaFile ,
51
100
GeneratorConfiguration $ configuration ,
52
101
?string $ streetAddress ,
53
102
?string $ country ,
54
103
?string $ postalCode
55
104
): void {
56
- $ className = $ this ->generateClassFromFile (' ConditionalObjectProperty.json ' , $ configuration );
105
+ $ className = $ this ->generateClassFromFile ($ schemaFile , $ configuration );
57
106
58
107
$ object = new $ className ([
59
108
'street_address ' => $ streetAddress ,
@@ -66,39 +115,55 @@ public function testConditionalObjectProperty(
66
115
$ this ->assertSame ($ postalCode , $ object ->getPostalCode ());
67
116
}
68
117
118
+ public function objectLevelConditionalSchemaDataProvider (): array
119
+ {
120
+ return [
121
+ 'Object top level conditional composition ' => ['ConditionalObjectProperty.json ' ],
122
+ 'Conditional composition nested in another composition ' => ['NestedIfInComposition.json ' ],
123
+ ];
124
+ }
125
+
69
126
public function validConditionalObjectPropertyDataProvider (): array
70
127
{
71
128
return $ this ->combineDataProvider (
72
- $ this ->validationMethodDataProvider (),
73
- [
74
- 'not provided postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , null ],
75
- 'USA postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , '20500 ' ],
76
- 'Canada postal code ' => ['24 Sussex Drive ' , 'Canada ' , 'K1M 1M4 ' ],
77
- ]
129
+ $ this ->objectLevelConditionalSchemaDataProvider (),
130
+ $ this ->combineDataProvider (
131
+ $ this ->validationMethodDataProvider (),
132
+ [
133
+ 'not provided postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , null ],
134
+ 'USA postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , '20500 ' ],
135
+ 'Canada postal code ' => ['24 Sussex Drive ' , 'Canada ' , 'K1M 1M4 ' ],
136
+ ]
137
+ )
78
138
);
79
139
}
80
140
81
141
/**
82
142
* @dataProvider invalidConditionalObjectPropertyDataProvider
83
143
*
144
+ * @param string $schemaFile
84
145
* @param GeneratorConfiguration $configuration
85
- * @param string $streetAddress
86
- * @param string $country
87
- * @param string $postalCode
146
+ * @param string|null $streetAddress
147
+ * @param string|null $country
148
+ * @param string|null $postalCode
88
149
*
89
150
* @throws FileSystemException
90
151
* @throws RenderException
91
152
* @throws SchemaException
92
153
*/
93
154
public function testInvalidConditionalObjectPropertyThrowsAnException (
155
+ string $ schemaFile ,
94
156
GeneratorConfiguration $ configuration ,
95
157
?string $ streetAddress ,
96
158
?string $ country ,
97
159
?string $ postalCode
98
160
): void {
99
- $ this ->expectValidationErrorRegExp ($ configuration , '/postal_code doesn \'t match pattern .*/ ' );
161
+ $ this ->expectValidationErrorRegExp (
162
+ $ configuration ,
163
+ '/(Invalid value for .*? declined by composition constraint|postal_code doesn \'t match pattern .*)/ '
164
+ );
100
165
101
- $ className = $ this ->generateClassFromFile (' ConditionalObjectProperty.json ' , $ configuration );
166
+ $ className = $ this ->generateClassFromFile ($ schemaFile , $ configuration );
102
167
103
168
new $ className ([
104
169
'street_address ' => $ streetAddress ,
@@ -110,13 +175,16 @@ public function testInvalidConditionalObjectPropertyThrowsAnException(
110
175
public function invalidConditionalObjectPropertyDataProvider (): array
111
176
{
112
177
return $ this ->combineDataProvider (
113
- $ this ->validationMethodDataProvider (),
114
- [
115
- 'empty provided postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , '' ],
116
- 'Canadian postal code for USA ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , 'K1M 1M4 ' ],
117
- 'USA postal code for Canada ' => ['24 Sussex Drive ' , 'Canada ' , '20500 ' ],
118
- 'Unmatching postal code for both ' => ['24 Sussex Drive ' , 'Canada ' , 'djqwWDJId8juw9duq9 ' ],
119
- ]
178
+ $ this ->objectLevelConditionalSchemaDataProvider (),
179
+ $ this ->combineDataProvider (
180
+ $ this ->validationMethodDataProvider (),
181
+ [
182
+ 'empty provided postal code ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , '' ],
183
+ 'Canadian postal code for USA ' => ['1600 Pennsylvania Avenue NW ' , 'USA ' , 'K1M 1M4 ' ],
184
+ 'USA postal code for Canada ' => ['24 Sussex Drive ' , 'Canada ' , '20500 ' ],
185
+ 'Unmatching postal code for both ' => ['24 Sussex Drive ' , 'Canada ' , 'djqwWDJId8juw9duq9 ' ],
186
+ ]
187
+ )
120
188
);
121
189
}
122
190
0 commit comments