diff --git a/.travis.yml b/.travis.yml index 130c3bf..028138e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,7 @@ branches: - master php: - - 7.1 - 7.2 - - 7.3 - 7.4 env: @@ -18,9 +16,9 @@ matrix: fast_finish: true include: - - php: 7.2 + - php: 7.4 env: PHPCS=1 DEFAULT=0 - - php: 7.2 + - php: 7.4 env: PHPSTAN=1 DEFAULT=0 install: diff --git a/composer.json b/composer.json index 572bbd8..f4ab89c 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,12 @@ "type": "cakephp-plugin", "license": "MIT", "require": { - "cakephp/cakephp": ">=3.5.0 <4.0.0", - "ishanvyas22/asset-mix": "^0.6" + "cakephp/cakephp": "^4.1.0", + "ishanvyas22/asset-mix": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^5.7.14|^6.0", - "cakephp/cakephp-codesniffer": "^3.0", + "phpunit/phpunit": "~8.5.0", + "cakephp/cakephp-codesniffer": "^4.2", "phpstan/phpstan": "^0.12.37" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index db83917..08c1708 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,6 @@ colors="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="tests/bootstrap.php" > diff --git a/src/Controller/InertiaResponseTrait.php b/src/Controller/InertiaResponseTrait.php index c034ae4..e00dc36 100644 --- a/src/Controller/InertiaResponseTrait.php +++ b/src/Controller/InertiaResponseTrait.php @@ -1,8 +1,9 @@ isErrorStatus() || $this->isFailureStatus()) { return null; @@ -83,7 +84,7 @@ private function isFailureStatus() */ protected function setFlashData() { - /** @var \Cake\Http\Session */ + /** @var \Cake\Http\Session $session */ $session = $this->getRequest()->getSession(); $this->set('flash', function () use ($session) { @@ -108,6 +109,6 @@ protected function setFlashData() */ private function setCsrfToken() { - $this->set('_csrfToken', $this->getRequest()->getParam('_csrfToken')); + $this->set('_csrfToken', $this->getRequest()->getAttribute('csrfToken')); } } diff --git a/src/Middleware/InertiaMiddleware.php b/src/Middleware/InertiaMiddleware.php index 92dbd12..34d56c2 100644 --- a/src/Middleware/InertiaMiddleware.php +++ b/src/Middleware/InertiaMiddleware.php @@ -1,33 +1,36 @@ hasHeader('X-Inertia')) { - return $next($request, $response); + if (!$request->hasHeader('X-Inertia')) { + return $handler->handle($request); + } + if ($request instanceof ServerRequest) { + $this->setupDetectors($request); } - $this->setupDetectors($request); - - $response = $next($request, $response); - + $response = $handler->handle($request); if ( - $response instanceof ResponseInterface - && $response->getStatusCode() === Message::STATUS_FOUND + $response->getStatusCode() === Message::STATUS_FOUND && in_array($request->getMethod(), [Message::METHOD_PUT, Message::METHOD_PATCH, Message::METHOD_DELETE]) ) { $response = $response->withStatus(Message::STATUS_SEE_OTHER); @@ -44,7 +47,7 @@ public function __invoke($request, $response, $next) * @param \Cake\Http\ServerRequest $request The request. * @return void */ - private function setupDetectors($request) + private function setupDetectors(ServerRequest $request): void { $request->addDetector('inertia', function ($request) { return $request->hasHeader('X-Inertia'); diff --git a/src/Plugin.php b/src/Plugin.php index 38e3ff6..6d2e1c0 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,10 +1,12 @@ add(new InertiaMiddleware()); @@ -27,9 +29,9 @@ public function middleware($middleware) } /** - * {@inheritdoc} + * @inheritDoc */ - public function bootstrap(PluginApplicationInterface $app) + public function bootstrap(PluginApplicationInterface $app): void { parent::bootstrap($app); diff --git a/src/Utility/Message.php b/src/Utility/Message.php index cd34032..bbce5ee 100644 --- a/src/Utility/Message.php +++ b/src/Utility/Message.php @@ -1,4 +1,6 @@ getRequest()->getParam('controller'), - ucwords($this->getRequest()->getParam('action')) + ucwords((string)$this->getRequest()->getParam('action')) ); } @@ -51,7 +53,7 @@ private function getProps() { $props = []; $only = $this->getPartialData(); - $onlyViewVars = (! empty($only)) ? $only : array_keys($this->viewVars); + $onlyViewVars = ! empty($only) ? $only : array_keys($this->viewVars); $passedViewVars = $this->viewVars; $this->viewVars = []; diff --git a/src/View/Helper/InertiaHelper.php b/src/View/Helper/InertiaHelper.php index b35044a..6e04dd3 100644 --- a/src/View/Helper/InertiaHelper.php +++ b/src/View/Helper/InertiaHelper.php @@ -1,4 +1,6 @@ $this->getComponentName(), @@ -22,8 +23,8 @@ public function render($view = null, $layout = null) 'props' => $this->getProps(), ]; + $this->setConfig('serialize', 'page'); $this->set([ - '_serialize' => 'page', 'page' => $page, ]); diff --git a/src/View/InertiaWebView.php b/src/View/InertiaWebView.php index 2f819d2..a3ed15c 100644 --- a/src/View/InertiaWebView.php +++ b/src/View/InertiaWebView.php @@ -1,8 +1,9 @@ loadHelper('Inertia.Inertia'); $this->loadHelper('AssetMix.AssetMix'); @@ -23,7 +24,7 @@ public function initialize() /** * @inheritDoc */ - public function render($view = null, $layout = null) + public function render(?string $view = null, $layout = null): string { $page = [ 'component' => $this->getComponentName(), diff --git a/src/Template/Inertia/app.ctp b/templates/Inertia/app.php similarity index 100% rename from src/Template/Inertia/app.ctp rename to templates/Inertia/app.php diff --git a/tests/TestCase/Controller/UsersControllerTest.php b/tests/TestCase/Controller/UsersControllerTest.php index b55d287..2c41d28 100644 --- a/tests/TestCase/Controller/UsersControllerTest.php +++ b/tests/TestCase/Controller/UsersControllerTest.php @@ -1,4 +1,5 @@ middleware($middleware); - $this->assertInstanceOf(ErrorHandlerMiddleware::class, $middleware->get(0)); - $this->assertInstanceOf(AssetMiddleware::class, $middleware->get(1)); - $this->assertInstanceOf(BodyParserMiddleware::class, $middleware->get(2)); - $this->assertInstanceOf(RoutingMiddleware::class, $middleware->get(3)); + $results = iterator_to_array($middleware); + $this->assertInstanceOf(ErrorHandlerMiddleware::class, $results[0]); + $this->assertInstanceOf(AssetMiddleware::class, $results[1]); + $this->assertInstanceOf(BodyParserMiddleware::class, $results[2]); + $this->assertInstanceOf(RoutingMiddleware::class, $results[3]); } } diff --git a/tests/TestCase/View/Helper/InertiaHelperTest.php b/tests/TestCase/View/Helper/InertiaHelperTest.php index b2ca92f..6f166b5 100644 --- a/tests/TestCase/View/Helper/InertiaHelperTest.php +++ b/tests/TestCase/View/Helper/InertiaHelperTest.php @@ -1,4 +1,6 @@ Inertia = new InertiaHelper($view); } - /** - * tearDown method - * - * @return void - */ - public function tearDown() - { - unset($this->Inertia); - - parent::tearDown(); - } - /** * Test initial setup * @@ -62,8 +51,8 @@ public function testItReturnsRootTemplateDiv() $result = $this->Inertia->make($page, 'app', 'container'); - $this->assertContains('
assertContains('page="{"component":"Users', $result); - $this->assertContains('class="container">
', $result); + $this->assertStringContainsString('
assertStringContainsString('page="{"component":"Users', $result); + $this->assertStringContainsString('class="container">
', $result); } } diff --git a/tests/TestCase/View/InertiaJsonViewTest.php b/tests/TestCase/View/InertiaJsonViewTest.php index 3b189c7..be8bd47 100644 --- a/tests/TestCase/View/InertiaJsonViewTest.php +++ b/tests/TestCase/View/InertiaJsonViewTest.php @@ -1,4 +1,6 @@ View->render(); - $this->assertContains('
assertContains(htmlentities('"props":{"user":{"id":1,"name":"John Doe"}}'), $result); + $this->assertStringContainsString('
assertStringContainsString(htmlentities('"props":{"user":{"id":1,"name":"John Doe"}}'), $result); } public function testRendersComponentName() @@ -34,6 +36,6 @@ public function testRendersComponentName() $result = $this->View->render(); - $this->assertContains('"Users\/Index"', $result); + $this->assertStringContainsString('"Users\/Index"', $result); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4129c23..18ac9e4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1,12 @@ 'css/', 'paths' => [ 'plugins' => [TEST_APP_DIR . 'plugins' . DS], - 'templates' => [TEST_APP_DIR . 'src' . DS . 'Template' . DS], + 'templates' => [TEST_APP_DIR . 'templates' . DS], ], ]); Configure::write('Error', [ @@ -82,5 +84,6 @@ 'timezone' => 'UTC', ]; ConnectionManager::setConfig('test', $config); +Security::setSalt('a long value no one will guess ever'); Plugin::getCollection()->add(new \Inertia\Plugin()); diff --git a/tests/test_app/config/routes.php b/tests/test_app/config/routes.php index f2aa769..a2b09d7 100644 --- a/tests/test_app/config/routes.php +++ b/tests/test_app/config/routes.php @@ -1,16 +1,16 @@ registerMiddleware('csrf', new CsrfProtectionMiddleware([ - 'httpOnly' => true, + 'httponly' => true, ])); $routes->applyMiddleware('csrf'); diff --git a/tests/test_app/src/Application.php b/tests/test_app/src/Application.php index ea7c527..adb39e8 100644 --- a/tests/test_app/src/Application.php +++ b/tests/test_app/src/Application.php @@ -1,4 +1,5 @@ add(new ErrorHandlerMiddleware(null, Configure::read('Error'))) + ->add(new ErrorHandlerMiddleware(Configure::read('Error'))) ->add(new AssetMiddleware()) ->add(new BodyParserMiddleware()) ->add(new RoutingMiddleware($this)); diff --git a/tests/test_app/src/Controller/AppController.php b/tests/test_app/src/Controller/AppController.php index 4e2219b..add2846 100644 --- a/tests/test_app/src/Controller/AppController.php +++ b/tests/test_app/src/Controller/AppController.php @@ -1,16 +1,17 @@ loadComponent('Flash'); } - public function beforeFilter(Event $event) + public function beforeFilter(EventInterface $event) { parent::beforeFilter($event); diff --git a/tests/test_app/src/Controller/ErrorController.php b/tests/test_app/src/Controller/ErrorController.php index 52ef31d..7413402 100644 --- a/tests/test_app/src/Controller/ErrorController.php +++ b/tests/test_app/src/Controller/ErrorController.php @@ -1,8 +1,9 @@ loadComponent('RequestHandler', [ 'enableBeforeRedirect' => false, @@ -31,20 +32,20 @@ public function initialize() /** * beforeFilter callback. * - * @param \Cake\Event\Event $event Event. + * @param \Cake\Event\EventInterface $event Event. * @return \Cake\Http\Response|null|void */ - public function beforeFilter(Event $event) + public function beforeFilter(EventInterface $event) { } /** * beforeRender callback. * - * @param \Cake\Event\Event $event Event. + * @param \Cake\Event\EventInterface $event Event. * @return \Cake\Http\Response|null|void */ - public function beforeRender(Event $event) + public function beforeRender(EventInterface $event) { parent::beforeRender($event); @@ -54,10 +55,10 @@ public function beforeRender(Event $event) /** * afterFilter callback. * - * @param \Cake\Event\Event $event Event. + * @param \Cake\Event\EventInterface $event Event. * @return \Cake\Http\Response|null|void */ - public function afterFilter(Event $event) + public function afterFilter(EventInterface $event) { } } diff --git a/tests/test_app/src/Controller/UsersController.php b/tests/test_app/src/Controller/UsersController.php index a943a13..c6bcd7b 100644 --- a/tests/test_app/src/Controller/UsersController.php +++ b/tests/test_app/src/Controller/UsersController.php @@ -1,4 +1,5 @@