10
10
use PHPModelGenerator \Exception \Object \MaxPropertiesException ;
11
11
use PHPModelGenerator \Exception \String \PatternException ;
12
12
use PHPModelGenerator \Model \GeneratorConfiguration ;
13
+ use PHPModelGenerator \Model \Property \PropertyInterface ;
14
+ use PHPModelGenerator \Model \Schema ;
13
15
use PHPModelGenerator \ModelGenerator ;
16
+ use PHPModelGenerator \SchemaProcessor \Hook \SetterAfterValidationHookInterface ;
17
+ use PHPModelGenerator \SchemaProcessor \Hook \SetterBeforeValidationHookInterface ;
14
18
use PHPModelGenerator \SchemaProcessor \PostProcessor \PopulatePostProcessor ;
19
+ use PHPModelGenerator \SchemaProcessor \PostProcessor \PostProcessorInterface ;
15
20
use PHPModelGenerator \Tests \AbstractPHPModelGeneratorTest ;
16
21
17
22
class PopulatePostProcessorTest extends AbstractPHPModelGeneratorTest
@@ -20,7 +25,7 @@ public function setUp(): void
20
25
{
21
26
parent ::setUp ();
22
27
23
- $ this ->modifyModelGenerator = function (ModelGenerator $ generator ) {
28
+ $ this ->modifyModelGenerator = function (ModelGenerator $ generator ): void {
24
29
$ generator ->addPostProcessor (new PopulatePostProcessor ());
25
30
};
26
31
}
@@ -145,4 +150,104 @@ public function invalidPopulateDataProvider(): array
145
150
],
146
151
];
147
152
}
153
+
154
+ public function testSetterBeforeValidationHookInsidePopulateIsResolved (): void
155
+ {
156
+ $ this ->modifyModelGenerator = function (ModelGenerator $ modelGenerator ): void {
157
+ $ modelGenerator ->addPostProcessor (new PopulatePostProcessor ());
158
+ $ modelGenerator ->addPostProcessor (new class () implements PostProcessorInterface {
159
+ public function process (Schema $ schema , GeneratorConfiguration $ generatorConfiguration ): void
160
+ {
161
+ $ schema ->addSchemaHook (new class () implements SetterBeforeValidationHookInterface {
162
+ public function getCode (PropertyInterface $ property ): string
163
+ {
164
+ return $ property ->getName () === 'age '
165
+ ? 'throw new \Exception("SetterBeforeValidationHook"); '
166
+ : '' ;
167
+ }
168
+ });
169
+ }
170
+ });
171
+ };
172
+
173
+ $ className = $ this ->generateClassFromFile ('BasicSchema.json ' );
174
+
175
+ $ object = new $ className (['name ' => 'Albert ' , 'age ' => 35 ]);
176
+ $ object ->populate (['name ' => 'Hannes ' ]);
177
+
178
+ $ this ->expectException (Exception::class);
179
+ $ this ->expectExceptionMessage ("SetterBeforeValidationHook " );
180
+
181
+ $ object ->populate (['age ' => 40 ]);
182
+ }
183
+
184
+ /**
185
+ * @dataProvider setterAfterValidationHookDataProvider
186
+ *
187
+ * @param string|null $expectedException
188
+ * @param string|null $expectedExceptionMessage
189
+ * @param array $populateValues
190
+ */
191
+ public function testSetterAfterValidationHookInsidePopulateIsResolved (
192
+ ?string $ expectedException ,
193
+ ?string $ expectedExceptionMessage ,
194
+ array $ populateValues
195
+ ): void
196
+ {
197
+ $ this ->modifyModelGenerator = function (ModelGenerator $ modelGenerator ): void {
198
+ $ modelGenerator ->addPostProcessor (new PopulatePostProcessor ());
199
+ $ modelGenerator ->addPostProcessor (new class () implements PostProcessorInterface {
200
+ public function process (Schema $ schema , GeneratorConfiguration $ generatorConfiguration ): void
201
+ {
202
+ $ schema ->addSchemaHook (new class () implements SetterAfterValidationHookInterface {
203
+ public function getCode (PropertyInterface $ property ): string
204
+ {
205
+ return $ property ->getName () === 'age '
206
+ ? 'throw new \Exception("SetterAfterValidationHook"); '
207
+ : '' ;
208
+ }
209
+ });
210
+ }
211
+ });
212
+ };
213
+
214
+ $ className = $ this ->generateClassFromFile ('BasicSchema.json ' );
215
+
216
+ $ object = new $ className (['name ' => 'Albert ' , 'age ' => 35 ]);
217
+
218
+ if ($ expectedException ) {
219
+ $ this ->expectException ($ expectedException );
220
+ $ this ->expectExceptionMessage ($ expectedExceptionMessage );
221
+ } else {
222
+ $ this ->expectNotToPerformAssertions ();
223
+ }
224
+
225
+ $ object ->populate ($ populateValues );
226
+ }
227
+
228
+ public function setterAfterValidationHookDataProvider (): array
229
+ {
230
+ return [
231
+ 'update not hooked value valid ' => [
232
+ null ,
233
+ null ,
234
+ ['name ' => 'Hannes ' ],
235
+ ],
236
+ 'update not hooked value invalid ' => [
237
+ InvalidTypeException::class,
238
+ 'Invalid type for name. Requires string, got boolean ' ,
239
+ ['name ' => false ],
240
+ ],
241
+ 'update hooked value valid ' => [
242
+ Exception::class,
243
+ 'SetterAfterValidationHook ' ,
244
+ ['age ' => 40 ],
245
+ ],
246
+ 'update hooked value invalid ' => [
247
+ InvalidTypeException::class,
248
+ 'Invalid type for age. Requires int, got boolean ' ,
249
+ ['age ' => false ],
250
+ ],
251
+ ];
252
+ }
148
253
}
0 commit comments