Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigBundle] Fix error page preview for custom twig.exception_controller
- Loading branch information
Showing
6 changed files
with
158 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bundle/TwigBundle/Controller/PreviewErrorController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Controller; | ||
|
||
use Symfony\Component\HttpKernel\Exception\FlattenException; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* PreviewErrorController can be used to test error pages. | ||
* | ||
* It will create a test exception and forward it to another controller. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
class PreviewErrorController | ||
{ | ||
protected $kernel; | ||
protected $controller; | ||
|
||
public function __construct(HttpKernelInterface $kernel, $controller) | ||
{ | ||
$this->kernel = $kernel; | ||
$this->controller = $controller; | ||
} | ||
|
||
public function previewErrorPageAction(Request $request, $code) | ||
{ | ||
$exception = FlattenException::create(new \Exception("Something has intentionally gone wrong."), $code); | ||
|
||
/* | ||
* This Request mimics the parameters set by | ||
* \Symfony\Component\HttpKernel\EventListener\ExceptionListener::duplicateRequest, with | ||
* the additional "showException" flag. | ||
*/ | ||
|
||
$subRequest = $request->duplicate(null, null, array( | ||
'_controller' => $this->controller, | ||
'exception' => $exception, | ||
'logger' => null, | ||
'format' => $request->getRequestFormat(), | ||
'showException' => false, | ||
)); | ||
|
||
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bundle/TwigBundle/Tests/Controller/PreviewErrorControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Tests\Controller; | ||
|
||
use Symfony\Bundle\TwigBundle\Controller\PreviewErrorController; | ||
use Symfony\Bundle\TwigBundle\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class PreviewErrorControllerTest extends TestCase | ||
{ | ||
public function testForwardRequestToConfiguredController() | ||
{ | ||
$self = $this; | ||
|
||
$request = Request::create('whatever'); | ||
$response = new Response(""); | ||
$code = 123; | ||
$logicalControllerName = 'foo:bar:baz'; | ||
|
||
$kernel = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
$kernel | ||
->expects($this->once()) | ||
->method('handle') | ||
->with( | ||
$this->callback(function (Request $request) use ($self, $logicalControllerName, $code) { | ||
|
||
$self->assertEquals($logicalControllerName, $request->attributes->get('_controller')); | ||
|
||
$exception = $request->attributes->get('exception'); | ||
$self->assertInstanceOf('Symfony\Component\HttpKernel\Exception\FlattenException', $exception); | ||
$self->assertEquals($code, $exception->getStatusCode()); | ||
|
||
$self->assertFalse($request->attributes->get('showException')); | ||
|
||
return true; | ||
}), | ||
$this->equalTo(HttpKernelInterface::SUB_REQUEST) | ||
) | ||
->will($this->returnValue($response)); | ||
|
||
$controller = new PreviewErrorController($kernel, $logicalControllerName); | ||
|
||
$this->assertSame($response, $controller->previewErrorPageAction($request, $code)); | ||
} | ||
} |