Если я правильно понял и вы хотите проверить соответствие роута конкретному экшну, то решение есть по проще.
<?php
namespace MyVendor\MyApplication\AppBundle\Tests;
use KoKoKo\assert\Assert;
use KoKoKo\assert\exceptions\InvalidNotEmptyException;
use KoKoKo\assert\exceptions\InvalidNotObjectException;
use KoKoKo\assert\exceptions\InvalidStringException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
trait ControllerTrait
{
/**
* @param Request $request
* @param string $controllerClass
* @param string $actionName
* @throws InvalidNotEmptyException
* @throws InvalidNotObjectException
* @throws InvalidStringException
*/
public function checkRoute(Request $request, $controllerClass, $actionName)
{
Assert::assert($controllerClass, 'controllerClass')->string()->notEmpty();
Assert::assert($actionName, 'actionName')->string()->notEmpty();
/** @noinspection PhpUndefinedMethodInspection */
static::$kernel->getContainer()->get('router_listener')->onKernelRequest(
new GetResponseEvent($this->getKernel(), $request, HttpKernelInterface::MASTER_REQUEST)
);
$parameters = $request->attributes->all();
/** @noinspection PhpUndefinedMethodInspection */
$this->assertInternalType('array', $parameters);
/** @noinspection PhpUndefinedMethodInspection */
$this->assertArrayHasKey('_controller', $parameters);
/** @noinspection PhpUndefinedMethodInspection */
$this->assertSame($controllerClass . '::' . $actionName, $parameters['_controller']);
}
}
Используется примерно так:
<?php
namespace MyVendor\MyApplication\AppBundle\Tests\Unit\Controller;
use MyVendor\MyApplication\AppBundle\Controller\MyController;
use MyVendor\MyApplication\AppBundle\Tests\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
class MyControllerTest extends KernelTestCase
{
use ControllerTrait;
public function testMyRoute()
{
self::bootKernel();
$request = new Request();
$request->server->set('REQUEST_URI', '/MyRoute');
$request->setMethod('POST');
$this->checkRoute($request, MyController::class, 'myRouteOfMyController');
}
}
Тут KoKoKo\assert\Assert - это
либа для валидации входящих аргументов