Merge branch '4.3' into 4.4
* 4.3: [HttpKernel] fix merge fix tests [HttpClient] fix throwing HTTP exceptions when the 1st chunk is emitted
This commit is contained in:
commit
bbf2ae576e
@ -339,9 +339,20 @@ trait ResponseTrait
|
||||
} elseif ($chunk instanceof ErrorChunk) {
|
||||
unset($responses[$j]);
|
||||
$isTimeout = true;
|
||||
} elseif ($chunk instanceof FirstChunk && $response->logger) {
|
||||
$info = $response->getInfo();
|
||||
$response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url']));
|
||||
} elseif ($chunk instanceof FirstChunk) {
|
||||
if ($response->logger) {
|
||||
$info = $response->getInfo();
|
||||
$response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url']));
|
||||
}
|
||||
|
||||
yield $response => $chunk;
|
||||
|
||||
if ($response->initializer && null === $response->info['error']) {
|
||||
// Ensure the HTTP status code is always checked
|
||||
$response->getHeaders(true);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $response => $chunk;
|
||||
@ -349,10 +360,7 @@ trait ResponseTrait
|
||||
|
||||
unset($multi->handlesActivity[$j]);
|
||||
|
||||
if ($chunk instanceof FirstChunk && null === $response->initializer && null === $response->info['error']) {
|
||||
// Ensure the HTTP status code is always checked
|
||||
$response->getHeaders(true);
|
||||
} elseif ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
|
||||
if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
|
||||
// Ensure transport exceptions are always thrown
|
||||
$chunk->getContent();
|
||||
}
|
||||
|
@ -13,15 +13,16 @@ namespace Symfony\Component\HttpKernel\Tests\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Debug\ErrorHandler;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
|
||||
|
||||
class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
{
|
||||
public function testGetControllerService()
|
||||
public function testGetControllerServiceWithSingleColon()
|
||||
{
|
||||
$service = new ControllerTestService('foo');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
@ -30,22 +31,47 @@ class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->willReturn($this)
|
||||
->willReturn($service)
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'foo:controllerMethod1');
|
||||
$request->attributes->set('_controller', 'foo:action');
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertInstanceOf(\get_class($this), $controller[0]);
|
||||
$this->assertSame('controllerMethod1', $controller[1]);
|
||||
$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')
|
||||
->willReturn(true);
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->willReturn($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()
|
||||
{
|
||||
$invokableController = new InvokableController('bar');
|
||||
$service = new InvokableControllerService('bar');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
@ -56,7 +82,7 @@ class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('foo')
|
||||
->willReturn($invokableController)
|
||||
->willReturn($service)
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
@ -65,124 +91,94 @@ class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertEquals($invokableController, $controller);
|
||||
$this->assertSame($service, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerInvokableServiceWithClassNameAsName()
|
||||
{
|
||||
$invokableController = new InvokableController('bar');
|
||||
$className = __NAMESPACE__.'\InvokableController';
|
||||
$service = new InvokableControllerService('bar');
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with($className)
|
||||
->with(InvokableControllerService::class)
|
||||
->willReturn(true)
|
||||
;
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with($className)
|
||||
->willReturn($invokableController)
|
||||
->with(InvokableControllerService::class)
|
||||
->willReturn($service)
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $className);
|
||||
$request->attributes->set('_controller', InvokableControllerService::class);
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertEquals($invokableController, $controller);
|
||||
$this->assertSame($service, $controller);
|
||||
}
|
||||
|
||||
public function testNonInstantiableController()
|
||||
/**
|
||||
* @dataProvider getControllers
|
||||
*/
|
||||
public function testInstantiateControllerWhenControllerStartsWithABackslash($controller)
|
||||
{
|
||||
$service = new ControllerTestService('foo');
|
||||
$class = ControllerTestService::class;
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->once())->method('has')->with($class)->willReturn(true);
|
||||
$container->expects($this->once())->method('get')->with($class)->willReturn($service);
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $controller);
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertInstanceOf(ControllerTestService::class, $controller[0]);
|
||||
$this->assertSame('action', $controller[1]);
|
||||
}
|
||||
|
||||
public function getControllers()
|
||||
{
|
||||
return [
|
||||
['\\'.ControllerTestService::class.'::action'],
|
||||
['\\'.ControllerTestService::class.':action'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
|
||||
{
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('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"?');
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with(NonInstantiableController::class)
|
||||
->willReturn(false)
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame([NonInstantiableController::class, 'action'], $controller);
|
||||
}
|
||||
|
||||
public function testNonConstructController()
|
||||
{
|
||||
$this->expectException('LogicException');
|
||||
$this->expectExceptionMessage('Controller "Symfony\Component\HttpKernel\Tests\Controller\ImpossibleConstructController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->at(0))
|
||||
->method('has')
|
||||
->with(ImpossibleConstructController::class)
|
||||
->willReturn(true)
|
||||
;
|
||||
|
||||
$container->expects($this->at(1))
|
||||
->method('has')
|
||||
->with(ImpossibleConstructController::class)
|
||||
->with(ControllerTestService::class)
|
||||
->willReturn(false)
|
||||
;
|
||||
|
||||
$container->expects($this->atLeastOnce())
|
||||
->method('getRemovedIds')
|
||||
->with()
|
||||
->willReturn([ImpossibleConstructController::class => true])
|
||||
->willReturn([ControllerTestService::class => true])
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', [ImpossibleConstructController::class, 'action']);
|
||||
$request->attributes->set('_controller', [ControllerTestService::class, 'action']);
|
||||
|
||||
if (\PHP_VERSION_ID < 70100) {
|
||||
ErrorHandler::register();
|
||||
try {
|
||||
$resolver->getController($request);
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
restore_exception_handler();
|
||||
}
|
||||
} else {
|
||||
$resolver->getController($request);
|
||||
}
|
||||
}
|
||||
|
||||
public function testNonInstantiableControllerWithCorrespondingService()
|
||||
{
|
||||
$service = new \stdClass();
|
||||
|
||||
$container = $this->createMockContainer();
|
||||
$container->expects($this->atLeastOnce())
|
||||
->method('has')
|
||||
->with(NonInstantiableController::class)
|
||||
->willReturn(true)
|
||||
;
|
||||
$container->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with(NonInstantiableController::class)
|
||||
->willReturn($service)
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', [NonInstantiableController::class, 'action']);
|
||||
|
||||
$controller = $resolver->getController($request);
|
||||
|
||||
$this->assertSame([$service, 'action'], $controller);
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function testExceptionWhenUsingRemovedControllerService()
|
||||
{
|
||||
$this->expectException('LogicException');
|
||||
$this->expectException('InvalidArgumentException');
|
||||
$this->expectExceptionMessage('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"?');
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->at(0))
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('app.my_controller')
|
||||
->willReturn(false)
|
||||
@ -201,56 +197,28 @@ class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function testExceptionWhenUsingControllerWithoutAnInvokeMethod()
|
||||
{
|
||||
$this->expectException('LogicException');
|
||||
$this->expectExceptionMessage('Controller "app.my_controller" cannot be called without a method name. Did you forget an "__invoke" method?');
|
||||
$container = $this->getMockBuilder(Container::class)->getMock();
|
||||
$container->expects($this->once())
|
||||
->method('has')
|
||||
->with('app.my_controller')
|
||||
->willReturn(true)
|
||||
;
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->with('app.my_controller')
|
||||
->willReturn(new ImpossibleConstructController('toto', 'controller'))
|
||||
;
|
||||
|
||||
$resolver = $this->createControllerResolver(null, $container);
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'app.my_controller');
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getUndefinedControllers
|
||||
*/
|
||||
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
|
||||
{
|
||||
// All this logic needs to be duplicated, since calling parent::testGetControllerOnNonUndefinedFunction will override the expected excetion and not use the regex
|
||||
$resolver = $this->createControllerResolver();
|
||||
$this->expectException($exceptionName);
|
||||
$this->expectExceptionMessageRegExp($exceptionMessage);
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $controller);
|
||||
$resolver->getController($request);
|
||||
}
|
||||
|
||||
public function getUndefinedControllers()
|
||||
{
|
||||
return [
|
||||
['foo', \LogicException::class, '/Controller not found: service "foo" does not exist\./'],
|
||||
['oof::bar', \InvalidArgumentException::class, '/Class "oof" does not exist\./'],
|
||||
['stdClass', \LogicException::class, '/Controller not found: service "stdClass" does not exist\./'],
|
||||
[
|
||||
'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar',
|
||||
\InvalidArgumentException::class,
|
||||
'/.?[cC]ontroller(.*?) for URI "\/" is not callable\.( Expected method(.*) Available methods)?/',
|
||||
],
|
||||
$tests = parent::getUndefinedControllers();
|
||||
$tests[0] = ['foo', \InvalidArgumentException::class, 'Controller "foo" does neither exist as service nor as class'];
|
||||
$tests[1] = ['oof::bar', \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
|
||||
$tests[2] = [['oof', 'bar'], \InvalidArgumentException::class, 'Controller "oof" does neither exist as service nor as class'];
|
||||
$tests[] = [
|
||||
[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 the controller as a service?',
|
||||
];
|
||||
$tests[] = [
|
||||
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 the controller as a service?',
|
||||
];
|
||||
$tests[] = [
|
||||
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 the controller as a service?',
|
||||
];
|
||||
|
||||
return $tests;
|
||||
}
|
||||
|
||||
protected function createControllerResolver(LoggerInterface $logger = null, ContainerInterface $container = null)
|
||||
@ -268,7 +236,7 @@ class ContainerControllerResolverTest extends ControllerResolverTest
|
||||
}
|
||||
}
|
||||
|
||||
class InvokableController
|
||||
class InvokableControllerService
|
||||
{
|
||||
public function __construct($bar) // mandatory argument to prevent automatic instantiation
|
||||
{
|
||||
@ -279,16 +247,9 @@ class InvokableController
|
||||
}
|
||||
}
|
||||
|
||||
abstract class NonInstantiableController
|
||||
class ControllerTestService
|
||||
{
|
||||
public static function action()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ImpossibleConstructController
|
||||
{
|
||||
public function __construct($toto, $controller)
|
||||
public function __construct($foo)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -15,8 +15,6 @@ use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
|
||||
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
|
||||
|
||||
class ControllerResolverTest extends TestCase
|
||||
{
|
||||
@ -43,51 +41,55 @@ class ControllerResolverTest extends TestCase
|
||||
public function testGetControllerWithObjectAndInvokeMethod()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$object = new InvokableController();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', $this);
|
||||
$request->attributes->set('_controller', $object);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame($this, $controller);
|
||||
$this->assertSame($object, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithObjectAndMethod()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$object = new ControllerTest();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', [$this, 'controllerMethod1']);
|
||||
$request->attributes->set('_controller', [$object, 'publicAction']);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame([$this, 'controllerMethod1'], $controller);
|
||||
$this->assertSame([$object, 'publicAction'], $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerWithClassAndMethod()
|
||||
public function testGetControllerWithClassAndMethodAsArray()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', ['Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4']);
|
||||
$request->attributes->set('_controller', [ControllerTest::class, 'publicAction']);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertSame(['Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'], $controller);
|
||||
$this->assertInstanceOf(ControllerTest::class, $controller[0]);
|
||||
$this->assertSame('publicAction', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerWithObjectAndMethodAsString()
|
||||
public function testGetControllerWithClassAndMethodAsString()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod1');
|
||||
$request->attributes->set('_controller', ControllerTest::class.'::publicAction');
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
|
||||
$this->assertInstanceOf(ControllerTest::class, $controller[0]);
|
||||
$this->assertSame('publicAction', $controller[1]);
|
||||
}
|
||||
|
||||
public function testGetControllerWithClassAndInvokeMethod()
|
||||
public function testGetControllerWithInvokableClass()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest');
|
||||
$request->attributes->set('_controller', InvokableController::class);
|
||||
$controller = $resolver->getController($request);
|
||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller);
|
||||
$this->assertInstanceOf(InvokableController::class, $controller);
|
||||
}
|
||||
|
||||
public function testGetControllerOnObjectWithoutInvokeMethod()
|
||||
@ -110,10 +112,49 @@ class ControllerResolverTest extends TestCase
|
||||
$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 [
|
||||
[TestAbstractController::class.'::staticAction', 'foo'],
|
||||
[[TestAbstractController::class, 'staticAction'], 'foo'],
|
||||
[PrivateConstructorController::class.'::staticAction', 'bar'],
|
||||
[[PrivateConstructorController::class, 'staticAction'], 'bar'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getUndefinedControllers
|
||||
*/
|
||||
public function testGetControllerOnNonUndefinedFunction($controller, $exceptionName = null, $exceptionMessage = null)
|
||||
public function testGetControllerWithUndefinedController($controller, $exceptionName = null, $exceptionMessage = null)
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
$this->expectException($exceptionName);
|
||||
@ -126,179 +167,30 @@ class ControllerResolverTest extends TestCase
|
||||
|
||||
public function getUndefinedControllers()
|
||||
{
|
||||
$controller = new ControllerTest();
|
||||
|
||||
return [
|
||||
[1, 'InvalidArgumentException', 'Unable to find controller "1".'],
|
||||
['foo', 'InvalidArgumentException', 'Unable to find controller "foo".'],
|
||||
['oof::bar', 'InvalidArgumentException', 'Class "oof" does not exist.'],
|
||||
['stdClass', 'InvalidArgumentException', 'Unable to find controller "stdClass".'],
|
||||
['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::staticsAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Expected method "staticsAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest", did you mean "staticAction"?'],
|
||||
['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::privateAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Method "privateAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
|
||||
['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::protectedAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Method "protectedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest" should be public and non-abstract'],
|
||||
['Symfony\Component\HttpKernel\Tests\Controller\ControllerTest::undefinedAction', 'InvalidArgumentException', 'The controller for URI "/" is not callable. Expected method "undefinedAction" on class "Symfony\Component\HttpKernel\Tests\Controller\ControllerTest". Available methods: "publicAction", "staticAction"'],
|
||||
['foo', \Error::class, 'Class \'foo\' not found'],
|
||||
['oof::bar', \Error::class, 'Class \'oof\' not found'],
|
||||
[['oof', 'bar'], \Error::class, 'Class \'oof\' not found'],
|
||||
['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"?'],
|
||||
['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'],
|
||||
['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'],
|
||||
['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"'],
|
||||
['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".'],
|
||||
[[$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"?'],
|
||||
[[$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'],
|
||||
[[$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'],
|
||||
[[$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"'],
|
||||
[$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".'],
|
||||
[['a' => 'foo', 'b' => 'bar'], \InvalidArgumentException::class, 'The controller for URI "/" is not callable. Invalid array callable, expected [controller, method].'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testGetArguments()
|
||||
{
|
||||
$resolver = $this->createControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$controller = [new self(), 'testGetArguments'];
|
||||
$this->assertEquals([], $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = [new self(), 'controllerMethod1'];
|
||||
$this->assertEquals(['foo'], $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = [new self(), 'controllerMethod2'];
|
||||
$this->assertEquals(['foo', null], $resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
|
||||
|
||||
$request->attributes->set('bar', 'bar');
|
||||
$this->assertEquals(['foo', 'bar'], $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = function ($foo) {};
|
||||
$this->assertEquals(['foo'], $resolver->getArguments($request, $controller));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = function ($foo, $bar = 'bar') {};
|
||||
$this->assertEquals(['foo', 'bar'], $resolver->getArguments($request, $controller));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$controller = new self();
|
||||
$this->assertEquals(['foo', null], $resolver->getArguments($request, $controller));
|
||||
$request->attributes->set('bar', 'bar');
|
||||
$this->assertEquals(['foo', 'bar'], $resolver->getArguments($request, $controller));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('foobar', 'foobar');
|
||||
$controller = 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function';
|
||||
$this->assertEquals(['foo', 'foobar'], $resolver->getArguments($request, $controller));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('foobar', 'foobar');
|
||||
$controller = [new self(), 'controllerMethod3'];
|
||||
|
||||
try {
|
||||
$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');
|
||||
}
|
||||
|
||||
$request = Request::create('/');
|
||||
$controller = [new self(), 'controllerMethod5'];
|
||||
$this->assertEquals([$request], $resolver->getArguments($request, $controller), '->getArguments() injects the request');
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 5.6
|
||||
* @group legacy
|
||||
*/
|
||||
public function testGetVariadicArguments()
|
||||
{
|
||||
$resolver = new ControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', ['foo', 'bar']);
|
||||
$controller = [new VariadicController(), 'action'];
|
||||
$this->assertEquals(['foo', 'foo', 'bar'], $resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
public function testCreateControllerCanReturnAnyCallable()
|
||||
{
|
||||
$mock = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolver')->setMethods(['createController'])->getMock();
|
||||
$mock->expects($this->once())->method('createController')->willReturn('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_controller', 'foobar');
|
||||
$mock->getController($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testIfExceptionIsThrownWhenMissingAnArgument()
|
||||
{
|
||||
$this->expectException('RuntimeException');
|
||||
$resolver = new ControllerResolver();
|
||||
$request = Request::create('/');
|
||||
|
||||
$controller = [$this, 'controllerMethod1'];
|
||||
|
||||
$resolver->getArguments($request, $controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 7.1
|
||||
* @group legacy
|
||||
*/
|
||||
public function testGetNullableArguments()
|
||||
{
|
||||
$resolver = new ControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('foo', 'foo');
|
||||
$request->attributes->set('bar', new \stdClass());
|
||||
$request->attributes->set('mandatory', 'mandatory');
|
||||
$controller = [new NullableController(), 'action'];
|
||||
$this->assertEquals(['foo', new \stdClass(), 'value', 'mandatory'], $resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 7.1
|
||||
* @group legacy
|
||||
*/
|
||||
public function testGetNullableArgumentsWithDefaults()
|
||||
{
|
||||
$resolver = new ControllerResolver();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('mandatory', 'mandatory');
|
||||
$controller = [new NullableController(), 'action'];
|
||||
$this->assertEquals([null, null, 'value', 'mandatory'], $resolver->getArguments($request, $controller));
|
||||
}
|
||||
|
||||
protected function createControllerResolver(LoggerInterface $logger = null)
|
||||
{
|
||||
return new ControllerResolver($logger);
|
||||
}
|
||||
|
||||
public function __invoke($foo, $bar = null)
|
||||
{
|
||||
}
|
||||
|
||||
public function controllerMethod1($foo)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerMethod2($foo, $bar = null)
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerMethod3($foo, $bar, $foobar)
|
||||
{
|
||||
}
|
||||
|
||||
protected static function controllerMethod4()
|
||||
{
|
||||
}
|
||||
|
||||
protected function controllerMethod5(Request $request)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
function some_controller_function($foo, $foobar)
|
||||
@ -307,6 +199,15 @@ function some_controller_function($foo, $foobar)
|
||||
|
||||
class ControllerTest
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function publicAction()
|
||||
{
|
||||
}
|
||||
@ -323,3 +224,30 @@ class ControllerTest
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
|
||||
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
|
||||
@ -109,41 +110,20 @@ class LazyLoadingMetadataFactoryTest extends TestCase
|
||||
*/
|
||||
public function testWriteMetadataToLegacyCache()
|
||||
{
|
||||
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
|
||||
$cache = new Psr6Cache(new ArrayAdapter());
|
||||
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
|
||||
|
||||
$parentClassConstraints = [
|
||||
new ConstraintA(['groups' => ['Default', 'EntityParent']]),
|
||||
new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]),
|
||||
];
|
||||
$interfaceAConstraints = [
|
||||
new ConstraintA(['groups' => ['Default', 'EntityInterfaceA']]),
|
||||
];
|
||||
|
||||
$cache->expects($this->never())
|
||||
->method('has');
|
||||
$cache->expects($this->exactly(2))
|
||||
->method('read')
|
||||
->withConsecutive(
|
||||
[$this->equalTo(self::PARENT_CLASS)],
|
||||
[$this->equalTo(self::INTERFACE_A_CLASS)]
|
||||
)
|
||||
->willReturn(false);
|
||||
$cache->expects($this->exactly(2))
|
||||
->method('write')
|
||||
->withConsecutive(
|
||||
$this->callback(function ($metadata) use ($interfaceAConstraints) {
|
||||
return $interfaceAConstraints == $metadata->getConstraints();
|
||||
}),
|
||||
$this->callback(function ($metadata) use ($parentClassConstraints) {
|
||||
return $parentClassConstraints == $metadata->getConstraints();
|
||||
})
|
||||
);
|
||||
|
||||
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
|
||||
|
||||
$this->assertEquals(self::PARENT_CLASS, $metadata->getClassName());
|
||||
$this->assertEquals($parentClassConstraints, $metadata->getConstraints());
|
||||
$this->assertInstanceOf(ClassMetadata::class, $cache->read(self::PARENT_CLASS));
|
||||
$this->assertInstanceOf(ClassMetadata::class, $cache->read(self::INTERFACE_A_CLASS));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,6 +152,16 @@ abstract class HttpClientTestCase extends TestCase
|
||||
$this->assertSame(404, $response->getStatusCode());
|
||||
$this->assertSame(['application/json'], $response->getHeaders(false)['content-type']);
|
||||
$this->assertNotEmpty($response->getContent(false));
|
||||
|
||||
$response = $client->request('GET', 'http://localhost:8057/404');
|
||||
|
||||
try {
|
||||
foreach ($client->stream($response) as $chunk) {
|
||||
$this->assertTrue($chunk->isFirst());
|
||||
}
|
||||
$this->fail(ClientExceptionInterface::class.' expected');
|
||||
} catch (ClientExceptionInterface $e) {
|
||||
}
|
||||
}
|
||||
|
||||
public function testIgnoreErrors()
|
||||
|
Reference in New Issue
Block a user