Skip to content

Commit ff00d37

Browse files
committed
merged 2.0
2 parents 134e0e7 + a9777cd commit ff00d37

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

HttpKernel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ public function render($controller, array $options = array())
126126
// controller or URI?
127127
if (0 === strpos($controller, '/')) {
128128
$subRequest = Request::create($request->getUriForPath($controller), 'get', array(), $request->cookies->all(), array(), $request->server->all());
129-
$subRequest->setSession($request->getSession());
129+
if ($session = $request->getSession()) {
130+
$subRequest->setSession($session);
131+
}
130132
} else {
131133
$options['attributes']['_controller'] = $controller;
132134
$options['attributes']['_format'] = $request->getRequestFormat();
133135
$options['attributes']['_route'] = '_internal';
134136
$subRequest = $request->duplicate($options['query'], null, $options['attributes']);
135137
}
136138

139+
$level = ob_get_level();
137140
try {
138141
$response = $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
139142

@@ -155,6 +158,11 @@ public function render($controller, array $options = array())
155158
if (!$options['ignore_errors']) {
156159
throw $e;
157160
}
161+
162+
// let's clean up the output buffers that were created by the sub-request
163+
while (ob_get_level() > $level) {
164+
ob_get_clean();
165+
}
158166
}
159167
}
160168

Templating/Helper/CodeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function formatFileFromText($text)
200200
{
201201
$that = $this;
202202

203-
return preg_replace_callback('/in (")?(.*?)\1(?: +(?:on|at))? +line (\d+)/', function ($match) use ($that) {
203+
return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) {
204204
return 'in '.$that->formatFile($match[2], $match[3]);
205205
}, $text);
206206
}

Test/WebTestCase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ static protected function getKernelClass()
116116

117117
$finder = new Finder();
118118
$finder->name('*Kernel.php')->depth(0)->in($dir);
119-
if (!count($finder)) {
120-
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
119+
$results = iterator_to_array($finder);
120+
if (!count($results)) {
121+
throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
121122
}
122123

123-
$file = current(iterator_to_array($finder));
124+
$file = current($results);
124125
$class = $file->getBasename('.php');
125126

126127
require_once $file;

Tests/HttpKernelTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,53 @@ public function getProviderTypes()
166166
array(HttpKernelInterface::SUB_REQUEST),
167167
);
168168
}
169+
170+
public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
171+
{
172+
$request = new Request();
173+
174+
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
175+
$container
176+
->expects($this->at(0))
177+
->method('getParameter')
178+
->with($this->equalTo('kernel.debug'))
179+
->will($this->returnValue(false))
180+
;
181+
$container
182+
->expects($this->at(1))
183+
->method('has')
184+
->with($this->equalTo('esi'))
185+
->will($this->returnValue(false))
186+
;
187+
$container
188+
->expects($this->at(2))
189+
->method('get')
190+
->with($this->equalTo('request'))
191+
->will($this->returnValue($request))
192+
;
193+
194+
$dispatcher = new EventDispatcher();
195+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
196+
$resolver->expects($this->once())
197+
->method('getController')
198+
->will($this->returnValue(function () {
199+
ob_start();
200+
echo 'bar';
201+
throw new \RuntimeException();
202+
}));
203+
$resolver->expects($this->once())
204+
->method('getArguments')
205+
->will($this->returnValue(array()));
206+
207+
$kernel = new HttpKernel($dispatcher, $container, $resolver);
208+
209+
// simulate a main request with output buffering
210+
ob_start();
211+
echo 'Foo';
212+
213+
// simulate a sub-request with output buffering and an exception
214+
$kernel->render('/');
215+
216+
$this->assertEquals('Foo', ob_get_clean());
217+
}
169218
}

0 commit comments

Comments
 (0)