5
5
namespace Atk4 \Ui \Tests ;
6
6
7
7
use Atk4 \Core \Phpunit \TestCase ;
8
+ use Atk4 \Data \Field ;
8
9
use Atk4 \Data \Model ;
9
10
use Atk4 \Data \Model \EntityFieldPair ;
10
11
use Atk4 \Data \ValidationException ;
@@ -28,10 +29,10 @@ public function testGetField(): void
28
29
$ form ->setApp ($ this ->createApp ());
29
30
$ form ->invokeInit ();
30
31
31
- $ form ->addControl ('test ' );
32
+ $ form ->addControl ('foo ' );
32
33
33
- self ::assertInstanceOf (Form \Control::class, $ form ->getControl ('test ' ));
34
- self ::assertSame ($ form ->getControl ('test ' ), $ form ->layout ->getControl ('test ' ));
34
+ self ::assertInstanceOf (Form \Control::class, $ form ->getControl ('foo ' ));
35
+ self ::assertSame ($ form ->getControl ('foo ' ), $ form ->layout ->getControl ('foo ' ));
35
36
}
36
37
37
38
public function testAddControlAlreadyExistsException (): void
@@ -131,11 +132,11 @@ public function testTextareaSubmit(): void
131
132
{
132
133
$ this ->assertFormSubmit (static function (App $ app ) {
133
134
$ form = Form::addTo ($ app );
134
- $ form ->addControl ('Textarea ' );
135
+ $ form ->addControl ('foo ' );
135
136
136
137
return $ form ;
137
- }, ['Textarea ' => '0 ' ], static function (Model $ m ) {
138
- self ::assertSame ('0 ' , $ m ->get ('Textarea ' ));
138
+ }, ['foo ' => '0 ' ], static function (Model $ m ) {
139
+ self ::assertSame ('0 ' , $ m ->get ('foo ' ));
139
140
});
140
141
}
141
142
@@ -203,35 +204,130 @@ public function testSubmitNonFormFieldError(): void
203
204
$ submitReached = false ;
204
205
$ catchReached = false ;
205
206
try {
206
- try {
207
- $ this ->assertFormSubmit (static function (App $ app ) {
208
- $ m = new Model ();
209
- $ m ->addField ('foo ' , ['nullable ' => false ]);
210
- $ m ->addField ('bar ' , ['nullable ' => false ]);
211
-
212
- $ form = Form::addTo ($ app );
213
- $ form ->setModel ($ m ->createEntity (), ['foo ' ]);
214
-
215
- return $ form ;
216
- }, ['foo ' => 'x ' ], static function (Model $ model ) use (&$ submitReached ) {
217
- $ submitReached = true ;
218
- $ model ->set ('bar ' , null );
207
+ $ this ->assertFormSubmit (static function (App $ app ) {
208
+ $ m = new Model ();
209
+ $ m ->addField ('foo ' , ['nullable ' => false ]);
210
+ $ m ->addField ('bar ' , ['nullable ' => false ]);
211
+
212
+ $ form = Form::addTo ($ app );
213
+ $ form ->setModel ($ m ->createEntity (), ['foo ' ]);
214
+
215
+ return $ form ;
216
+ }, ['foo ' => 'x ' ], static function (Model $ model ) use (&$ submitReached ) {
217
+ $ submitReached = true ;
218
+ $ model ->set ('bar ' , null );
219
+ });
220
+ } catch (UnhandledCallbackExceptionError $ e ) {
221
+ $ catchReached = true ;
222
+ self ::assertSame ('bar ' , $ e ->getPrevious ()->getParams ()['field ' ]->shortName ); // @phpstan-ignore-line
223
+
224
+ $ this ->expectException (ValidationException::class);
225
+ $ this ->expectExceptionMessage ('Must not be null ' );
226
+
227
+ throw $ e ->getPrevious ();
228
+ } finally {
229
+ self ::assertTrue ($ submitReached );
230
+ self ::assertTrue ($ catchReached );
231
+ }
232
+ }
233
+
234
+ public function testLoadPostConvertedWarningNotWrappedException (): void
235
+ {
236
+ $ catchReached = false ;
237
+ try {
238
+ $ this ->assertFormSubmit (static function (App $ app ) {
239
+ $ m = new Model ();
240
+ $ m ->addField ('foo ' , new class () extends Field {
241
+ public function normalize ($ value )
242
+ {
243
+ TestCase::assertSame ('x ' , $ value );
244
+
245
+ throw new \ErrorException ('Converted PHP warning ' );
246
+ }
219
247
});
220
- } catch (UnhandledCallbackExceptionError $ e ) {
221
- $ catchReached = true ;
222
- self ::assertSame ('bar ' , $ e ->getPrevious ()->getParams ()['field ' ]->shortName ); // @phpstan-ignore-line
223
248
224
- $ this -> expectException (ValidationException::class );
225
- $ this -> expectExceptionMessage ( ' Must not be null ' );
249
+ $ form = Form:: addTo ( $ app );
250
+ $ form -> setModel ( $ m -> createEntity () );
226
251
227
- throw $ e ->getPrevious ();
228
- }
252
+ return $ form ;
253
+ }, ['foo ' => 'x ' ]);
254
+ } catch (UnhandledCallbackExceptionError $ e ) {
255
+ $ catchReached = true ;
256
+
257
+ $ this ->expectException (\ErrorException::class);
258
+ $ this ->expectExceptionMessage ('Converted PHP warning ' );
259
+
260
+ throw $ e ->getPrevious ();
229
261
} finally {
230
- self ::assertTrue ($ submitReached );
231
262
self ::assertTrue ($ catchReached );
232
263
}
233
264
}
234
265
266
+ public function testCreateControlException (): void
267
+ {
268
+ $ form = new Form ();
269
+ $ form ->setApp ($ this ->createApp ());
270
+ $ form ->invokeInit ();
271
+
272
+ $ controlClass = get_class (new class () extends Form \Control {
273
+ public static bool $ firstCreate = true ;
274
+
275
+ public function __construct () // @phpstan-ignore-line
276
+ {
277
+ if (self ::$ firstCreate ) {
278
+ self ::$ firstCreate = false ;
279
+
280
+ return ;
281
+ }
282
+
283
+ throw new Exception ('x ' );
284
+ }
285
+ });
286
+
287
+ $ this ->expectException (Exception::class);
288
+ $ this ->expectExceptionMessage ('Unable to create form control ' );
289
+ try {
290
+ $ form ->addControl ('foo ' , [$ controlClass ]);
291
+ } catch (Exception $ e ) {
292
+ self ::assertSame (Exception::class, get_class ($ e ->getPrevious ()));
293
+ self ::assertSame ('x ' , $ e ->getPrevious ()->getMessage ());
294
+
295
+ throw $ e ;
296
+ } finally {
297
+ $ controlClass ::$ firstCreate = true ;
298
+ }
299
+ }
300
+
301
+ public function testCreateControlConvertedWarningNotWrappedException (): void
302
+ {
303
+ $ form = new Form ();
304
+ $ form ->setApp ($ this ->createApp ());
305
+ $ form ->invokeInit ();
306
+
307
+ $ controlClass = get_class (new class () extends Form \Control {
308
+ public static bool $ firstCreate = true ;
309
+
310
+ public function __construct () // @phpstan-ignore-line
311
+ {
312
+ if (self ::$ firstCreate ) {
313
+ self ::$ firstCreate = false ;
314
+
315
+ return ;
316
+ }
317
+
318
+ throw new \ErrorException ('Converted PHP warning ' );
319
+ }
320
+ });
321
+
322
+ $ this ->expectException (\ErrorException::class);
323
+ $ this ->expectExceptionMessage ('Converted PHP warning ' );
324
+ try {
325
+ $ form ->addControl ('foo ' , [$ controlClass ]);
326
+ } finally {
327
+ $ controlClass ::$ firstCreate = true ;
328
+ }
329
+ }
330
+
235
331
public function testNoDisabledAttrWithHiddenType (): void
236
332
{
237
333
$ input = new Form \Control \Line ();
0 commit comments