Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Better capabilities surrounding console notFoundAction
Browse files Browse the repository at this point in the history
- Return a console response for non-HTTP response types; resolves comment noted
  in #3186
  • Loading branch information
weierophinney committed Dec 19, 2012
1 parent 6a3b2a1 commit c7467b4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
40 changes: 35 additions & 5 deletions library/Zend/Mvc/Controller/AbstractActionController.php
Expand Up @@ -13,6 +13,7 @@
use Zend\Http\Response as HttpResponse;
use Zend\Mvc\Exception;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ConsoleModel;
use Zend\View\Model\ViewModel;

/**
Expand Down Expand Up @@ -51,15 +52,16 @@ public function notFoundAction()
$response = $this->response;
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('action', 'not-found');

if ($response instanceof HttpResponse) {
$response->setStatusCode(404);
$viewModel = $this->createHttpNotFoundModel($response);
}
if (!$response instanceof HttpResponse) {
$viewModel = $this->createConsoleNotFoundModel($response);
}
$routeMatch->setParam('action', 'not-found');

return new ViewModel(array(
'content' => 'Page not found'
));
return $viewModel;
}

/**
Expand Down Expand Up @@ -93,4 +95,32 @@ public function onDispatch(MvcEvent $e)

return $actionResponse;
}

/**
* Create an HTTP view model representing a "not found" page
*
* @param HttpResponse $response
* @return ViewModel
*/
protected function createHttpNotFoundModel(HttpResponse $response)
{
$response->setStatusCode(404);
return new ViewModel(array(
'content' => 'Page not found',
));
}

/**
* Create a console view model representing a "not found" action
*
* @param \Zend\Stdlib\ResponseInterface $response
* @return ConsoleModel
*/
protected function createConsoleNotFoundModel($response)
{
$viewModel = new ConsoleModel();
$viewModel->setErrorLevel(1);
$viewModel->setResult('Page not found');
return $viewModel;
}
}
8 changes: 6 additions & 2 deletions library/Zend/Mvc/View/Console/DefaultRenderingStrategy.php
Expand Up @@ -83,15 +83,19 @@ public function render(MvcEvent $e)
// Collect results from child models
$responseText = '';
if ($result->hasChildren()) {
/* @var $child ViewModel */
foreach ($result->getChildren() as $child) {
// Do not use ::getResult() method here as we cannot be sure if children are also console models.
$responseText .= $child->getVariable(ConsoleViewModel::RESULT);
}
}

// Fetch result from primary model
$responseText .= $result->getResult();
if ($result instanceof ConsoleViewModel) {
$responseText .= $result->getResult();
}
if (!$result instanceof ConsoleViewModel) {
$responseText .= $result->getVariable(ConsoleViewModel::RESULT);
}

// Append console response to response object
$response->setContent(
Expand Down
8 changes: 4 additions & 4 deletions tests/ZendTest/Mvc/Controller/ActionControllerTest.php
Expand Up @@ -208,10 +208,10 @@ public function testNotFoundActionReturnsSuccessfullyForConsoleResponse()
$result = $this->controller->dispatch($this->request, $response);
$testResponse = $this->controller->getResponse();
$this->assertSame($response, $testResponse);
$this->assertInstanceOf('Zend\View\Model\ModelInterface', $result);
$this->assertEquals('content', $result->captureTo());
$this->assertInstanceOf('Zend\View\Model\ConsoleModel', $result);
$vars = $result->getVariables();
$this->assertArrayHasKey('content', $vars, var_export($vars, 1));
$this->assertContains('Page not found', $vars['content']);
$this->assertTrue(isset($vars['result']));
$this->assertContains('Page not found', $vars['result']);
$this->assertEquals(1, $result->getErrorLevel());
}
}
77 changes: 77 additions & 0 deletions tests/ZendTest/Mvc/View/Console/DefaultRenderingStrategyTest.php
@@ -0,0 +1,77 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace ZendTest\Mvc\View\Console;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\Event;
use Zend\EventManager\EventManager;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\View\Console\DefaultRenderingStrategy;
use Zend\Stdlib\Response;
use Zend\View\Model;

/**
* @category Zend
* @package Zend_Mvc
* @subpackage UnitTest
*/
class DefaultRenderingStrategyTest extends TestCase
{
protected $strategy;

public function setUp()
{
$this->strategy = new DefaultRenderingStrategy();
}

public function testAttachesRendererAtExpectedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$listeners = $events->getListeners(MvcEvent::EVENT_RENDER);

$expectedCallback = array($this->strategy, 'render');
$expectedPriority = -10000;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Renderer not found');
}

public function testCanDetachListenersFromEventManager()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$this->assertEquals(1, count($events->getListeners(MvcEvent::EVENT_RENDER)));

$events->detachAggregate($this->strategy);
$this->assertEquals(0, count($events->getListeners(MvcEvent::EVENT_RENDER)));
}

public function testIgnoresNonConsoleModelNotContainingResultKeyWhenObtainingResult()
{
$event = new MvcEvent();
$model = new Model\ViewModel(array('content' => 'Page not found'));
$response = new Response();
$event->setResult($model);
$event->setResponse($response);
$this->strategy->render($event);
$content = $response->getContent();
$this->assertNotContains('Page not found', $content);
}
}

0 comments on commit c7467b4

Please sign in to comment.