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

Commit

Permalink
Merge branch 'hotfix/3254' into develop
Browse files Browse the repository at this point in the history
Forward port zendframework#3254
  • Loading branch information
akrabat committed Dec 19, 2012
2 parents 2561b5f + 5a90310 commit 3d41fed
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
38 changes: 32 additions & 6 deletions library/Zend/Mvc/Controller/AbstractActionController.php
Original file line number Diff line number Diff line change
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,12 @@ public function notFoundAction()
$response = $this->response;
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('action', 'not-found');

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

return new ViewModel(array(
'content' => 'Page not found'
));
return $this->createConsoleNotFoundModel($response);
}

/**
Expand Down Expand Up @@ -93,4 +91,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;
}
}
6 changes: 5 additions & 1 deletion library/Zend/Mvc/View/Console/DefaultRenderingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public function render(MvcEvent $e)
}

// Fetch result from primary model
$responseText .= $result->getResult();
if ($result instanceof ConsoleViewModel) {
$responseText .= $result->getResult();
} else {
$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 3d41fed

Please sign in to comment.