Laravel version update
Laravel version update
This commit is contained in:
166
vendor/symfony/http-kernel/Tests/HttpKernelTest.php
vendored
166
vendor/symfony/http-kernel/Tests/HttpKernelTest.php
vendored
@@ -11,24 +11,31 @@
|
||||
|
||||
namespace Symfony\Component\HttpKernel\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
||||
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\HttpKernel\HttpKernel;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
|
||||
class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
class HttpKernelTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
|
||||
{
|
||||
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
||||
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
|
||||
|
||||
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
|
||||
}
|
||||
@@ -38,7 +45,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
|
||||
{
|
||||
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
|
||||
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
|
||||
|
||||
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
|
||||
}
|
||||
@@ -50,7 +57,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$event->setResponse(new Response($event->getException()->getMessage()));
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException('foo'); });
|
||||
$response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
|
||||
|
||||
$this->assertEquals('500', $response->getStatusCode());
|
||||
@@ -66,7 +73,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
// should set a response, but does not
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($exception) { throw $exception; }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () use ($exception) { throw $exception; });
|
||||
|
||||
try {
|
||||
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
|
||||
@@ -83,7 +90,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$event->setResponse(new RedirectResponse('/login', 301));
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new AccessDeniedHttpException(); }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { throw new AccessDeniedHttpException(); });
|
||||
$response = $kernel->handle(new Request());
|
||||
|
||||
$this->assertEquals('301', $response->getStatusCode());
|
||||
@@ -97,7 +104,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$event->setResponse(new Response($event->getException()->getMessage()));
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new MethodNotAllowedHttpException(array('POST')); }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { throw new MethodNotAllowedHttpException(array('POST')); });
|
||||
$response = $kernel->handle(new Request());
|
||||
|
||||
$this->assertEquals('405', $response->getStatusCode());
|
||||
@@ -105,16 +112,17 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @dataProvider getStatusCodes
|
||||
*/
|
||||
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
|
||||
public function testLegacyHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
|
||||
$event->setResponse(new Response('', $responseStatusCode, array('X-Status-Code' => $expectedStatusCode)));
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException(); }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
|
||||
$response = $kernel->handle(new Request());
|
||||
|
||||
$this->assertEquals($expectedStatusCode, $response->getStatusCode());
|
||||
@@ -131,6 +139,32 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getSpecificStatusCodes
|
||||
*/
|
||||
public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) use ($expectedStatusCode) {
|
||||
$event->allowCustomResponseCode();
|
||||
$event->setResponse(new Response('', $expectedStatusCode));
|
||||
});
|
||||
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
|
||||
$response = $kernel->handle(new Request());
|
||||
|
||||
$this->assertEquals($expectedStatusCode, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function getSpecificStatusCodes()
|
||||
{
|
||||
return array(
|
||||
array(200),
|
||||
array(302),
|
||||
array(403),
|
||||
);
|
||||
}
|
||||
|
||||
public function testHandleWhenAListenerReturnsAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
@@ -138,7 +172,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$event->setResponse(new Response('hello'));
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
||||
$kernel = $this->getHttpKernel($dispatcher);
|
||||
|
||||
$this->assertEquals('hello', $kernel->handle(new Request())->getContent());
|
||||
}
|
||||
@@ -149,7 +183,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenNoControllerIsFound()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(false));
|
||||
$kernel = $this->getHttpKernel($dispatcher, false);
|
||||
|
||||
$kernel->handle(new Request());
|
||||
}
|
||||
@@ -158,7 +192,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$response = new Response('foo');
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($response) { return $response; }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () use ($response) { return $response; });
|
||||
|
||||
$this->assertSame($response, $kernel->handle(new Request()));
|
||||
}
|
||||
@@ -166,7 +200,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenTheControllerIsAnObjectWithInvoke()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(new Controller()));
|
||||
$kernel = $this->getHttpKernel($dispatcher, new Controller());
|
||||
|
||||
$this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
|
||||
}
|
||||
@@ -174,7 +208,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenTheControllerIsAFunction()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver('Symfony\Component\HttpKernel\Tests\controller_func'));
|
||||
$kernel = $this->getHttpKernel($dispatcher, 'Symfony\Component\HttpKernel\Tests\controller_func');
|
||||
|
||||
$this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
|
||||
}
|
||||
@@ -182,7 +216,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenTheControllerIsAnArray()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(array(new Controller(), 'controller')));
|
||||
$kernel = $this->getHttpKernel($dispatcher, array(new Controller(), 'controller'));
|
||||
|
||||
$this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
|
||||
}
|
||||
@@ -190,7 +224,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenTheControllerIsAStaticArray()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller')));
|
||||
$kernel = $this->getHttpKernel($dispatcher, array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller'));
|
||||
|
||||
$this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
|
||||
}
|
||||
@@ -201,7 +235,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
public function testHandleWhenTheControllerDoesNotReturnAResponse()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
|
||||
|
||||
$kernel->handle(new Request());
|
||||
}
|
||||
@@ -212,7 +246,8 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$dispatcher->addListener(KernelEvents::VIEW, function ($event) {
|
||||
$event->setResponse(new Response($event->getControllerResult()));
|
||||
});
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
|
||||
|
||||
$kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
|
||||
|
||||
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
|
||||
}
|
||||
@@ -223,15 +258,51 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
$dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
|
||||
$event->setResponse(new Response('foo'));
|
||||
});
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
||||
$kernel = $this->getHttpKernel($dispatcher);
|
||||
|
||||
$this->assertEquals('foo', $kernel->handle(new Request())->getContent());
|
||||
}
|
||||
|
||||
public function testHandleAllowChangingControllerArguments()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
|
||||
$event->setArguments(array('foo'));
|
||||
});
|
||||
|
||||
$kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); });
|
||||
|
||||
$this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
|
||||
}
|
||||
|
||||
public function testHandleAllowChangingControllerAndArguments()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
|
||||
$oldController = $event->getController();
|
||||
$oldArguments = $event->getArguments();
|
||||
|
||||
$newController = function ($id) use ($oldController, $oldArguments) {
|
||||
$response = \call_user_func_array($oldController, $oldArguments);
|
||||
|
||||
$response->headers->set('X-Id', $id);
|
||||
|
||||
return $response;
|
||||
};
|
||||
|
||||
$event->setController($newController);
|
||||
$event->setArguments(array('bar'));
|
||||
});
|
||||
|
||||
$kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); }, null, array('foo'));
|
||||
|
||||
$this->assertResponseEquals(new Response('foo', 200, array('X-Id' => 'bar')), $kernel->handle(new Request()));
|
||||
}
|
||||
|
||||
public function testTerminate()
|
||||
{
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
||||
$kernel = $this->getHttpKernel($dispatcher);
|
||||
$dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
|
||||
$called = true;
|
||||
$capturedKernel = $event->getKernel();
|
||||
@@ -250,55 +321,60 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$request = new Request();
|
||||
|
||||
$stack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack', array('push', 'pop'));
|
||||
$stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
|
||||
$stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
|
||||
$stack->expects($this->at(1))->method('pop');
|
||||
|
||||
$dispatcher = new EventDispatcher();
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver(), $stack);
|
||||
$kernel = $this->getHttpKernel($dispatcher, null, $stack);
|
||||
|
||||
$kernel->handle($request, HttpKernelInterface::MASTER_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||
*/
|
||||
public function testInconsistentClientIpsOnMasterRequests()
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setTrustedProxies(array('1.1.1.1'), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_FORWARDED);
|
||||
$request->server->set('REMOTE_ADDR', '1.1.1.1');
|
||||
$request->headers->set('FORWARDED', 'for=2.2.2.2');
|
||||
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
|
||||
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
|
||||
$event->getRequest()->getClientIp();
|
||||
});
|
||||
|
||||
$kernel = new HttpKernel($dispatcher, $this->getResolver());
|
||||
|
||||
$request = new Request();
|
||||
$request->setTrustedProxies(array('1.1.1.1'));
|
||||
$request->server->set('REMOTE_ADDR', '1.1.1.1');
|
||||
$request->headers->set('FORWARDED', '2.2.2.2');
|
||||
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
|
||||
|
||||
$kernel = $this->getHttpKernel($dispatcher);
|
||||
$kernel->handle($request, $kernel::MASTER_REQUEST, false);
|
||||
|
||||
Request::setTrustedProxies(array(), -1);
|
||||
}
|
||||
|
||||
protected function getResolver($controller = null)
|
||||
private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = array())
|
||||
{
|
||||
if (null === $controller) {
|
||||
$controller = function () { return new Response('Hello'); };
|
||||
}
|
||||
|
||||
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
|
||||
$resolver->expects($this->any())
|
||||
$controllerResolver = $this->getMockBuilder(ControllerResolverInterface::class)->getMock();
|
||||
$controllerResolver
|
||||
->expects($this->any())
|
||||
->method('getController')
|
||||
->will($this->returnValue($controller));
|
||||
$resolver->expects($this->any())
|
||||
->method('getArguments')
|
||||
->will($this->returnValue(array()));
|
||||
|
||||
return $resolver;
|
||||
$argumentResolver = $this->getMockBuilder(ArgumentResolverInterface::class)->getMock();
|
||||
$argumentResolver
|
||||
->expects($this->any())
|
||||
->method('getArguments')
|
||||
->will($this->returnValue($arguments));
|
||||
|
||||
return new HttpKernel($eventDispatcher, $controllerResolver, $requestStack, $argumentResolver);
|
||||
}
|
||||
|
||||
protected function assertResponseEquals(Response $expected, Response $actual)
|
||||
private function assertResponseEquals(Response $expected, Response $actual)
|
||||
{
|
||||
$expected->setDate($actual->getDate());
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
Reference in New Issue
Block a user