Skip to content

Commit 556bfcb

Browse files
committed
[HttpKernel] added some more unit tests
1 parent a19cdce commit 556bfcb

File tree

5 files changed

+344
-4
lines changed

5 files changed

+344
-4
lines changed

src/Symfony/Component/HttpKernel/BaseHttpKernel.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public function handle(Request $request = null, $type = HttpKernelInterface::MAS
8484
}
8585

8686
// exception
87-
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e)));
87+
$event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
88+
$this->dispatcher->notifyUntil($event);
8889
if ($event->isProcessed()) {
8990
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
9091
}
@@ -109,7 +110,8 @@ public function handle(Request $request = null, $type = HttpKernelInterface::MAS
109110
protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
110111
{
111112
// request
112-
$event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request)));
113+
$event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
114+
$this->dispatcher->notifyUntil($event);
113115
if ($event->isProcessed()) {
114116
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
115117
}
@@ -119,7 +121,8 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
119121
throw new NotFoundHttpException('Unable to find the controller.');
120122
}
121123

122-
$event = $this->dispatcher->filter(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request)), $controller);
124+
$event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
125+
$this->dispatcher->filter($event, $controller);
123126
$controller = $event->getReturnValue();
124127

125128
// controller must be a callable
@@ -134,7 +137,8 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
134137
$retval = call_user_func_array($controller, $arguments);
135138

136139
// view
137-
$event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval);
140+
$event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
141+
$this->dispatcher->filter($event, $retval);
138142

139143
return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type);
140144
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
}

tests/Symfony/Tests/Component/HttpKernel/Controller/ControllerResolverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function testGetController()
3232
$this->assertInstanceOf('Symfony\Tests\Component\HttpKernel\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
3333
$this->assertEquals(array('Using controller "Symfony\Tests\Component\HttpKernel\ControllerResolverTest::testGetController"'), $logger->getLogs('info'));
3434

35+
$request->attributes->set('_controller', $lambda = function () {});
36+
$controller = $resolver->getController($request);
37+
$this->assertSame($lambda, $controller);
38+
3539
$request->attributes->set('_controller', 'foo');
3640
try {
3741
$resolver->getController($request);
@@ -78,6 +82,11 @@ public function testGetArguments()
7882
$request->attributes->set('bar', 'bar');
7983
$this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
8084

85+
$request = Request::create('/');
86+
$request->attributes->set('foo', 'foo');
87+
$controller = function ($foo) {};
88+
$this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
89+
8190
$request = Request::create('/');
8291
$request->attributes->set('foo', 'foo');
8392
$request->attributes->set('foobar', 'foobar');
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\HttpKernel;
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 HttpKernelTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testHandleGetsTheRequestFromTheContainer()
23+
{
24+
$request = Request::create('/');
25+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
26+
$container->expects($this->any())
27+
->method('get')
28+
->will($this->returnValue($request))
29+
;
30+
31+
$kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver());
32+
33+
$kernel->handle();
34+
35+
$this->assertEquals($request, $kernel->getRequest());
36+
}
37+
38+
public function testHandleSetsTheRequestIfPassed()
39+
{
40+
$request = Request::create('/');
41+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
42+
$container->expects($this->exactly(2))
43+
->method('set')
44+
->with('request', $request)
45+
;
46+
47+
$kernel = new HttpKernel($container, new EventDispatcher(), $this->getResolver());
48+
49+
$kernel->handle($request);
50+
}
51+
52+
protected function getResolver($controller = null)
53+
{
54+
if (null === $controller) {
55+
$controller = function () { return new Response('Hello'); };
56+
}
57+
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
58+
$resolver->expects($this->any())
59+
->method('getController')
60+
->will($this->returnValue($controller))
61+
;
62+
$resolver->expects($this->any())
63+
->method('getArguments')
64+
->will($this->returnValue(array()))
65+
;
66+
67+
return $resolver;
68+
}
69+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\ResponseListener;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
16+
use Symfony\Component\EventDispatcher\Event;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
21+
class ResponseListenerTest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function testFilterDoesNothingForSubRequests()
24+
{
25+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
26+
$response = new Response('foo');
27+
28+
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
29+
}
30+
31+
public function testFilterDoesNothingIfContentTypeIsSet()
32+
{
33+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
34+
$response = new Response('foo');
35+
$response->headers->set('Content-Type', 'text/plain');
36+
37+
$this->assertEquals(array('content-type' => array('text/plain')), $this->getResponseListener()->filter($event, $response)->headers->all());
38+
}
39+
40+
public function testFilterDoesNothingIfRequestFormatIsNotDefined()
41+
{
42+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => Request::create('/')));
43+
$response = new Response('foo');
44+
45+
$this->assertEquals(array(), $this->getResponseListener()->filter($event, $response)->headers->all());
46+
}
47+
48+
public function testFilterSetContentType()
49+
{
50+
$request = Request::create('/');
51+
$request->setRequestFormat('json');
52+
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => $request));
53+
$response = new Response('foo');
54+
55+
$this->assertEquals(array('content-type' => array('application/json')), $this->getResponseListener()->filter($event, $response)->headers->all());
56+
}
57+
58+
protected function getResponseListener()
59+
{
60+
$dispatcher = new EventDispatcher();
61+
$listener = new ResponseListener();
62+
$listener->register($dispatcher);
63+
64+
return $listener;
65+
}
66+
}

0 commit comments

Comments
 (0)