Skip to content

Commit

Permalink
Merge pull request #71 from APY/feature/invokable-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanlaak authored Nov 16, 2021
2 parents 51a7b9a + 8cd09b9 commit aeda2de
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=4" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=6" />
<env name="SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT " value="1" />
</php>

Expand Down
11 changes: 6 additions & 5 deletions src/EventListener/BreadcrumbListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions tests/EventListener/BreadcrumbListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
21 changes: 21 additions & 0 deletions tests/Fixtures/InvokableControllerWithAnnotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace APY\BreadcrumbTrailBundle\Fixtures;

use APY\BreadcrumbTrailBundle\Annotation\Breadcrumb;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* @Breadcrumb("first-breadcrumb")
* @Breadcrumb("second-breadcrumb")
*/
class InvokableControllerWithAnnotations extends AbstractController
{
/**
* @Breadcrumb("third-breadcrumb")
*/
public function __invoke()
{
return [];
}
}

0 comments on commit aeda2de

Please sign in to comment.