7
7
use Cake \Core \Configure ;
8
8
use Cake \Routing \Router ;
9
9
use Crud \TestSuite \IntegrationTestCase ;
10
+ use PHPUnit \Framework \Attributes \DataProvider ;
10
11
11
12
/**
12
13
* Licensed under The MIT License
@@ -87,12 +88,12 @@ public function testActionPost()
87
88
'Controller.initialize ' ,
88
89
['priority ' => 11 ],
89
90
function ($ event ) {
90
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
91
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
91
92
->onlyMethods (['set ' ])
92
93
->setConstructorArgs ([$ this ->_controller ->components ()])
93
94
->getMock ();
94
95
95
- $ this -> _controller -> Flash
96
+ $ component
96
97
->expects ($ this ->once ())
97
98
->method ('set ' )
98
99
->with (
@@ -104,6 +105,8 @@ function ($event) {
104
105
]
105
106
);
106
107
108
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
109
+
107
110
$ this ->_subscribeToEvents ($ this ->_controller );
108
111
}
109
112
);
@@ -132,12 +135,12 @@ public function testActionPostWithAddRedirect()
132
135
'Controller.initialize ' ,
133
136
['priority ' => 11 ],
134
137
function ($ event ) {
135
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
138
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
136
139
->onlyMethods (['set ' ])
137
140
->setConstructorArgs ([$ this ->_controller ->components ()])
138
141
->getMock ();
139
142
140
- $ this -> _controller -> Flash
143
+ $ component
141
144
->expects ($ this ->once ())
142
145
->method ('set ' )
143
146
->with (
@@ -149,6 +152,8 @@ function ($event) {
149
152
]
150
153
);
151
154
155
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
156
+
152
157
$ this ->_subscribeToEvents ($ this ->_controller );
153
158
}
154
159
);
@@ -177,12 +182,12 @@ public function testActionPostWithEditRedirect()
177
182
'Controller.initialize ' ,
178
183
['priority ' => 11 ],
179
184
function ($ event ) {
180
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
185
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
181
186
->onlyMethods (['set ' ])
182
187
->setConstructorArgs ([$ this ->_controller ->components ()])
183
188
->getMock ();
184
189
185
- $ this -> _controller -> Flash
190
+ $ component
186
191
->expects ($ this ->once ())
187
192
->method ('set ' )
188
193
->with (
@@ -194,6 +199,8 @@ function ($event) {
194
199
]
195
200
);
196
201
202
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
203
+
197
204
$ this ->_subscribeToEvents ($ this ->_controller );
198
205
}
199
206
);
@@ -221,12 +228,12 @@ public function testActionPostErrorSave()
221
228
'Controller.initialize ' ,
222
229
['priority ' => 11 ],
223
230
function ($ event ) {
224
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
231
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
225
232
->onlyMethods (['set ' ])
226
233
->setConstructorArgs ([$ this ->_controller ->components ()])
227
234
->getMock ();
228
235
229
- $ this -> _controller -> Flash
236
+ $ component
230
237
->expects ($ this ->once ())
231
238
->method ('set ' )
232
239
->with (
@@ -238,6 +245,8 @@ function ($event) {
238
245
]
239
246
);
240
247
248
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
249
+
241
250
$ this ->_subscribeToEvents ($ this ->_controller );
242
251
243
252
$ blogs = $ this ->getMockForModel (
@@ -248,7 +257,7 @@ function ($event) {
248
257
$ blogs
249
258
->expects ($ this ->once ())
250
259
->method ('save ' )
251
- ->will ( $ this -> returnValue ( false ) );
260
+ ->willReturn ( false );
252
261
253
262
$ this ->getTableLocator ()->set ('Blogs ' , $ blogs );
254
263
}
@@ -275,12 +284,12 @@ public function testActionPostValidationErrors()
275
284
'Controller.initialize ' ,
276
285
['priority ' => 11 ],
277
286
function ($ event ) {
278
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
287
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
279
288
->onlyMethods (['set ' ])
280
289
->setConstructorArgs ([$ this ->_controller ->components ()])
281
290
->getMock ();
282
291
283
- $ this -> _controller -> Flash
292
+ $ component
284
293
->expects ($ this ->once ())
285
294
->method ('set ' )
286
295
->with (
@@ -292,6 +301,8 @@ function ($event) {
292
301
]
293
302
);
294
303
304
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
305
+
295
306
$ this ->_subscribeToEvents ($ this ->_controller );
296
307
297
308
$ this ->_controller ->Blogs
@@ -344,10 +355,10 @@ public static function apiGetHttpMethodProvider()
344
355
/**
345
356
* Test HTTP & DELETE verbs using API Listener
346
357
*
347
- * @dataProvider apiGetHttpMethodProvider
348
358
* @param string $method
349
359
* @return void
350
360
*/
361
+ #[DataProvider('apiGetHttpMethodProvider ' )]
351
362
public function testApiGet ($ method )
352
363
{
353
364
Router::createRouteBuilder ('/ ' )
@@ -376,25 +387,27 @@ public static function apiUpdateHttpMethodProvider()
376
387
/**
377
388
* Test POST & PUT verbs using API Listener
378
389
*
379
- * @dataProvider apiUpdateHttpMethodProvider
380
390
* @param string $method
381
391
* @return void
382
392
*/
393
+ #[DataProvider('apiUpdateHttpMethodProvider ' )]
383
394
public function testApiCreate ($ method )
384
395
{
385
396
$ this ->_eventManager ->on (
386
397
'Controller.initialize ' ,
387
398
['priority ' => 11 ],
388
399
function ($ event ) {
389
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
400
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
390
401
->onlyMethods (['set ' ])
391
402
->setConstructorArgs ([$ this ->_controller ->components ()])
392
403
->getMock ();
393
404
394
- $ this -> _controller -> Flash
405
+ $ component
395
406
->expects ($ this ->never ())
396
407
->method ('set ' );
397
408
409
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
410
+
398
411
$ this ->_subscribeToEvents ($ this ->_controller );
399
412
400
413
$ this ->_controller ->Crud ->addListener ('api ' , 'Crud.Api ' );
@@ -417,10 +430,10 @@ function ($event) {
417
430
* Test POST & PUT verbs using API Listener
418
431
* with data validation error
419
432
*
420
- * @dataProvider apiUpdateHttpMethodProvider
421
433
* @param string $method
422
434
* @return void
423
435
*/
436
+ #[DataProvider('apiUpdateHttpMethodProvider ' )]
424
437
public function testApiCreateError ($ method )
425
438
{
426
439
$ this ->_eventManager ->on (
@@ -431,15 +444,17 @@ function ($event) {
431
444
return ;
432
445
}
433
446
434
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
447
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
435
448
->onlyMethods (['set ' ])
436
449
->setConstructorArgs ([$ this ->_controller ->components ()])
437
450
->getMock ();
438
451
439
- $ this -> _controller -> Flash
452
+ $ component
440
453
->expects ($ this ->never ())
441
454
->method ('set ' );
442
455
456
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
457
+
443
458
$ this ->_subscribeToEvents ($ this ->_controller );
444
459
445
460
$ this ->_controller ->Crud ->addListener ('api ' , 'Crud.Api ' );
@@ -469,10 +484,10 @@ function ($event) {
469
484
* Test POST & PUT verbs using API Listener
470
485
* with data validation errors
471
486
*
472
- * @dataProvider apiUpdateHttpMethodProvider
473
487
* @param string $method
474
488
* @return void
475
489
*/
490
+ #[DataProvider('apiUpdateHttpMethodProvider ' )]
476
491
public function testApiCreateErrors ($ method )
477
492
{
478
493
$ this ->_eventManager ->on (
@@ -483,15 +498,17 @@ function ($event) {
483
498
return ;
484
499
}
485
500
486
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
501
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
487
502
->onlyMethods (['set ' ])
488
503
->setConstructorArgs ([$ this ->_controller ->components ()])
489
504
->getMock ();
490
505
491
- $ this -> _controller -> Flash
506
+ $ component
492
507
->expects ($ this ->never ())
493
508
->method ('set ' );
494
509
510
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
511
+
495
512
$ this ->_subscribeToEvents ($ this ->_controller );
496
513
497
514
$ this ->_controller ->Crud ->addListener ('api ' , 'Crud.Api ' );
@@ -529,12 +546,12 @@ public function testStopAddWithDefaultSubjectSuccess()
529
546
'Controller.initialize ' ,
530
547
['priority ' => 11 ],
531
548
function ($ event ) {
532
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
549
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
533
550
->onlyMethods (['set ' ])
534
551
->setConstructorArgs ([$ this ->_controller ->components ()])
535
552
->getMock ();
536
553
537
- $ this -> _controller -> Flash
554
+ $ component
538
555
->expects ($ this ->once ())
539
556
->method ('set ' )
540
557
->with (
@@ -546,17 +563,21 @@ function ($event) {
546
563
]
547
564
);
548
565
566
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
567
+
549
568
$ this ->_subscribeToEvents ($ this ->_controller );
550
569
551
570
$ this ->_controller ->Crud ->on ('beforeSave ' , function ($ event ) {
552
571
$ event ->stopPropagation ();
553
572
});
554
573
555
- $ this -> _controller -> Blogs = $ this ->getMockForModel (
574
+ $ model = $ this ->getMockForModel (
556
575
$ this ->tableClass ,
557
576
[],
558
577
['alias ' => 'Blogs ' , 'table ' => 'blogs ' ]
559
578
);
579
+
580
+ $ this ->getTableLocator ()->set ('Blogs ' , $ model );
560
581
}
561
582
);
562
583
@@ -579,12 +600,12 @@ public function testStopAddWithManuallySetSubjectSuccess()
579
600
'Controller.initialize ' ,
580
601
['priority ' => 11 ],
581
602
function ($ event ) {
582
- $ this -> _controller -> Flash = $ this ->getMockBuilder (FlashComponent::class)
603
+ $ component = $ this ->getMockBuilder (FlashComponent::class)
583
604
->onlyMethods (['set ' ])
584
605
->setConstructorArgs ([$ this ->_controller ->components ()])
585
606
->getMock ();
586
607
587
- $ this -> _controller -> Flash
608
+ $ component
588
609
->expects ($ this ->once ())
589
610
->method ('set ' )
590
611
->with (
@@ -596,18 +617,22 @@ function ($event) {
596
617
]
597
618
);
598
619
620
+ $ this ->_controller ->components ()->set ('Flash ' , $ component );
621
+
599
622
$ this ->_subscribeToEvents ($ this ->_controller );
600
623
601
624
$ this ->_controller ->Crud ->on ('beforeSave ' , function ($ event ) {
602
625
$ event ->stopPropagation ();
603
626
$ event ->getSubject ()->success = true ; // assert this
604
627
});
605
628
606
- $ this -> _controller -> Blogs = $ this ->getMockForModel (
629
+ $ model = $ this ->getMockForModel (
607
630
$ this ->tableClass ,
608
631
[],
609
632
['alias ' => 'Blogs ' , 'table ' => 'blogs ' ]
610
633
);
634
+
635
+ $ this ->getTableLocator ()->set ('Blogs ' , $ model );
611
636
}
612
637
);
613
638
0 commit comments