Initial Commit
This commit is contained in:
158
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolver/ServiceValueResolverTest.php
vendored
Normal file
158
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolver/ServiceValueResolverTest.php
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\ServiceLocator;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
|
||||
|
||||
class ServiceValueResolverTest extends TestCase
|
||||
{
|
||||
public function testDoNotSupportWhenControllerDoNotExists()
|
||||
{
|
||||
$resolver = new ServiceValueResolver(new ServiceLocator(array()));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
$request = $this->requestWithAttributes(array('_controller' => 'my_controller'));
|
||||
|
||||
$this->assertFalse($resolver->supports($request, $argument));
|
||||
}
|
||||
|
||||
public function testExistingController()
|
||||
{
|
||||
$resolver = new ServiceValueResolver(new ServiceLocator(array(
|
||||
'App\\Controller\\Mine::method' => function () {
|
||||
return new ServiceLocator(array(
|
||||
'dummy' => function () {
|
||||
return new DummyService();
|
||||
},
|
||||
));
|
||||
},
|
||||
)));
|
||||
|
||||
$request = $this->requestWithAttributes(array('_controller' => 'App\\Controller\\Mine::method'));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
|
||||
$this->assertTrue($resolver->supports($request, $argument));
|
||||
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
|
||||
}
|
||||
|
||||
public function testExistingControllerWithATrailingBackSlash()
|
||||
{
|
||||
$resolver = new ServiceValueResolver(new ServiceLocator(array(
|
||||
'App\\Controller\\Mine::method' => function () {
|
||||
return new ServiceLocator(array(
|
||||
'dummy' => function () {
|
||||
return new DummyService();
|
||||
},
|
||||
));
|
||||
},
|
||||
)));
|
||||
|
||||
$request = $this->requestWithAttributes(array('_controller' => '\\App\\Controller\\Mine::method'));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
|
||||
$this->assertTrue($resolver->supports($request, $argument));
|
||||
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
|
||||
}
|
||||
|
||||
public function testExistingControllerWithMethodNameStartUppercase()
|
||||
{
|
||||
$resolver = new ServiceValueResolver(new ServiceLocator(array(
|
||||
'App\\Controller\\Mine::method' => function () {
|
||||
return new ServiceLocator(array(
|
||||
'dummy' => function () {
|
||||
return new DummyService();
|
||||
},
|
||||
));
|
||||
},
|
||||
)));
|
||||
$request = $this->requestWithAttributes(array('_controller' => 'App\\Controller\\Mine::Method'));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
|
||||
$this->assertTrue($resolver->supports($request, $argument));
|
||||
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
|
||||
}
|
||||
|
||||
public function testControllerNameIsAnArray()
|
||||
{
|
||||
$resolver = new ServiceValueResolver(new ServiceLocator(array(
|
||||
'App\\Controller\\Mine::method' => function () {
|
||||
return new ServiceLocator(array(
|
||||
'dummy' => function () {
|
||||
return new DummyService();
|
||||
},
|
||||
));
|
||||
},
|
||||
)));
|
||||
|
||||
$request = $this->requestWithAttributes(array('_controller' => array('App\\Controller\\Mine', 'method')));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
|
||||
$this->assertTrue($resolver->supports($request, $argument));
|
||||
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
||||
* @expectedExceptionMessage Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.
|
||||
*/
|
||||
public function testErrorIsTruncated()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
|
||||
|
||||
$container->register('argument_resolver.service', ServiceValueResolver::class)->addArgument(null)->setPublic(true);
|
||||
$container->register(DummyController::class)->addTag('controller.service_arguments')->setPublic(true);
|
||||
|
||||
$container->compile();
|
||||
|
||||
$request = $this->requestWithAttributes(array('_controller' => array(DummyController::class, 'index')));
|
||||
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
|
||||
$container->get('argument_resolver.service')->resolve($request, $argument)->current();
|
||||
}
|
||||
|
||||
private function requestWithAttributes(array $attributes)
|
||||
{
|
||||
$request = Request::create('/');
|
||||
|
||||
foreach ($attributes as $name => $value) {
|
||||
$request->attributes->set($name, $value);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
private function assertYieldEquals(array $expected, \Generator $generator)
|
||||
{
|
||||
$args = array();
|
||||
foreach ($generator as $arg) {
|
||||
$args[] = $arg;
|
||||
}
|
||||
|
||||
$this->assertEquals($expected, $args);
|
||||
}
|
||||
}
|
||||
|
||||
class DummyService
|
||||
{
|
||||
}
|
||||
|
||||
class DummyController
|
||||
{
|
||||
public function index(DummyService $dummy)
|
||||
{
|
||||
}
|
||||
}
|
||||
76
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolver/TraceableValueResolverTest.php
vendored
Normal file
76
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolver/TraceableValueResolverTest.php
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
|
||||
class TraceableValueResolverTest extends TestCase
|
||||
{
|
||||
public function testTimingsInSupports()
|
||||
{
|
||||
$stopwatch = new Stopwatch();
|
||||
$resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch);
|
||||
$argument = new ArgumentMetadata('dummy', 'string', false, false, null);
|
||||
$request = new Request();
|
||||
|
||||
$this->assertTrue($resolver->supports($request, $argument));
|
||||
|
||||
$event = $stopwatch->getEvent(ResolverStub::class.'::supports');
|
||||
$this->assertCount(1, $event->getPeriods());
|
||||
}
|
||||
|
||||
public function testTimingsInResolve()
|
||||
{
|
||||
$stopwatch = new Stopwatch();
|
||||
$resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch);
|
||||
$argument = new ArgumentMetadata('dummy', 'string', false, false, null);
|
||||
$request = new Request();
|
||||
|
||||
$iterable = $resolver->resolve($request, $argument);
|
||||
|
||||
foreach ($iterable as $index => $resolved) {
|
||||
$event = $stopwatch->getEvent(ResolverStub::class.'::resolve');
|
||||
$this->assertTrue($event->isStarted());
|
||||
$this->assertEmpty($event->getPeriods());
|
||||
switch ($index) {
|
||||
case 0:
|
||||
$this->assertEquals('first', $resolved);
|
||||
break;
|
||||
case 1:
|
||||
$this->assertEquals('second', $resolved);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$event = $stopwatch->getEvent(ResolverStub::class.'::resolve');
|
||||
$this->assertCount(1, $event->getPeriods());
|
||||
}
|
||||
}
|
||||
|
||||
class ResolverStub implements ArgumentValueResolverInterface
|
||||
{
|
||||
public function supports(Request $request, ArgumentMetadata $argument)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function resolve(Request $request, ArgumentMetadata $argument)
|
||||
{
|
||||
yield 'first';
|
||||
yield 'second';
|
||||
}
|
||||
}
|
||||
338
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolverTest.php
vendored
Normal file
338
vendor/symfony/http-kernel/Tests/Controller/ArgumentResolverTest.php
vendored
Normal file
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests\Controller;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingRequest;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\ExtendingSession;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
|
||||
|
||||
class ArgumentResolverTest extends TestCase
|
||||
{
|
||||
/** @var ArgumentResolver */
|
||||
private static $resolver;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
$factory = new ArgumentMetadataFactory();
|
||||
|
||||
self::$resolver = new ArgumentResolver($factory);
|
||||
}
|
||||
|
||||
public function testDefaultState()
|
||||
{
|
||||
$this->assertEquals(self::$resolver, new ArgumentResolver());
|
||||
$this->assertNotEquals(self::$resolver, new ArgumentResolver(null, array(new RequestAttributeValueResolver())));
|
||||
}
|
||||
|
||||
public function testGetArguments()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = array(new self(), 'controllerWithFoo');
|
||||
|
||||
$this->assertEquals(array('foo'), self::$resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
|
||||
}
|
||||
|
||||
public function testGetArgumentsReturnsEmptyArrayWhenNoArguments()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$controller = array(new self(), 'controllerWithoutArguments');
|
||||
|
||||
$this->assertEquals(array(), self::$resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
|
||||
}
|
||||
|
||||
public function testGetArgumentsUsesDefaultValue()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = array(new self(), 'controllerWithFooAndDefaultBar');
|
||||
|
||||
$this->assertEquals(array('foo', null), self::$resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
|
||||
}
|
||||
|
||||
public function testGetArgumentsOverrideDefaultValueByRequestAttribute()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', 'bar');
|
||||
$controller = array(new self(), 'controllerWithFooAndDefaultBar');
|
||||
|
||||
$this->assertEquals(array('foo', 'bar'), self::$resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
|
||||
}
|
||||
|
||||
public function testGetArgumentsFromClosure()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = function ($foo) {};
|
||||
|
||||
$this->assertEquals(array('foo'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetArgumentsUsesDefaultValueFromClosure()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = function ($foo, $bar = 'bar') {};
|
||||
|
||||
$this->assertEquals(array('foo', 'bar'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetArgumentsFromInvokableObject()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = new self();
|
||||
|
||||
$this->assertEquals(array('foo', null), self::$resolver->getArguments($request, $controller));
|
||||
|
||||
// Test default bar overridden by request attribute
|
||||
$request->attributes->set('bar', 'bar');
|
||||
|
||||
$this->assertEquals(array('foo', 'bar'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetArgumentsFromFunctionName()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('foobar', 'foobar');
|
||||
$controller = __NAMESPACE__.'\controller_function';
|
||||
|
||||
$this->assertEquals(array('foo', 'foobar'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetArgumentsFailsOnUnresolvedValue()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('foobar', 'foobar');
|
||||
$controller = array(new self(), 'controllerWithFooBarFoobar');
|
||||
|
||||
try {
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
$this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetArgumentsInjectsRequest()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$controller = array(new self(), 'controllerWithRequest');
|
||||
|
||||
$this->assertEquals(array($request), self::$resolver->getArguments($request, $controller), '->getArguments() injects the request');
|
||||
}
|
||||
|
||||
public function testGetArgumentsInjectsExtendingRequest()
|
||||
{
|
||||
$request = ExtendingRequest::create('/');
|
||||
$controller = array(new self(), 'controllerWithExtendingRequest');
|
||||
|
||||
$this->assertEquals(array($request), self::$resolver->getArguments($request, $controller), '->getArguments() injects the request when extended');
|
||||
}
|
||||
|
||||
public function testGetVariadicArguments()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', array('foo', 'bar'));
|
||||
$controller = array(new VariadicController(), 'action');
|
||||
|
||||
$this->assertEquals(array('foo', 'foo', 'bar'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testGetVariadicArgumentsWithoutArrayInRequest()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', 'foo');
|
||||
$controller = array(new VariadicController(), 'action');
|
||||
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testGetArgumentWithoutArray()
|
||||
{
|
||||
$factory = new ArgumentMetadataFactory();
|
||||
$valueResolver = $this->getMockBuilder(ArgumentValueResolverInterface::class)->getMock();
|
||||
$resolver = new ArgumentResolver($factory, array($valueResolver));
|
||||
|
||||
$valueResolver->expects($this->any())->method('supports')->willReturn(true);
|
||||
$valueResolver->expects($this->any())->method('resolve')->willReturn('foo');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', 'foo');
|
||||
$controller = array($this, 'controllerWithFooAndDefaultBar');
|
||||
$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testIfExceptionIsThrownWhenMissingAnArgument()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$controller = array($this, 'controllerWithFoo');
|
||||
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
public function testGetNullableArguments()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', new \stdClass());
|
||||
$request->attributes->set('mandatory', 'mandatory');
|
||||
$controller = array(new NullableController(), 'action');
|
||||
|
||||
$this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetNullableArgumentsWithDefaults()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('mandatory', 'mandatory');
|
||||
$controller = array(new NullableController(), 'action');
|
||||
|
||||
$this->assertEquals(array(null, null, 'value', 'mandatory'), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetSessionArguments()
|
||||
{
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
$controller = array($this, 'controllerWithSession');
|
||||
|
||||
$this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetSessionArgumentsWithExtendedSession()
|
||||
{
|
||||
$session = new ExtendingSession(new MockArraySessionStorage());
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
$controller = array($this, 'controllerWithExtendingSession');
|
||||
|
||||
$this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testGetSessionArgumentsWithInterface()
|
||||
{
|
||||
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
$controller = array($this, 'controllerWithSessionInterface');
|
||||
|
||||
$this->assertEquals(array($session), self::$resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetSessionMissMatchWithInterface()
|
||||
{
|
||||
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
$controller = array($this, 'controllerWithExtendingSession');
|
||||
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetSessionMissMatchWithImplementation()
|
||||
{
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
$controller = array($this, 'controllerWithExtendingSession');
|
||||
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testGetSessionMissMatchOnNull()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$controller = array($this, 'controllerWithExtendingSession');
|
||||
|
||||
self::$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
public function __invoke($foo, $bar = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function controllerWithFoo($foo)
|
||||
{
|
||||
}
|
||||
|
||||
public function controllerWithoutArguments()
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithFooAndDefaultBar($foo, $bar = null)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithFooBarFoobar($foo, $bar, $foobar)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithRequest(Request $request)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithExtendingRequest(ExtendingRequest $request)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithSession(Session $session)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithSessionInterface(SessionInterface $session)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerWithExtendingSession(ExtendingSession $session)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
function controller_function($foo, $foobar)
|
||||
{
|
||||
}
|
||||
241
vendor/symfony/http-kernel/Tests/Controller/ContainerControllerResolverTest.php
vendored
Normal file
241
vendor/symfony/http-kernel/Tests/Controller/ContainerControllerResolverTest.php
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
|
||||
|
||||
class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
{
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Referencing controllers with a single colon is deprecated since Symfony 4.1. Use foo::action instead.
|
||||
*/
|
||||
public function testGetControllerServiceWithSingleColon()
|
||||
{
|
||||
$service = new ControllerTestService('foo');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('foo')
|
||||
->will($this->returnValue(true));
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue($service))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'foo:action');
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame($service, $controller[0]);
|
||||
$this->assertSame('action', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerService()
|
||||
{
|
||||
$service = new ControllerTestService('foo');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('foo')
|
||||
->will($this->returnValue(true));
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue($service))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'foo::action');
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame($service, $controller[0]);
|
||||
$this->assertSame('action', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerInvokableService()
|
||||
{
|
||||
$service = new InvokableControllerService('bar');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('foo')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->will($this->returnValue($service))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'foo');
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame($service, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerInvokableServiceWithClassNameAsName()
|
||||
{
|
||||
$service = new InvokableControllerService('bar');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with(InvokableControllerService::class)
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with(InvokableControllerService::class)
|
||||
->will($this->returnValue($service))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', InvokableControllerService::class);
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame($service, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests where the fallback instantiation fails due to required constructor arguments.
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
|
||||
*/
|
||||
public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
|
||||
{
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with(ControllerTestService::class)
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$container->expects($this->atLeastOnce())
|
||||
->method('getRemovedIds')
|
||||
->with()
|
||||
->will($this->returnValue(array(ControllerTestService::class => true)))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', array(ControllerTestService::class, 'action'));
|
||||
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests where the fallback instantiation fails due to non-existing class.
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Controller "app.my_controller" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
|
||||
*/
|
||||
public function testExceptionWhenUsingRemovedControllerService()
|
||||
{
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('app.my_controller')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$container->expects($this->atLeastOnce())
|
||||
->method('getRemovedIds')
|
||||
->with()
|
||||
->will($this->returnValue(array('app.my_controller' => true)))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'app.my_controller');
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function getUndefinedControllers()
|
||||
{
|
||||
$tests = parent::getUndefinedControllers();
|
||||
$tests[0] = array('foo', \InvalidArgumentException::class, 'Controller "foo" does neither exist as service nor as class');
|
||||
$tests[1] = array('oof::bar', \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class');
|
||||
$tests[2] = array(array('oof', 'bar'), \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class');
|
||||
$tests[] = array(
|
||||
array(ControllerTestService::class, 'action'),
|
||||
\InvalidArgumentException::class,
|
||||
'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
|
||||
);
|
||||
$tests[] = array(
|
||||
ControllerTestService::class.'::action',
|
||||
\InvalidArgumentException::class, 'Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
|
||||
);
|
||||
$tests[] = array(
|
||||
InvokableControllerService::class,
|
||||
\InvalidArgumentException::class,
|
||||
'Controller "Symfony\Component\HttpKernel\Tests\Controller\InvokableControllerService" has required constructor arguments and does not exist in the container. Did you forget to define such a service?',
|
||||
);
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
|
||||
{
|
||||
if (!$container) {
|
||||
$container = $this->createMockContainer();
|
||||
}
|
||||
|
||||
return new ContainerControllerResolver($container, $logger);
|
||||
}
|
||||
|
||||
protected function createMockContainer()
|
||||
{
|
||||
return $this->getMockBuilder(ContainerInterface::class)->getMock();
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableControllerService
|
||||
{
|
||||
public function __construct($bar) // mandatory argument to prevent automatic instantiation
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ControllerTestService
|
||||
{
|
||||
public function __construct($foo)
|
||||
{
|
||||
}
|
||||
|
||||
public function action()
|
||||
{
|
||||
}
|
||||
}
|
||||
259
vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php
vendored
Normal file
259
vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php
vendored
Normal file
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests\Controller;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
|
||||
|
||||
class ControllerResolverTest extends TestCase
|
||||
{
|
||||
public function testGetControllerWithoutControllerParameter()
|
||||
{
|
||||
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
|
||||
$logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
|
||||
$resolver = $this->createControllerResolver($logger);
|
||||
|
||||
$request = Request::create('/');
|
||||
$this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
|
||||
}
|
||||
|
||||
public function testGetControllerWithLambda()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $lambda = function () {});
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame($lambda, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithObjectAndInvokeMethod()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$object = new InvokableController();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $object);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame($object, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithObjectAndMethod()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$object = new ControllerTest();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', array($object, 'publicAction'));
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame(array($object, 'publicAction'), $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithClassAndMethodAsArray()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', array(ControllerTest::class, 'publicAction'));
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf(ControllerTest::class, $controller[0]);
|
||||
$this->assertSame('publicAction', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerWithClassAndMethodAsString()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', ControllerTest::class.'::publicAction');
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf(ControllerTest::class, $controller[0]);
|
||||
$this->assertSame('publicAction', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerWithInvokableClass()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', InvokableController::class);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf(InvokableController::class, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testGetControllerOnObjectWithoutInvokeMethod()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', new \stdClass());
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function testGetControllerWithFunction()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function', $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithClosure()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$closure = function () {
|
||||
return 'test';
|
||||
};
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $closure);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf(\Closure::class, $controller);
|
||||
$this->assertSame('test', $controller());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getStaticControllers
|
||||
*/
|
||||
public function testGetControllerWithStaticController($staticController, $returnValue)
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $staticController);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame($staticController, $controller);
|
||||
$this->assertSame($returnValue, $controller());
|
||||
}
|
||||
|
||||
public function getStaticControllers()
|
||||
{
|
||||
return array(
|
||||
array(TestAbstractController::class.'::staticAction', 'foo'),
|
||||
array(array(TestAbstractController::class, 'staticAction'), 'foo'),
|
||||
array(PrivateConstructorController::class.'::staticAction', 'bar'),
|
||||
array(array(PrivateConstructorController::class, 'staticAction'), 'bar'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getUndefinedControllers
|
||||
*/
|
||||
public function testGetControllerWithUndefinedController($controller, $exceptionName = null, $exceptionMessage = null)
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
if (method_exists($this, 'expectException')) {
|
||||
$this->expectException($exceptionName);
|
||||
$this->expectExceptionMessage($exceptionMessage);
|
||||
} else {
|
||||
$this->setExpectedException($exceptionName, $exceptionMessage);
|
||||
}
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $controller);
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function getUndefinedControllers()
|
||||
{
|
||||
$controller = new ControllerTest();
|
||||
|
||||
return array(
|
||||
array('foo', \Error::class, 'Class \'foo\' not found'),
|
||||
array('oof::bar', \Error::class, 'Class \'oof\' not found'),
|
||||
array(array('oof', 'bar'), \Error::class, 'Class \'oof\' not found'),
|
||||
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::staticsAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'),
|
||||
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::privateAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
|
||||
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::protectedAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
|
||||
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::undefinedAction', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'),
|
||||
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerTest', \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Controller class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" cannot be called without a method name. You need to implement "__invoke" or use one of the available methods: "publicAction", "staticAction".'),
|
||||
array(array($controller, 'staticsAction'), \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'),
|
||||
array(array($controller, 'privateAction'), \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
|
||||
array(array($controller, 'protectedAction'), \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'),
|
||||
array(array($controller, 'undefinedAction'), \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'),
|
||||
array($controller, \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Controller class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" cannot be called without a method name. You need to implement "__invoke" or use one of the available methods: "publicAction", "staticAction".'),
|
||||
array(array('a' => 'foo', 'b' => 'bar'), \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Invalid array callable, expected array(controller, method).'),
|
||||
);
|
||||
}
|
||||
|
||||
protected function createControllerResolver(LoggerInterface $logger = null)
|
||||
{
|
||||
return new ControllerResolver($logger);
|
||||
}
|
||||
}
|
||||
|
||||
function some_controller_function($foo, $foobar)
|
||||
{
|
||||
}
|
||||
|
||||
class ControllerTest
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function publicAction()
|
||||
{
|
||||
}
|
||||
|
||||
private function privateAction()
|
||||
{
|
||||
}
|
||||
|
||||
protected function protectedAction()
|
||||
{
|
||||
}
|
||||
|
||||
public static function staticAction()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableController
|
||||
{
|
||||
public function __invoke($foo, $bar = null)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TestAbstractController
|
||||
{
|
||||
public static function staticAction()
|
||||
{
|
||||
return 'foo';
|
||||
}
|
||||
}
|
||||
|
||||
class PrivateConstructorController
|
||||
{
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function staticAction()
|
||||
{
|
||||
return 'bar';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user