diff --git a/.editorconfig b/.editorconfig index 5e9a93e..ad466f0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,10 @@ indent_style = space indent_size = 4 trim_trailing_whitespace = true +[*.php] +ij_php_space_before_short_closure_left_parenthesis = true +ij_php_space_after_type_cast = true + [*.md] trim_trailing_whitespace = false diff --git a/CHANGELOG.md b/CHANGELOG.md index 3787096..477a9ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 5.0.1 under development -- no changes in this release. +- Bug #61: Fixed getting incorrect controller name based on controller instance (@vjik, @kamarton) ## 5.0.0 July 23, 2022 diff --git a/src/ViewRenderer.php b/src/ViewRenderer.php index e9512df..00b828e 100644 --- a/src/ViewRenderer.php +++ b/src/ViewRenderer.php @@ -561,7 +561,7 @@ private function extractControllerName(object $controller): string return $cache[$class]; } - $regexp = '/((?<=controller\\\|s\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU'; + $regexp = '/((?<=controller\\\|controllers\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU'; if (!preg_match($regexp, $class, $m) || empty($m[1])) { throw new RuntimeException('Cannot detect controller name.'); } diff --git a/tests/Support/Controller/SubNamespace/FakeController.php b/tests/Support/Controller/SubNamespace/FakeController.php new file mode 100644 index 0000000..55d81e2 --- /dev/null +++ b/tests/Support/Controller/SubNamespace/FakeController.php @@ -0,0 +1,9 @@ +assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody()); - $this->assertEqualStringsIgnoringLineEndings($expected, $renderer->renderAsString('view', [ - 'name' => 'donatello', - ])); + $this->assertEqualStringsIgnoringLineEndings( + $expected, + $renderer->renderAsString('view', [ + 'name' => 'donatello', + ]) + ); } public function testRenderWithAbsoluteLayoutPath(): void @@ -175,7 +179,44 @@ public function testRenderWithLocale(): void $this->assertSame('
de_DE locale', (string) $response->getBody()); } - public function testWithController(): void + public function dataWithController(): array + { + return [ + 'controller name, no "controller" / "controllers" namespaces, no subnamespaces' => [new Support\FakeController(), '/fake'], + 'controller name, "controller" namespace, 1 subnamespace' => [ + new Support\Controller\SubNamespace\FakeController(), + '/sub-namespace/fake', + ], + 'controller name, "controllers" namespace, 1 subnamespace' => [ + new Support\Controllers\SubNamespace\FakeController(), + '/sub-namespace/fake', + ], + 'controller name, "controller" namespace, 2 subnamespaces' => [ + new Support\Controller\SubNamespace\SubNamespace2\FakeController(), + '/sub-namespace/sub-namespace2/fake', + ], + 'controller name, "controllers" namespace, 2 subnamespaces' => [ + new Support\Controllers\SubNamespace\SubNamespace2\FakeController(), + '/sub-namespace/sub-namespace2/fake', + ], + 'controller name, without "controller" / "controllers" namespaces, subnamespaces' => [ + new Support\NotCntrls\SubNamespace\FakeController(), + '/fake', + ], + ]; + } + + /** + * @dataProvider dataWithController + */ + public function testWithController(object $controller, string $path): void + { + $renderer = $this->getRenderer()->withController($controller); + + $this->assertSame($this->getViewsDir() . $path, $renderer->getViewPath()); + } + + public function testTwiceWithController(): void { $controller = new FakeController(); @@ -184,16 +225,27 @@ public function testWithController(): void ->withController($controller) ->withController($controller); // twice for test of cache - $this->assertSame($this->getViewsDir() . '/support/fake', $renderer->getViewPath()); + $this->assertSame($this->getViewsDir() . '/fake', $renderer->getViewPath()); } - public function testWithIncorrectController(): void + public function dataWithIncorrectController(): array + { + return [ + 'stdClass' => [new stdClass()], + 'withNamespace' => [new FakeCntrl()], + ]; + } + + /** + * @dataProvider dataWithIncorrectController + */ + public function testWithIncorrectController(object $controller): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Cannot detect controller name.'); $this ->getRenderer() - ->withController(new stdClass()); + ->withController($controller); } public function testWithViewPath(): void @@ -221,7 +273,7 @@ public function testWithViewPathWithController(): void ->withViewPath('/dir//') ->withController(new FakeController()); - $this->assertSame('/dir/support/fake', $renderer->getViewPath()); + $this->assertSame('/dir/fake', $renderer->getViewPath()); } public function testInvalidMetaTag(): void @@ -278,7 +330,7 @@ public function testCommonParametersInjectionsToNestedViews(): void $response = $renderer->render('nested/root', ['label' => 'root']); - $this->assertSame('root: leonardo. nested-1: leonardo. nested-2: leonardo.', (string)$response->getBody()); + $this->assertSame('root: leonardo. nested-1: leonardo. nested-2: leonardo.', (string) $response->getBody()); } public function testLayoutParametersInjectionsToNestedViews(): void @@ -292,7 +344,7 @@ public function testLayoutParametersInjectionsToNestedViews(): void $this->assertSame( '