From 8cd09b963be173de4cff4df44563f518a37192e0 Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Tue, 16 Nov 2021 13:41:10 +0100 Subject: [PATCH] support breadcrumbs on invokable controllers --- README.md | 5 +++++ phpunit.xml.dist | 2 +- src/EventListener/BreadcrumbListener.php | 11 +++++----- .../EventListener/BreadcrumbListenerTest.php | 17 +++++++++++++-- .../InvokableControllerWithAnnotations.php | 21 +++++++++++++++++++ 5 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/Fixtures/InvokableControllerWithAnnotations.php diff --git a/README.md b/README.md index 5f14933..98b54cb 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,11 @@ $ make test-php73 $ make test-php74-lowest ``` +In case all test suites pass but running tests still returns an error code, that +might be related to the number of allowed deprecations. Make sure that the +`SYMFONY_DEPRECATIONS_HELPER` value of `max[self]` as found in `phpunit.xml.dist` +matches the "Remaining self deprecation notices" count from the test runner output. + ## Code style PHP-CS-Fixer is used to keep the code style in shape. There is a make target that uses Docker to fix diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 38d8027..5938b32 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,7 +14,7 @@ - + diff --git a/src/EventListener/BreadcrumbListener.php b/src/EventListener/BreadcrumbListener.php index a2649ef..ed13a90 100644 --- a/src/EventListener/BreadcrumbListener.php +++ b/src/EventListener/BreadcrumbListener.php @@ -47,12 +47,13 @@ public function __construct(Reader $reader, Trail $breadcrumbTrail) */ public function onKernelController(KernelEvent $event) { - if (!\is_array($controller = $event->getController())) { - return; - } + $controller = $event->getController(); + + $reflectableClass = \is_array($controller) ? $controller[0] : \get_class($controller); + $reflectableMethod = \is_array($controller) ? $controller[1] : '__invoke'; // Annotations from class - $class = new \ReflectionClass($controller[0]); + $class = new \ReflectionClass($reflectableClass); // Manage JMSSecurityExtraBundle proxy class if (false !== $className = $this->getRealClass($class->getName())) { @@ -83,7 +84,7 @@ public function onKernelController(KernelEvent $event) $this->addBreadcrumbsToTrail($classBreadcrumbs); // Annotations from method - $method = $class->getMethod($controller[1]); + $method = $class->getMethod($reflectableMethod); $methodBreadcrumbs = $this->reader->getMethodAnnotations($method); if ($this->supportsLoadingAttributes()) { $methodAttributeBreadcrumbs = $this->getAttributes($method); diff --git a/tests/EventListener/BreadcrumbListenerTest.php b/tests/EventListener/BreadcrumbListenerTest.php index 1f1a9c6..53d7ae1 100644 --- a/tests/EventListener/BreadcrumbListenerTest.php +++ b/tests/EventListener/BreadcrumbListenerTest.php @@ -7,6 +7,7 @@ use APY\BreadcrumbTrailBundle\Fixtures\ControllerWithAnnotations; use APY\BreadcrumbTrailBundle\Fixtures\ControllerWithAttributes; use APY\BreadcrumbTrailBundle\Fixtures\ControllerWithAttributesAndAnnotations; +use APY\BreadcrumbTrailBundle\Fixtures\InvokableControllerWithAnnotations; use APY\BreadcrumbTrailBundle\MixedAnnotationWithAttributeBreadcrumbsException; use Nyholm\BundleTest\AppKernel; use Nyholm\BundleTest\BaseBundleTestCase; @@ -74,6 +75,17 @@ public function testMixingAnnotationsWithAttributesFails() $this->listener->onKernelController($kernelEvent); } + public function testInvokableController() + { + $this->setUpTest(); + + $controller = new InvokableControllerWithAnnotations(); + $kernelEvent = $this->createControllerEvent($controller); + $this->listener->onKernelController($kernelEvent); + + self::assertCount(3, $this->breadcrumbTrail); + } + protected function getBundleClass() { return APYBreadcrumbTrailBundle::class; @@ -84,10 +96,11 @@ protected function getBundleClass() */ private function createControllerEvent($controller) { + $callable = \is_callable($controller) ? $controller : [$controller, 'indexAction']; if (Kernel::MAJOR_VERSION <= 4) { - return new FilterControllerEvent($this->kernel, [$controller, 'indexAction'], new Request(), HttpKernelInterface::MASTER_REQUEST); + return new FilterControllerEvent($this->kernel, $callable, new Request(), HttpKernelInterface::MASTER_REQUEST); } - return new ControllerEvent($this->kernel, [$controller, 'indexAction'], new Request(), HttpKernelInterface::MASTER_REQUEST); + return new ControllerEvent($this->kernel, $callable, new Request(), HttpKernelInterface::MASTER_REQUEST); } } diff --git a/tests/Fixtures/InvokableControllerWithAnnotations.php b/tests/Fixtures/InvokableControllerWithAnnotations.php new file mode 100644 index 0000000..2e78970 --- /dev/null +++ b/tests/Fixtures/InvokableControllerWithAnnotations.php @@ -0,0 +1,21 @@ +