1818
1919use Authorization \AuthorizationService ;
2020use Authorization \IdentityDecorator ;
21+ use Authorization \IdentityInterface ;
2122use Authorization \Policy \BeforePolicyInterface ;
2223use Authorization \Policy \BeforeScopeInterface ;
2324use Authorization \Policy \Exception \MissingMethodException ;
2425use Authorization \Policy \MapResolver ;
2526use Authorization \Policy \OrmResolver ;
2627use Authorization \Policy \Result ;
2728use Authorization \Policy \ResultInterface ;
29+ use Cake \Datasource \EntityInterface ;
2830use Cake \Datasource \QueryInterface ;
2931use Cake \TestSuite \TestCase ;
32+ use PHPUnit \Framework \ExpectationFailedException ;
3033use TestApp \Model \Entity \Article ;
3134use TestApp \Model \Table \ArticlesTable ;
3235use TestApp \Policy \ArticlePolicy ;
@@ -317,24 +320,22 @@ public function testApplyScopeAdditionalArguments()
317320 public function testBeforeFalse ()
318321 {
319322 $ entity = new Article ();
320-
321- $ policy = $ this ->getMockBuilder (BeforePolicyInterface::class)
322- ->onlyMethods (['before ' ])
323- ->addMethods (['canAdd ' ])
324- ->getMock ();
325-
326- $ policy ->expects ($ this ->once ())
327- ->method ('before ' )
328- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'add ' )
329- ->willReturn (false );
323+ $ policy = new class implements BeforePolicyInterface {
324+ public function before ($ identity , $ resource , $ action ): bool |ResultInterface |null
325+ {
326+ return false ;
327+ }
328+
329+ public function canAdd ($ user , $ entity )
330+ {
331+ throw new ExpectationFailedException ('This method should not be called ' );
332+ }
333+ };
330334
331335 $ resolver = new MapResolver ([
332336 Article::class => $ policy ,
333337 ]);
334338
335- $ policy ->expects ($ this ->never ())
336- ->method ('canAdd ' );
337-
338339 $ service = new AuthorizationService ($ resolver );
339340
340341 $ user = new IdentityDecorator ($ service , [
@@ -348,19 +349,17 @@ public function testBeforeFalse()
348349 public function testBeforeTrue ()
349350 {
350351 $ entity = new Article ();
351-
352- $ policy = $ this ->getMockBuilder (BeforePolicyInterface::class)
353- ->onlyMethods (['before ' ])
354- ->addMethods (['canAdd ' ])
355- ->getMock ();
356-
357- $ policy ->expects ($ this ->once ())
358- ->method ('before ' )
359- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'add ' )
360- ->willReturn (true );
361-
362- $ policy ->expects ($ this ->never ())
363- ->method ('canAdd ' );
352+ $ policy = new class implements BeforePolicyInterface {
353+ public function before ($ identity , $ resource , $ action ): bool |ResultInterface |null
354+ {
355+ return true ;
356+ }
357+
358+ public function canAdd ($ user , $ entity )
359+ {
360+ throw new ExpectationFailedException ('This method should not be called ' );
361+ }
362+ };
364363
365364 $ resolver = new MapResolver ([
366365 Article::class => $ policy ,
@@ -379,21 +378,17 @@ public function testBeforeTrue()
379378 public function testBeforeNull ()
380379 {
381380 $ entity = new Article ();
382-
383- $ policy = $ this ->getMockBuilder (BeforePolicyInterface::class)
384- ->onlyMethods (['before ' ])
385- ->addMethods (['canAdd ' ])
386- ->getMock ();
387-
388- $ policy ->expects ($ this ->once ())
389- ->method ('before ' )
390- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'add ' )
391- ->willReturn (null );
392-
393- $ policy ->expects ($ this ->once ())
394- ->method ('canAdd ' )
395- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity )
396- ->willReturn (true );
381+ $ policy = new class implements BeforePolicyInterface {
382+ public function before ($ identity , $ resource , $ action ): bool |ResultInterface |null
383+ {
384+ return null ;
385+ }
386+
387+ public function canAdd ($ user , $ entity ): bool
388+ {
389+ return true ;
390+ }
391+ };
397392
398393 $ resolver = new MapResolver ([
399394 Article::class => $ policy ,
@@ -412,19 +407,17 @@ public function testBeforeNull()
412407 public function testBeforeResultTrue ()
413408 {
414409 $ entity = new Article ();
415-
416- $ policy = $ this ->getMockBuilder (BeforePolicyInterface::class)
417- ->onlyMethods (['before ' ])
418- ->addMethods (['canAdd ' ])
419- ->getMock ();
420-
421- $ policy ->expects ($ this ->once ())
422- ->method ('before ' )
423- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'add ' )
424- ->willReturn (new Result (true ));
425-
426- $ policy ->expects ($ this ->never ())
427- ->method ('canAdd ' );
410+ $ policy = new class implements BeforePolicyInterface {
411+ public function before ($ identity , $ resource , $ action ): bool |ResultInterface |null
412+ {
413+ return new Result (true );
414+ }
415+
416+ public function canAdd ($ user , $ entity )
417+ {
418+ throw new ExpectationFailedException ('This method should not be called ' );
419+ }
420+ };
428421
429422 $ resolver = new MapResolver ([
430423 Article::class => $ policy ,
@@ -443,19 +436,17 @@ public function testBeforeResultTrue()
443436 public function testBeforeResultFalse ()
444437 {
445438 $ entity = new Article ();
446-
447- $ policy = $ this ->getMockBuilder (BeforePolicyInterface::class)
448- ->onlyMethods (['before ' ])
449- ->addMethods (['canAdd ' ])
450- ->getMock ();
451-
452- $ policy ->expects ($ this ->once ())
453- ->method ('before ' )
454- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'add ' )
455- ->willReturn (new Result (false ));
456-
457- $ policy ->expects ($ this ->never ())
458- ->method ('canAdd ' );
439+ $ policy = new class implements BeforePolicyInterface {
440+ public function before ($ identity , $ resource , $ action ): bool |ResultInterface |null
441+ {
442+ return new Result (false );
443+ }
444+
445+ public function canAdd ($ user , $ entity )
446+ {
447+ throw new ExpectationFailedException ('This method should not be called ' );
448+ }
449+ };
459450
460451 $ resolver = new MapResolver ([
461452 Article::class => $ policy ,
@@ -474,19 +465,17 @@ public function testBeforeResultFalse()
474465 public function testBeforeScopeNonNull ()
475466 {
476467 $ entity = new Article ();
477-
478- $ policy = $ this ->getMockBuilder (BeforeScopeInterface::class)
479- ->onlyMethods (['beforeScope ' ])
480- ->addMethods (['scopeIndex ' ])
481- ->getMock ();
482-
483- $ policy ->expects ($ this ->once ())
484- ->method ('beforeScope ' )
485- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'index ' )
486- ->willReturn ('foo ' );
487-
488- $ policy ->expects ($ this ->never ())
489- ->method ('scopeIndex ' );
468+ $ policy = new class implements BeforeScopeInterface {
469+ public function beforeScope (?IdentityInterface $ identity , mixed $ resource , string $ action ): mixed
470+ {
471+ return 'foo ' ;
472+ }
473+
474+ public function scopeIndex (IdentityInterface $ user , QueryInterface $ query )
475+ {
476+ throw new ExpectationFailedException ('This method should not be called ' );
477+ }
478+ };
490479
491480 $ resolver = new MapResolver ([
492481 Article::class => $ policy ,
@@ -505,21 +494,17 @@ public function testBeforeScopeNonNull()
505494 public function testBeforeScopeNull ()
506495 {
507496 $ entity = new Article ();
508-
509- $ policy = $ this ->getMockBuilder (BeforeScopeInterface::class)
510- ->onlyMethods (['beforeScope ' ])
511- ->addMethods (['scopeIndex ' ])
512- ->getMock ();
513-
514- $ policy ->expects ($ this ->once ())
515- ->method ('beforeScope ' )
516- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity , 'index ' )
517- ->willReturn (null );
518-
519- $ policy ->expects ($ this ->once ())
520- ->method ('scopeIndex ' )
521- ->with ($ this ->isInstanceOf (IdentityDecorator::class), $ entity )
522- ->willReturn ('bar ' );
497+ $ policy = new class implements BeforeScopeInterface {
498+ public function beforeScope (?IdentityInterface $ identity , mixed $ resource , string $ action ): mixed
499+ {
500+ return null ;
501+ }
502+
503+ public function scopeIndex (IdentityInterface $ user , EntityInterface $ entity )
504+ {
505+ return 'bar ' ;
506+ }
507+ };
523508
524509 $ resolver = new MapResolver ([
525510 Article::class => $ policy ,
0 commit comments