diff --git a/composer.json b/composer.json index 66e146591..eecc80395 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "require-dev": { "friendsofcake/cakephp-test-utilities": "^3.0", "friendsofcake/search": "^7.0", - "phpunit/phpunit": "^10.1" + "phpunit/phpunit": "^10.5.5 || ^11.1.3" }, "autoload": { "psr-4": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 75a4da41b..c1c6d1964 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,7 @@ parameters: ignoreErrors: + - + identifier: missingType.iterableValue - message: "#^Call to an undefined method Crud\\\\Action\\\\BaseAction\\:\\:findMethod\\(\\)\\.$#" count: 2 @@ -19,8 +21,3 @@ parameters: message: "#^Access to an undefined property Cake\\\\Controller\\\\Controller\\:\\:\\$Crud\\.$#" count: 1 path: src/Core/BaseObject.php - - - - message: "#^Call to an undefined method Cake\\\\Controller\\\\Controller\\:\\:setViewClasses\\(\\)\\.$#" - count: 1 - path: src/Listener/ApiListener.php diff --git a/phpstan.neon b/phpstan.neon index f49c8ea27..4ae108765 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,11 +5,7 @@ parameters: level: 6 paths: - src - checkMissingIterableValueType: false - checkGenericClassInNonGenericObjectType: false excludePaths: - */src/TestSuite/* universalObjectCratesClasses: - Crud\Event\Subject - bootstrapFiles: - - vendor/cakephp/cakephp/src/Core/Exception/CakeException.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b04e4e1df..de5df5c6d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,10 +1,5 @@ - - - setViewClasses - - new QueryLogger() diff --git a/src/Controller/ControllerTrait.php b/src/Controller/ControllerTrait.php index 78ffa441e..748fa87a1 100644 --- a/src/Controller/ControllerTrait.php +++ b/src/Controller/ControllerTrait.php @@ -78,6 +78,7 @@ public function invokeAction(Closure $action, array $args): void * * @param array> $map View class map. * @return void + * @deprecated 7.1.0 Use addViewClasses() instead. */ public function setViewClasses(array $map): void { diff --git a/src/Core/BaseObject.php b/src/Core/BaseObject.php index 603a11102..0e9347856 100644 --- a/src/Core/BaseObject.php +++ b/src/Core/BaseObject.php @@ -60,7 +60,7 @@ public function implementedEvents(): array /** * Convenient method for Request::is * - * @param array|string $method Method(s) to check for + * @param list|string $method Method(s) to check for * @return bool */ protected function _checkRequestType(array|string $method): bool diff --git a/src/Listener/ApiListener.php b/src/Listener/ApiListener.php index 9df05bf98..5c25e8ce1 100644 --- a/src/Listener/ApiListener.php +++ b/src/Listener/ApiListener.php @@ -336,7 +336,7 @@ protected function _expandPath(Subject $subject, string $path): string */ public function injectViewClasses(): void { - $this->_controller()->setViewClasses($this->getConfig('viewClasses')); + $this->_controller()->addViewClasses($this->getConfig('viewClasses')); } /** diff --git a/tests/TestCase/Action/AddActionTest.php b/tests/TestCase/Action/AddActionTest.php index a489c0667..aed52fdc6 100644 --- a/tests/TestCase/Action/AddActionTest.php +++ b/tests/TestCase/Action/AddActionTest.php @@ -7,6 +7,7 @@ use Cake\Core\Configure; use Cake\Routing\Router; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -87,12 +88,12 @@ public function testActionPost() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -104,6 +105,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -132,12 +135,12 @@ public function testActionPostWithAddRedirect() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -149,6 +152,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -177,12 +182,12 @@ public function testActionPostWithEditRedirect() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -194,6 +199,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -221,12 +228,12 @@ public function testActionPostErrorSave() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -238,6 +245,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $blogs = $this->getMockForModel( @@ -248,7 +257,7 @@ function ($event) { $blogs ->expects($this->once()) ->method('save') - ->will($this->returnValue(false)); + ->willReturn(false); $this->getTableLocator()->set('Blogs', $blogs); } @@ -275,12 +284,12 @@ public function testActionPostValidationErrors() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -292,6 +301,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Blogs @@ -344,10 +355,10 @@ public static function apiGetHttpMethodProvider() /** * Test HTTP & DELETE verbs using API Listener * - * @dataProvider apiGetHttpMethodProvider * @param string $method * @return void */ + #[DataProvider('apiGetHttpMethodProvider')] public function testApiGet($method) { Router::createRouteBuilder('/') @@ -376,25 +387,27 @@ public static function apiUpdateHttpMethodProvider() /** * Test POST & PUT verbs using API Listener * - * @dataProvider apiUpdateHttpMethodProvider * @param string $method * @return void */ + #[DataProvider('apiUpdateHttpMethodProvider')] public function testApiCreate($method) { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->never()) ->method('set'); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->addListener('api', 'Crud.Api'); @@ -417,10 +430,10 @@ function ($event) { * Test POST & PUT verbs using API Listener * with data validation error * - * @dataProvider apiUpdateHttpMethodProvider * @param string $method * @return void */ + #[DataProvider('apiUpdateHttpMethodProvider')] public function testApiCreateError($method) { $this->_eventManager->on( @@ -431,15 +444,17 @@ function ($event) { return; } - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->never()) ->method('set'); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->addListener('api', 'Crud.Api'); @@ -469,10 +484,10 @@ function ($event) { * Test POST & PUT verbs using API Listener * with data validation errors * - * @dataProvider apiUpdateHttpMethodProvider * @param string $method * @return void */ + #[DataProvider('apiUpdateHttpMethodProvider')] public function testApiCreateErrors($method) { $this->_eventManager->on( @@ -483,15 +498,17 @@ function ($event) { return; } - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->never()) ->method('set'); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->addListener('api', 'Crud.Api'); @@ -529,12 +546,12 @@ public function testStopAddWithDefaultSubjectSuccess() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -546,17 +563,21 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeSave', function ($event) { $event->stopPropagation(); }); - $this->_controller->Blogs = $this->getMockForModel( + $model = $this->getMockForModel( $this->tableClass, [], ['alias' => 'Blogs', 'table' => 'blogs'] ); + + $this->getTableLocator()->set('Blogs', $model); } ); @@ -579,12 +600,12 @@ public function testStopAddWithManuallySetSubjectSuccess() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -596,6 +617,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeSave', function ($event) { @@ -603,11 +626,13 @@ function ($event) { $event->getSubject()->success = true; // assert this }); - $this->_controller->Blogs = $this->getMockForModel( + $model = $this->getMockForModel( $this->tableClass, [], ['alias' => 'Blogs', 'table' => 'blogs'] ); + + $this->getTableLocator()->set('Blogs', $model); } ); diff --git a/tests/TestCase/Action/BaseActionTest.php b/tests/TestCase/Action/BaseActionTest.php index 00cfa55ee..6b3721691 100644 --- a/tests/TestCase/Action/BaseActionTest.php +++ b/tests/TestCase/Action/BaseActionTest.php @@ -4,6 +4,7 @@ namespace Crud\TestCase\Action; use Cake\Controller\Component\FlashComponent; +use Cake\Controller\ComponentRegistry; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Event\EventManager; @@ -15,6 +16,8 @@ use Crud\Action\BaseAction; use Crud\Controller\Component\CrudComponent; use Crud\Event\Subject; +use Crud\Test\App\Action\TestAction; +use Crud\Test\App\Action\TestGetAction; use Crud\TestSuite\TestCase; use Exception; @@ -25,6 +28,11 @@ class BaseActionTest extends TestCase { protected ServerRequest $Request; + protected Controller $Controller; + protected CrudComponent $Crud; + protected ComponentRegistry $Registry; + protected $actionClassName; + protected BaseAction $ActionClass; public function setUp(): void { @@ -41,16 +49,13 @@ public function setUp(): void ]) ->getMock(); $this->Registry = $this->Controller->components(); - $this->Crud = $this->getMockBuilder(CrudComponent::class) - ->setConstructorArgs([$this->Registry]) - ->addMethods(['foobar']) - ->getMock(); - $this->Controller->Crud = $this->Crud; + $this->Crud = new CrudComponent($this->Registry); + $this->Controller->components()->set('Crud', $this->Crud); $this->getTableLocator()->get('CrudExamples')->setAlias('MyModel'); - $this->actionClassName = $this->getMockBuilder(BaseAction::class) - ->addMethods(['_handle']) + $this->actionClassName = $this->getMockBuilder(TestAction::class) + ->onlyMethods(['_handle']) ->disableOriginalConstructor() ->getMock(); $this->ActionClass = new $this->actionClassName($this->Controller); @@ -148,22 +153,21 @@ public function testEnabledActionWorks() $Request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); - $Action = $this->getMockBuilder(BaseAction::class) - ->onlyMethods(['_request']) - ->addMethods(['_get']) + $Action = $this->getMockBuilder(TestAction::class) + ->onlyMethods(['_request', '_handle']) ->setConstructorArgs([$this->Controller]) ->getMock(); $Action ->expects($this->any()) ->method('_request') ->with() - ->will($this->returnValue($Request)); + ->willReturn($Request); $Action ->expects($this->once()) - ->method('_get', '_handle was never called on a enabled action') - ->will($this->returnValue(true)); + ->method('_handle') + ->willReturn(true); $this->_configureAction($Action); $Action->setConfig('action', 'add'); @@ -193,7 +197,7 @@ public function testDisable() ->getMock(); $Action ->expects($this->once()) - ->method('setConfig', 'enabled was not changed to false by config()') + ->method('setConfig') ->with('enabled', false); $Action->disable(); @@ -215,7 +219,7 @@ public function testEnable() ->getMock(); $Action ->expects($this->once()) - ->method('setConfig', 'enabled was not changed to false by config()') + ->method('setConfig') ->with('enabled', true); $Action->enable(); @@ -242,15 +246,17 @@ public function testSetFlash() $Subject = new Subject(); - $this->Controller->Crud = $this->getMockBuilder(CrudComponent::class) + $component = $this->getMockBuilder(CrudComponent::class) ->onlyMethods(['trigger']) ->setConstructorArgs([$this->Registry]) ->getMock(); - $this->Controller->Crud + $component ->expects($this->once()) ->method('trigger') ->with('setFlash', $Subject) - ->will($this->returnValue(new Event('Crud.setFlash'))); + ->willReturn(new Event('Crud.setFlash')); + + $this->Controller->components()->set('Crud', $component); $flash = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) @@ -514,9 +520,8 @@ public function testBadRequestMethodMessage() */ public function testHandle() { - $Action = $this->getMockBuilder(BaseAction::class) - ->onlyMethods(['_request', 'getConfig']) - ->addMethods(['_get']) + $Action = $this->getMockBuilder(TestGetAction::class) + ->onlyMethods(['_request', 'getConfig', '_get']) ->setConstructorArgs([$this->Controller]) ->getMock(); @@ -526,17 +531,17 @@ public function testHandle() $Request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $Action ->expects($this->once()) ->method('getConfig') ->with('enabled') - ->will($this->returnValue(true)); + ->willReturn(true); $Action ->expects($this->once()) ->method('_request') - ->will($this->returnValue($Request)); + ->willReturn($Request); $Action ->expects($this->once()) ->method('_get'); @@ -554,9 +559,8 @@ public function testHandle() */ public function testHandleDisabled() { - $Action = $this->getMockBuilder(BaseAction::class) - ->onlyMethods(['getConfig']) - ->addMethods(['_handle', '_get']) + $Action = $this->getMockBuilder(TestAction::class) + ->onlyMethods(['getConfig', '_handle']) ->setConstructorArgs([$this->Controller]) ->getMock(); @@ -564,7 +568,7 @@ public function testHandleDisabled() ->expects($this->once()) ->method('getConfig') ->with('enabled') - ->will($this->returnValue(false)); + ->willReturn(false); $Action ->expects($this->never()) ->method('_handle'); @@ -582,9 +586,8 @@ public function testHandleDisabled() */ public function testGenericHandle() { - $Action = $this->getMockBuilder(BaseAction::class) - ->onlyMethods(['_request', 'getConfig']) - ->addMethods(['_handle']) + $Action = $this->getMockBuilder(TestAction::class) + ->onlyMethods(['_request', 'getConfig', '_handle']) ->setConstructorArgs([$this->Controller]) ->getMock(); @@ -594,17 +597,17 @@ public function testGenericHandle() $Request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $Action ->expects($this->once()) ->method('getConfig') ->with('enabled') - ->will($this->returnValue(true)); + ->willReturn(true); $Action ->expects($this->once()) ->method('_request') - ->will($this->returnValue($Request)); + ->willReturn($Request); $Action ->expects($this->once()) ->method('_handle'); @@ -635,17 +638,17 @@ public function testHandleException() $Request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $Action ->expects($this->once()) ->method('getConfig') ->with('enabled') - ->will($this->returnValue(true)); + ->willReturn(true); $Action ->expects($this->once()) ->method('_request') - ->will($this->returnValue($Request)); + ->willReturn($Request); $Action->handle(); } diff --git a/tests/TestCase/Action/Bulk/DeleteActionTest.php b/tests/TestCase/Action/Bulk/DeleteActionTest.php index f4072222a..c5afcb9c7 100644 --- a/tests/TestCase/Action/Bulk/DeleteActionTest.php +++ b/tests/TestCase/Action/Bulk/DeleteActionTest.php @@ -5,6 +5,7 @@ use Cake\Controller\Component\FlashComponent; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -45,21 +46,21 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for all HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testAllRequestMethods($method) { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -71,6 +72,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -98,12 +101,12 @@ public function testStopBeforeBulk() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -115,6 +118,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeBulk', function ($event) { @@ -146,12 +151,12 @@ public function testUuidRequestData() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -163,6 +168,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); diff --git a/tests/TestCase/Action/Bulk/SetValueActionTest.php b/tests/TestCase/Action/Bulk/SetValueActionTest.php index 2ef8d9e79..a5afe3fe6 100644 --- a/tests/TestCase/Action/Bulk/SetValueActionTest.php +++ b/tests/TestCase/Action/Bulk/SetValueActionTest.php @@ -5,6 +5,7 @@ use Cake\Controller\Component\FlashComponent; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -45,21 +46,21 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for all HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testAllRequestMethods($method) { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -71,6 +72,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -98,12 +101,12 @@ public function testStopBeforeBulk() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -115,6 +118,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeBulk', function ($event) { @@ -146,12 +151,12 @@ public function testUuidRequestData() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -163,6 +168,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); diff --git a/tests/TestCase/Action/Bulk/ToggleActionTest.php b/tests/TestCase/Action/Bulk/ToggleActionTest.php index d9b6a141a..764c9355f 100644 --- a/tests/TestCase/Action/Bulk/ToggleActionTest.php +++ b/tests/TestCase/Action/Bulk/ToggleActionTest.php @@ -5,6 +5,7 @@ use Cake\Controller\Component\FlashComponent; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -45,21 +46,21 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for all HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testAllRequestMethods($method) { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -71,6 +72,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -98,12 +101,12 @@ public function testStopBeforeBulk() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -115,6 +118,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeBulk', function ($event) { @@ -146,12 +151,12 @@ public function testUuidRequestData() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -163,6 +168,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); diff --git a/tests/TestCase/Action/DeleteActionTest.php b/tests/TestCase/Action/DeleteActionTest.php index b5cdcad98..b3d7d562e 100644 --- a/tests/TestCase/Action/DeleteActionTest.php +++ b/tests/TestCase/Action/DeleteActionTest.php @@ -5,6 +5,7 @@ use Cake\Controller\Component\FlashComponent; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -42,21 +43,21 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testAllRequestMethods($method) { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -68,6 +69,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $blogs = $this->getMockForModel( @@ -78,7 +81,7 @@ function ($event) { $blogs ->expects($this->once()) ->method('delete') - ->will($this->returnValue(true)); + ->willReturn(true); $this->getTableLocator()->set('Blogs', $blogs); } @@ -102,12 +105,12 @@ public function testStopDelete() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -119,6 +122,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeDelete', function ($event) { @@ -148,21 +153,21 @@ function ($event) { /** * Test the flow when the beforeRedirect event is stopped (no redirection) * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testStopBeforeRedirect() { $this->_eventManager->on( 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -174,6 +179,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Crud->on('beforeRedirect', function ($event) { @@ -188,7 +195,7 @@ function ($event) { $blogs ->expects($this->once()) ->method('delete') - ->will($this->returnValue(true)); + ->willReturn(true); $this->getTableLocator()->set('Blogs', $blogs); } diff --git a/tests/TestCase/Action/EditActionTest.php b/tests/TestCase/Action/EditActionTest.php index 68dec77c3..a7aadf348 100644 --- a/tests/TestCase/Action/EditActionTest.php +++ b/tests/TestCase/Action/EditActionTest.php @@ -106,12 +106,12 @@ public function testActionPost() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -123,6 +123,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); @@ -149,12 +151,12 @@ public function testActionPostErrorSave() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -166,6 +168,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $blogs = $this->getMockForModel( @@ -176,7 +180,7 @@ function ($event) { $blogs ->expects($this->once()) ->method('save') - ->will($this->returnValue(false)); + ->willReturn(false); $this->getTableLocator()->set('Blogs', $blogs); } @@ -203,12 +207,12 @@ public function testActionPostValidationErrors() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -220,6 +224,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); $this->_controller->Blogs @@ -267,12 +273,12 @@ public function testActionPatch() 'Controller.initialize', ['priority' => 11], function ($event) { - $this->_controller->Flash = $this->getMockBuilder(FlashComponent::class) + $component = $this->getMockBuilder(FlashComponent::class) ->onlyMethods(['set']) ->setConstructorArgs([$this->_controller->components()]) ->getMock(); - $this->_controller->Flash + $component ->expects($this->once()) ->method('set') ->with( @@ -284,6 +290,8 @@ function ($event) { ] ); + $this->_controller->components()->set('Flash', $component); + $this->_subscribeToEvents($this->_controller); } ); diff --git a/tests/TestCase/Action/IndexActionTest.php b/tests/TestCase/Action/IndexActionTest.php index 6eff63904..ce10bbf3f 100644 --- a/tests/TestCase/Action/IndexActionTest.php +++ b/tests/TestCase/Action/IndexActionTest.php @@ -4,6 +4,7 @@ namespace Crud\Test\TestCase\Action; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -36,9 +37,9 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for all HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testGet($method) { $this->_eventManager->on( diff --git a/tests/TestCase/Action/ViewActionTest.php b/tests/TestCase/Action/ViewActionTest.php index 476eba1c4..1f3b2bf8e 100644 --- a/tests/TestCase/Action/ViewActionTest.php +++ b/tests/TestCase/Action/ViewActionTest.php @@ -4,6 +4,7 @@ namespace Crud\Test\TestCase\Action; use Crud\TestSuite\IntegrationTestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -36,9 +37,9 @@ public static function allHttpMethodProvider() /** * Test the normal HTTP flow for all HTTP verbs * - * @dataProvider allHttpMethodProvider * @return void */ + #[DataProvider('allHttpMethodProvider')] public function testGet($method) { $this->_eventManager->on( diff --git a/tests/TestCase/Controller/Component/CrudComponentTest.php b/tests/TestCase/Controller/Component/CrudComponentTest.php index 396c0f444..a0d2aac53 100644 --- a/tests/TestCase/Controller/Component/CrudComponentTest.php +++ b/tests/TestCase/Controller/Component/CrudComponentTest.php @@ -59,7 +59,7 @@ public function setUp(): void ->getMock() ->withParam('action', 'index'); - $this->request->expects($this->any())->method('is')->will($this->returnValue(true)); + $this->request->expects($this->any())->method('is')->willReturn(true); $this->controller = $this->getMockBuilder(CrudExamplesController::class) ->onlyMethods(['redirect', 'render']) @@ -80,7 +80,7 @@ public function setUp(): void $this->Crud = new TestCrudComponent($this->Registry, $config); $this->Crud->beforeFilter(new Event('Controller.beforeFilter')); - $this->controller->Crud = $this->Crud; + $this->controller->components()->set('Crud', $this->Crud); } /** @@ -237,7 +237,7 @@ public function testView() $this->request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $this->controller ->expects($this->once()) @@ -285,7 +285,7 @@ public function testIsActionMappedNo() $this->Crud = new TestCrudComponent($this->Registry, $config); $this->Crud->beforeFilter(new Event('Controller.beforeFilter')); - $this->controller->Crud = $this->Crud; + $this->controller->components()->set('Crud', $this->Crud); $this->Crud->beforeFilter(new Event('Controller.beforeFilter')); $result = $this->Crud->isActionMapped(); @@ -364,12 +364,12 @@ public function testViewWithArrayNewAction() $this->request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $this->request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $this->controller ->expects($this->once()) @@ -392,7 +392,7 @@ public function testViewWithArrayIndexAction() $this->request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $this->controller ->expects($this->once()) @@ -444,7 +444,7 @@ public function testViewCanBeChangedInControllerAction() $request ->expects($this->once()) ->method('getMethod') - ->will($this->returnValue('GET')); + ->willReturn('GET'); $this->controller->setRequest($request); @@ -873,7 +873,7 @@ public function testUseModel() { $this->Crud = new CrudComponent($this->Registry, ['actions' => ['Crud.Index']]); $this->Crud->beforeFilter(new Event('Controller.beforeFilter')); - $this->controller->Crud = $this->Crud; + $this->controller->components()->set('Crud', $this->Crud); $this->controller->getTableLocator()->set('MyModel', new Table([ 'table' => 'posts', 'alias' => 'MyModel', diff --git a/tests/TestCase/Error/ExceptionRendererTest.php b/tests/TestCase/Error/ExceptionRendererTest.php index 746254aab..11735ebd4 100644 --- a/tests/TestCase/Error/ExceptionRendererTest.php +++ b/tests/TestCase/Error/ExceptionRendererTest.php @@ -39,7 +39,7 @@ public function testNormalExceptionRendering() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -99,7 +99,7 @@ public function testNormalExceptionRenderingQueryLog() ->expects($this->once()) ->method('getLogs') ->with() - ->will($this->returnValue(['query'])); + ->willReturn(['query']); $Controller = $this->getMockBuilder(Controller::class) ->onlyMethods(['render']) @@ -114,7 +114,7 @@ public function testNormalExceptionRenderingQueryLog() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -185,7 +185,7 @@ public function testNormalNestedExceptionRendering() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -246,7 +246,7 @@ public function testMissingViewExceptionDuringRendering() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -307,7 +307,7 @@ public function testGenericExceptionDuringRendering() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -372,7 +372,7 @@ public function testValidationErrorSingleKnownError() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -414,7 +414,7 @@ public function testValidationErrorSingleKnownErrorWithDebug() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); @@ -465,7 +465,7 @@ public function testValidationErrorMultipleMessages() ->expects($this->once()) ->method('_getController') ->with() - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Renderer->__construct($Exception); $Renderer->render(); diff --git a/tests/TestCase/Event/SubjectTest.php b/tests/TestCase/Event/SubjectTest.php index 5eaf1a1f7..832bc8b60 100644 --- a/tests/TestCase/Event/SubjectTest.php +++ b/tests/TestCase/Event/SubjectTest.php @@ -13,6 +13,8 @@ */ class SubjectTest extends TestCase { + protected Subject $Subject; + public function setup(): void { parent::setup(); diff --git a/tests/TestCase/Listener/ApiListenerTest.php b/tests/TestCase/Listener/ApiListenerTest.php index d123b5fad..1b3db1b3f 100644 --- a/tests/TestCase/Listener/ApiListenerTest.php +++ b/tests/TestCase/Listener/ApiListenerTest.php @@ -19,6 +19,7 @@ use Crud\Listener\ApiListener; use Crud\Test\App\Controller\BlogsController; use Crud\TestSuite\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use StdClass; /** @@ -46,7 +47,7 @@ public function testImplementedEvents() ->expects($this->once()) ->method('_checkRequestType') ->with('api') - ->will($this->returnValue(true)); + ->willReturn(true); $expected = [ 'Crud.beforeHandle' => ['callable' => [$listener, 'beforeHandle'], 'priority' => 10], @@ -78,7 +79,7 @@ public function testImplementedEventsWithoutApi() ->expects($this->once()) ->method('_checkRequestType') ->with('api') - ->will($this->returnValue(false)); + ->willReturn(false); $expected = []; $result = $listener->implementedEvents(); @@ -138,17 +139,17 @@ public function testResponse() ->expects($this->once()) ->method('_action') ->with() - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue(['code' => 200])); + ->willReturn(['code' => 200]); $listener ->expects($this->once()) ->method('render') ->with($subject) - ->will($this->returnValue($response)); + ->willReturn($response); $response ->expects($this->once()) ->method('withStatus') @@ -190,12 +191,12 @@ public function testResponseDeleteError() ->expects($this->once()) ->method('_action') ->with() - ->will($this->returnValue($action)); + ->willReturn($action); $listener ->expects($this->once()) ->method('render') ->with($subject) - ->will($this->returnValue($response)); + ->willReturn($response); $response ->expects($this->once()) ->method('withStatus') @@ -239,17 +240,17 @@ public function testResponseWithStatusCodeNotSpecified() ->expects($this->once()) ->method('_action') ->with() - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue(null)); + ->willReturn(null); $listener ->expects($this->once()) ->method('render') ->with($subject) - ->will($this->returnValue($response)); + ->willReturn($response); $response ->expects($this->never()) ->method('withStatus'); @@ -285,12 +286,12 @@ public function testResponseWithExceptionConfig() ->expects($this->once()) ->method('_action') ->with() - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue(['exception' => ['SomethingExceptional']])); + ->willReturn(['exception' => ['SomethingExceptional']]); $listener ->expects($this->once()) ->method('_exceptionResponse') @@ -405,7 +406,6 @@ public static function dataExceptionResponse() /** * Test _exceptionResponse * - * @dataProvider dataExceptionResponse * @param array $apiConfig * @param string $exceptionClass * @param string $exceptionMessage @@ -413,6 +413,7 @@ public static function dataExceptionResponse() * @param array $validationErrors * @return void */ + #[DataProvider('dataExceptionResponse')] public function testExceptionResponse( $apiConfig, $exceptionClass, @@ -438,7 +439,7 @@ public function testExceptionResponse( $event->getSubject()->entity ->expects($this->any()) ->method('getErrors') - ->will($this->returnValue($validationErrors)); + ->willReturn($validationErrors); } $this->expectException($exceptionClass, $exceptionMessage, $exceptionCode); @@ -475,15 +476,15 @@ public function testEnsureSerializeWithViewVar() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('viewVar') - ->will($this->returnValue('items')); + ->willReturn('items'); $this->setReflectionClassInstance($listener); $this->callProtectedMethod('_ensureSerialize', [], $listener); @@ -512,9 +513,9 @@ public static function dataSerializeTraitActions() /** * Test SerializeTrait * - * @dataProvider dataSerializeTraitActions * @return void */ + #[DataProvider('dataSerializeTraitActions')] public function testEnsureSerializeWithSerializeTrait($action) { $listener = $this @@ -538,11 +539,11 @@ public function testEnsureSerializeWithSerializeTrait($action) $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('setConfig') @@ -550,12 +551,12 @@ public function testEnsureSerializeWithSerializeTrait($action) $action ->expects($this->once()) ->method('viewVar') - ->will($this->returnValue(null)); + ->willReturn(null); $action ->expects($this->once()) ->method('getConfig') ->with('serialize') - ->will($this->returnValue(['something'])); + ->willReturn(['something']); $this->setReflectionClassInstance($action); $this->callProtectedMethod('serialize', [['something']], $action); @@ -599,7 +600,7 @@ public function testEnsureSerializeAlreadySet() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->never()) ->method('_action'); @@ -647,15 +648,15 @@ public function testEnsureSerializeWithViewVarChanged() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('viewVar') - ->will($this->returnValue('helloWorld')); + ->willReturn('helloWorld'); $this->setReflectionClassInstance($listener); $this->callProtectedMethod('_ensureSerialize', [], $listener); @@ -694,21 +695,21 @@ public function testEnsureSerializeWithoutViewVar() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $controller ->expects($this->any()) ->method('getName') - ->will($this->returnValue('')); + ->willReturn(''); $action->expects($this->any()) ->method('scope') - ->will($this->returnValue('table')); + ->willReturn('table'); $action->expects($this->any()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $this->setReflectionClassInstance($listener); $this->callProtectedMethod('_ensureSerialize', [], $listener); @@ -743,7 +744,7 @@ public function testEnsureSuccess() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $controller ->expects($this->once()) ->method('set') @@ -785,16 +786,16 @@ public function testEnsureData() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue($config)); + ->willReturn($config); $controller ->expects($this->once()) ->method('set') @@ -841,16 +842,16 @@ public function testEnsureDataSubject() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue($config)); + ->willReturn($config); $controller ->expects($this->once()) ->method('set') @@ -892,16 +893,16 @@ public function testEnsureDataRaw() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.success') - ->will($this->returnValue($config)); + ->willReturn($config); $controller ->expects($this->once()) ->method('set') @@ -943,16 +944,16 @@ public function testEnsureDataError() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api.error') - ->will($this->returnValue($config)); + ->willReturn($config); $controller ->expects($this->once()) ->method('set') @@ -988,7 +989,7 @@ public function testEnsureSuccessAlreadySet() $listener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $controller ->expects($this->never()) ->method('set'); @@ -1089,9 +1090,9 @@ public static function dataExpandPath() /** * testExpandPath * - * @dataProvider dataExpandPath * @return void */ + #[DataProvider('dataExpandPath')] public function testExpandPath($subject, $path, $expected) { $listener = $this @@ -1135,12 +1136,12 @@ public function testSetupDetectorsIntegration() $listener ->expects($this->once()) ->method('_request') - ->will($this->returnValue($request)); + ->willReturn($request); $listener ->expects($this->once()) ->method('getConfig') ->with('detectors') - ->will($this->returnValue($detectors)); + ->willReturn($detectors); $listener->setupDetectors(); @@ -1204,9 +1205,9 @@ public static function dataCheckRequestMethods() /** * testCheckRequestMethods * - * @dataProvider dataCheckRequestMethods * @return void */ + #[DataProvider('dataCheckRequestMethods')] public function testCheckRequestMethods($apiConfig, $exception, $requestMethods) { $listener = $this @@ -1230,18 +1231,18 @@ public function testCheckRequestMethods($apiConfig, $exception, $requestMethods) $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $action ->expects($this->once()) ->method('getConfig') ->with('api') - ->will($this->returnValue($apiConfig)); + ->willReturn($apiConfig); if (!empty($apiConfig['methods'])) { $listener ->expects($this->once()) ->method('_request') - ->will($this->returnValue($request)); + ->willReturn($request); $withs = $returns = []; foreach ($requestMethods as $method => $bool) { @@ -1321,12 +1322,12 @@ public function testInjectViewClasses() { $controller = $this ->getMockBuilder(Controller::class) - ->addMethods(['foobar', 'setViewClasses']) + ->onlyMethods(['addViewClasses']) ->disableOriginalConstructor() ->getMock(); $controller ->expects($this->once()) - ->method('setViewClasses') + ->method('addViewClasses') ->with(['json' => JsonView::class, 'xml' => XmlView::class]); $apiListener = $this->getMockBuilder(ApiListener::class) @@ -1337,7 +1338,7 @@ public function testInjectViewClasses() $apiListener ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($controller)); + ->willReturn($controller); $apiListener->injectViewClasses(); } diff --git a/tests/TestCase/Listener/ApiPaginationListenerTest.php b/tests/TestCase/Listener/ApiPaginationListenerTest.php index f8180c78d..733968d39 100644 --- a/tests/TestCase/Listener/ApiPaginationListenerTest.php +++ b/tests/TestCase/Listener/ApiPaginationListenerTest.php @@ -33,7 +33,7 @@ public function testImplementedEvents() $Instance ->expects($this->once()) ->method('_checkRequestType') - ->will($this->returnValue(true)); + ->willReturn(true); $result = $Instance->implementedEvents(); $expected = [ @@ -70,11 +70,11 @@ public function testBeforeRenderNoPaginationData() $Instance ->expects($this->once()) ->method('_action') - ->will($this->returnValue($Action)); + ->willReturn($Action); $Instance ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Instance->beforeRender(new Event('something')); @@ -110,7 +110,7 @@ public function testBeforeRenderViewVarNotPaginatedInterface() $Instance ->expects($this->once()) ->method('_action') - ->will($this->returnValue($Action)); + ->willReturn($Action); $Instance->beforeRender(new Event('something')); @@ -160,11 +160,11 @@ public function testBeforeRenderWithPaginationData() $Instance ->expects($this->any()) ->method('_controller') - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Instance ->expects($this->any()) ->method('_action') - ->will($this->returnValue($Action)); + ->willReturn($Action); $Instance->beforeRender(new Event('something')); diff --git a/tests/TestCase/Listener/ApiQueryLogListenerTest.php b/tests/TestCase/Listener/ApiQueryLogListenerTest.php index eaf3716e9..0cdf343db 100644 --- a/tests/TestCase/Listener/ApiQueryLogListenerTest.php +++ b/tests/TestCase/Listener/ApiQueryLogListenerTest.php @@ -51,7 +51,7 @@ public function testImplementedEvents() ->expects($this->once()) ->method('_checkRequestType') ->with('api') - ->will($this->returnValue(true)); + ->willReturn(true); $result = $Instance->implementedEvents(); $expected = [ @@ -78,7 +78,7 @@ public function testImplementedEventsNotApiRequest() ->expects($this->once()) ->method('_checkRequestType') ->with('api') - ->will($this->returnValue(false)); + ->willReturn(false); $result = $Instance->implementedEvents(); $expected = []; @@ -147,15 +147,15 @@ public function testBeforeRenderDebugTrue() $Instance ->expects($this->once()) ->method('_action') - ->will($this->returnValue($Action)); + ->willReturn($Action); $Instance ->expects($this->once()) ->method('_controller') - ->will($this->returnValue($Controller)); + ->willReturn($Controller); $Instance ->expects($this->once()) ->method('_getQueryLogs') - ->will($this->returnValue([])); + ->willReturn([]); $Instance->beforeRender(new Event('something')); } @@ -192,12 +192,12 @@ public function testSetupLogging() $Instance ->expects($this->once()) ->method('_getSources') - ->will($this->returnValue(['default'])); + ->willReturn(['default']); $Instance ->expects($this->any()) ->method('_getSource') ->with('default') - ->will($this->returnValue($DefaultSource)); + ->willReturn($DefaultSource); $Instance->setupLogging(new Event('something')); } @@ -246,7 +246,7 @@ public function testSetupLoggingConfiguredSources() ->expects($this->exactly(2)) ->method('_getSource') ->with(...self::withConsecutive(['default'], ['test'])) - ->willReturnOnConsecutiveCalls($this->returnValue($DefaultSource), $this->returnValue($TestSource)); + ->willReturnOnConsecutiveCalls($DefaultSource, $TestSource); $Instance->setConfig('connections', ['default', 'test']); $Instance->setupLogging(new Event('something')); diff --git a/tests/TestCase/Listener/RedirectListenerTest.php b/tests/TestCase/Listener/RedirectListenerTest.php index c38e21f7a..9f1325b3b 100644 --- a/tests/TestCase/Listener/RedirectListenerTest.php +++ b/tests/TestCase/Listener/RedirectListenerTest.php @@ -10,6 +10,7 @@ use Crud\Event\Subject; use Crud\Listener\RedirectListener; use Crud\TestSuite\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * Licensed under The MIT License @@ -158,7 +159,7 @@ public function testReaderRequestKey() $subject = new Subject(); $request = (new ServerRequest())->withParam('action', 'index'); - $listener->expects($this->any())->method('_request')->will($this->returnValue($request)); + $listener->expects($this->any())->method('_request')->willReturn($request); $reader = $listener->reader('request.key'); $result = $reader($subject, 'action'); @@ -186,7 +187,7 @@ public function testReaderRequestData() $subject = new Subject(); $request = (new ServerRequest())->withData('hello', 'world'); - $listener->expects($this->any())->method('_request')->will($this->returnValue($request)); + $listener->expects($this->any())->method('_request')->willReturn($request); $reader = $listener->reader('request.data'); $result = $reader($subject, 'hello'); @@ -214,7 +215,7 @@ public function testReaderRequestQuery() $subject = new Subject(); $request = (new ServerRequest())->withQueryParams(['hello' => 'world']); - $listener->expects($this->any())->method('_request')->will($this->returnValue($request)); + $listener->expects($this->any())->method('_request')->willReturn($request); $reader = $listener->reader('request.query'); $result = $reader($subject, 'hello'); @@ -249,7 +250,7 @@ public function testReaderEntityField() ->expects($this->once()) ->method('get') ->with('slug') - ->will($this->returnValue('ok-slug-is-ok')); + ->willReturn('ok-slug-is-ok'); $reader = $listener->reader('entity.field'); $result = $reader($subject, 'slug'); @@ -304,7 +305,7 @@ public function testRedirectWithNoConfig() $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $listener ->expects($this->never()) ->method('_getKey'); @@ -340,12 +341,12 @@ public function testRedirectWithConfigButNoValidKey() $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $listener ->expects($this->once()) ->method('_getKey') ->with($subject, 'request.key', 'hello') - ->will($this->returnValue(false)); + ->willReturn(false); $listener ->expects($this->never()) ->method('_getUrl'); @@ -383,17 +384,17 @@ public function testRedirectWithConfigAndValidKey() $listener ->expects($this->once()) ->method('_action') - ->will($this->returnValue($action)); + ->willReturn($action); $listener ->expects($this->once()) ->method('_getKey') ->with($subject, 'request.key', 'hello') - ->will($this->returnValue(true)); + ->willReturn(true); $listener ->expects($this->once()) ->method('_getUrl') ->with($subject, ['action' => 'index']) - ->will($this->returnValue(['action' => 'index'])); + ->willReturn(['action' => 'index']); $listener->beforeRedirect(new Event('Crud.beforeRedirect', $subject)); @@ -454,9 +455,9 @@ public static function dataProviderGetUrl() /** * Test _getUrl * - * @dataProvider dataProviderGetUrl * @return void */ + #[DataProvider('dataProviderGetUrl')] public function testGetUrl(Subject $subject, $url, $expected) { $listener = $this @@ -475,7 +476,7 @@ public function testGetUrl(Subject $subject, $url, $expected) ->expects($this->any()) ->method('_request') ->with() - ->will($this->returnValue($request)); + ->willReturn($request); $listener->setup(); $this->setReflectionClassInstance($listener); diff --git a/tests/TestCase/Listener/RelatedModelsListenerTest.php b/tests/TestCase/Listener/RelatedModelsListenerTest.php index 89e3914e4..37efc2cd4 100644 --- a/tests/TestCase/Listener/RelatedModelsListenerTest.php +++ b/tests/TestCase/Listener/RelatedModelsListenerTest.php @@ -39,7 +39,7 @@ public function testModels() ->expects($this->once()) ->method('relatedModels') ->with(null, null) - ->will($this->returnValue(null)); + ->willReturn(null); $result = $listener->models(); $expected = []; @@ -65,7 +65,7 @@ public function testModelsEmpty() ->expects($this->once()) ->method('relatedModels') ->with(null, null) - ->will($this->returnValue([])); + ->willReturn([]); $result = $listener->models(); $expected = []; @@ -90,7 +90,7 @@ public function testModelsString() ->expects($this->once()) ->method('relatedModels') ->with(null) - ->will($this->returnValue(['posts'])); + ->willReturn(['posts']); $listener ->expects($this->once()) ->method('getAssociatedByName') @@ -117,7 +117,7 @@ public function testModelsTrue() ->expects($this->once()) ->method('relatedModels') ->with(null, null) - ->will($this->returnValue(true)); + ->willReturn(true); $listener ->expects($this->once()) ->method('getAssociatedByType') @@ -160,28 +160,28 @@ public function testGetAssociatedByTypeReturnValue() ->expects($this->once()) ->method('_model') ->withAnyParameters() - ->will($this->returnValue($table)); + ->willReturn($table); $table ->expects($this->atLeastOnce()) ->method('associations') - ->will($this->returnValue($associationCollection)); + ->willReturn($associationCollection); $associationCollection ->expects($this->once()) ->method('keys') - ->will($this->returnValue(['posts'])); + ->willReturn(['posts']); $associationCollection ->expects($this->once()) ->method('get') ->with('posts') - ->will($this->returnValue($association)); + ->willReturn($association); $association ->expects($this->once()) ->method('getName') - ->will($this->returnValue('Posts')); + ->willReturn('Posts'); $association ->expects($this->once()) ->method('type') - ->will($this->returnValue('oneToOne')); + ->willReturn('oneToOne'); $expected = ['Posts' => $association]; $result = $listener->getAssociatedByType(['oneToOne', 'manyToMany', 'manyToOne']); @@ -223,20 +223,20 @@ public function testGetAssociatedByNameReturnValue() ->expects($this->once()) ->method('_model') ->withAnyParameters() - ->will($this->returnValue($table)); + ->willReturn($table); $table ->expects($this->once()) ->method('associations') - ->will($this->returnValue($associationCollection)); + ->willReturn($associationCollection); $associationCollection ->expects($this->once()) ->method('get') ->with('posts') - ->will($this->returnValue($association)); + ->willReturn($association); $association ->expects($this->once()) ->method('getName') - ->will($this->returnValue('Posts')); + ->willReturn('Posts'); $expected = ['Posts' => $association]; $result = $listener->getAssociatedByName(['posts']); @@ -263,7 +263,7 @@ public function testListFinder() $association ->expects($this->any()) ->method('getTarget') - ->will($this->returnValue($model)); + ->willReturn($model); $listener = $this ->getMockBuilder(RelatedModelsListener::class) @@ -296,12 +296,12 @@ public function testbeforePaginate() $table ->expects($this->any()) ->method('getSchema') - ->will($this->returnValue(new TableSchema('Users'))); + ->willReturn(new TableSchema('Users')); $listener ->expects($this->once()) ->method('models') - ->will($this->returnValue(['Users' => 'manyToOne'])); + ->willReturn(['Users' => 'manyToOne']); $query = new SelectQuery($table); $subject = new Subject(['query' => $query]); diff --git a/tests/TestCase/Listener/SearchListenerTest.php b/tests/TestCase/Listener/SearchListenerTest.php index b93cbc464..fb3e81f5e 100644 --- a/tests/TestCase/Listener/SearchListenerTest.php +++ b/tests/TestCase/Listener/SearchListenerTest.php @@ -77,7 +77,7 @@ public function testInjectSearchException() ->getMock(); $behaviorRegistryMock->expects($this->once()) ->method('has') - ->will($this->returnValue(false)); + ->willReturn(false); $tableMock = $this->getMockBuilder(Table::class) ->setMockClassName('SearchesTable') @@ -85,7 +85,7 @@ public function testInjectSearchException() ->getMock(); $tableMock->expects($this->any()) ->method('behaviors') - ->will($this->returnValue($behaviorRegistryMock)); + ->willReturn($behaviorRegistryMock); $this->getTableLocator()->set('Search', $tableMock); @@ -135,7 +135,7 @@ public function testInjectSearch() ->getMock(); $behaviorRegistryMock->expects($this->once()) ->method('has') - ->will($this->returnValue(true)); + ->willReturn(true); $tableMock = $this->getMockBuilder(Table::class) ->setMockClassName('SearchTables') @@ -143,7 +143,7 @@ public function testInjectSearch() ->getMock(); $tableMock->expects($this->any()) ->method('behaviors') - ->will($this->returnValue($behaviorRegistryMock)); + ->willReturn($behaviorRegistryMock); $this->getTableLocator()->set('Search', $tableMock); @@ -209,7 +209,7 @@ public function testInjectSearchWebserviceEndpoint() $queryMock->expects($this->once()) ->method('find') ->with('search', $params) - ->will($this->returnValue($queryMock)); + ->willReturn($queryMock); $subject = new Subject(); $subject->query = $queryMock; diff --git a/tests/test_app/src/Action/TestAction.php b/tests/test_app/src/Action/TestAction.php new file mode 100644 index 000000000..2d250bdd0 --- /dev/null +++ b/tests/test_app/src/Action/TestAction.php @@ -0,0 +1,13 @@ +