Skip to content

Commit

Permalink
bug #4164 #4163 Allow to use arrays in routing controller configurati…
Browse files Browse the repository at this point in the history
…on (edefimov)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

#4163 Allow to use arrays in routing controller configuration

Fix for #4163

Commits
-------

a6fd3bf #4163 Allow to use arrays in routing controller configuration
  • Loading branch information
javiereguiluz committed Jan 30, 2021
2 parents 092bdfb + a6fd3bf commit dd7f6ca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/EventListener/AdminRouterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,18 @@ public function onKernelController(ControllerEvent $event): void
*/
private function getDashboardControllerFqcn(Request $request): ?string
{
[$controllerFqcn, ] = explode('::', $request->attributes->get('_controller'));
$controllerFqcn = $request->attributes->get('_controller');
if (\is_string($controllerFqcn)) {
[$controllerFqcn, ] = explode('::', $controllerFqcn);
}

if (\is_array($controllerFqcn)) {
$controllerFqcn = $controllerFqcn[0];
}

if (\is_object($controllerFqcn)) {
$controllerFqcn = \get_class($controllerFqcn);
}

return is_subclass_of($controllerFqcn, DashboardControllerInterface::class) ? $controllerFqcn : null;
}
Expand Down
64 changes: 64 additions & 0 deletions tests/EventListener/AdminRouterSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\EventListener;

use EasyCorp\Bundle\EasyAdminBundle\EventListener\AdminRouterSubscriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Class AdminRouterSubscriberTest.
*/
final class AdminRouterSubscriberTest extends TestCase
{
/**
* @var AdminRouterSubscriber
*/
private $subscriber;

protected function setUp(): void
{
parent::setUp();

$this->subscriber = (new \ReflectionClass(AdminRouterSubscriber::class))->newInstanceWithoutConstructor();
}

/**
* testFetchingControllerFromRequestAttributes.
*
* @param $controller
*
* @dataProvider controllerDataProvider
*
* @doesNotPerformAssertions
*/
public function testFetchingControllerFromRequestAttributes($controller): void
{
$request = Request::create('/');
$request->attributes->set('_controller', $controller);

$this->subscriber->onKernelRequest(
new RequestEvent(
$this->getMockForAbstractClass(HttpKernelInterface::class),
$request,
HttpKernelInterface::MASTER_REQUEST
)
);
}

public static function controllerDataProvider(): iterable
{
yield ['Some\\Class::method'];
yield ['Some\\Class'];
yield [['Some\\Class', 'method']];
yield [new class() {
public function __invoke()
{
}
}];
}
}

0 comments on commit dd7f6ca

Please sign in to comment.