|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Tests\Component\HttpKernel; |
| 13 | + |
| 14 | +use Symfony\Component\HttpKernel\BaseHttpKernel; |
| 15 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 19 | + |
| 20 | +class BaseHttpKernelTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + public function testHandleChangingMasterRequest() |
| 23 | + { |
| 24 | + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver()); |
| 25 | + |
| 26 | + $kernel->handle(); |
| 27 | + $this->assertInstanceof('Symfony\Component\HttpFoundation\Request', $kernel->getRequest()); |
| 28 | + |
| 29 | + $request = Request::create('/'); |
| 30 | + $kernel->handle($request); |
| 31 | + $this->assertSame($request, $kernel->getRequest()); |
| 32 | + |
| 33 | + $subRequest = Request::create('/'); |
| 34 | + $kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); |
| 35 | + $this->assertSame($request, $kernel->getRequest()); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @expectedException RuntimeException |
| 40 | + */ |
| 41 | + public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue() |
| 42 | + { |
| 43 | + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); |
| 44 | + |
| 45 | + $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, true); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @expectedException RuntimeException |
| 50 | + */ |
| 51 | + public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalseAndNoListenerIsRegistered() |
| 52 | + { |
| 53 | + $kernel = new BaseHttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); })); |
| 54 | + |
| 55 | + $kernel->handle(null, HttpKernelInterface::MASTER_REQUEST, false); |
| 56 | + } |
| 57 | + |
| 58 | + public function testHandleWhenControllerThrowsAnExceptionAndRawIsFalse() |
| 59 | + { |
| 60 | + $dispatcher = new EventDispatcher(); |
| 61 | + $dispatcher->connect('core.exception', function ($event) |
| 62 | + { |
| 63 | + $event->setReturnValue(new Response($event->getParameter('exception')->getMessage())); |
| 64 | + |
| 65 | + return true; |
| 66 | + }); |
| 67 | + |
| 68 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); })); |
| 69 | + |
| 70 | + $this->assertEquals('foo', $kernel->handle()->getContent()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testHandleWhenAListenerReturnsAResponse() |
| 74 | + { |
| 75 | + $dispatcher = new EventDispatcher(); |
| 76 | + $dispatcher->connect('core.request', function ($event) |
| 77 | + { |
| 78 | + $event->setReturnValue(new Response('hello')); |
| 79 | + |
| 80 | + return true; |
| 81 | + }); |
| 82 | + |
| 83 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); |
| 84 | + |
| 85 | + $this->assertEquals('hello', $kernel->handle()->getContent()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
| 90 | + */ |
| 91 | + public function testHandleWhenNoControllerIsFound() |
| 92 | + { |
| 93 | + $dispatcher = new EventDispatcher(); |
| 94 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(false)); |
| 95 | + |
| 96 | + $kernel->handle(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @expectedException LogicException |
| 101 | + */ |
| 102 | + public function testHandleWhenNoControllerIsNotACallable() |
| 103 | + { |
| 104 | + $dispatcher = new EventDispatcher(); |
| 105 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver('foobar')); |
| 106 | + |
| 107 | + $kernel->handle(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @expectedException RuntimeException |
| 112 | + */ |
| 113 | + public function testHandleWhenControllerDoesNotReturnAResponse() |
| 114 | + { |
| 115 | + $dispatcher = new EventDispatcher(); |
| 116 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); |
| 117 | + |
| 118 | + $kernel->handle(); |
| 119 | + } |
| 120 | + |
| 121 | + public function testHandleWhenControllerDoesNotReturnAResponseButAViewIsRegistered() |
| 122 | + { |
| 123 | + $dispatcher = new EventDispatcher(); |
| 124 | + $dispatcher->connect('core.view', function ($event, $retval) |
| 125 | + { |
| 126 | + return new Response($retval); |
| 127 | + }); |
| 128 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); |
| 129 | + |
| 130 | + $this->assertEquals('foo', $kernel->handle()->getContent()); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @expectedException RuntimeException |
| 135 | + */ |
| 136 | + public function testHandleWhenAViewDoesNotReturnAResponse() |
| 137 | + { |
| 138 | + $dispatcher = new EventDispatcher(); |
| 139 | + $dispatcher->connect('core.view', function ($event, $retval) |
| 140 | + { |
| 141 | + return $retval; |
| 142 | + }); |
| 143 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; })); |
| 144 | + |
| 145 | + $kernel->handle(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @expectedException RuntimeException |
| 150 | + */ |
| 151 | + public function testHandleWhenAResponseListenerDoesNotReturnAResponse() |
| 152 | + { |
| 153 | + $dispatcher = new EventDispatcher(); |
| 154 | + $dispatcher->connect('core.response', function ($event, $response) |
| 155 | + { |
| 156 | + return 'foo'; |
| 157 | + }); |
| 158 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); |
| 159 | + |
| 160 | + $kernel->handle(); |
| 161 | + } |
| 162 | + |
| 163 | + public function testHandleWithAResponseListener() |
| 164 | + { |
| 165 | + $dispatcher = new EventDispatcher(); |
| 166 | + $dispatcher->connect('core.response', function ($event, $response) |
| 167 | + { |
| 168 | + return new Response('foo'); |
| 169 | + }); |
| 170 | + $kernel = new BaseHttpKernel($dispatcher, $this->getResolver()); |
| 171 | + |
| 172 | + $this->assertEquals('foo', $kernel->handle()->getContent()); |
| 173 | + } |
| 174 | + |
| 175 | + protected function getResolver($controller = null) |
| 176 | + { |
| 177 | + if (null === $controller) { |
| 178 | + $controller = function () { return new Response('Hello'); }; |
| 179 | + } |
| 180 | + $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); |
| 181 | + $resolver->expects($this->any()) |
| 182 | + ->method('getController') |
| 183 | + ->will($this->returnValue($controller)) |
| 184 | + ; |
| 185 | + $resolver->expects($this->any()) |
| 186 | + ->method('getArguments') |
| 187 | + ->will($this->returnValue(array())) |
| 188 | + ; |
| 189 | + |
| 190 | + return $resolver; |
| 191 | + } |
| 192 | +} |
0 commit comments