Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Better capabilities surrounding console notFoundAction

- Return a console response for non-HTTP response types; resolves comment noted
  in zendframework/zf2#3186
  • Loading branch information...
commit c7467b4412b801a10dc1009d4317b534d390dfcb 1 parent 6a3b2a1
@weierophinney weierophinney authored
View
40 library/Zend/Mvc/Controller/AbstractActionController.php
@@ -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;
/**
@@ -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;
}
/**
@@ -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;
+ }
}
View
8 library/Zend/Mvc/View/Console/DefaultRenderingStrategy.php
@@ -83,7 +83,6 @@ 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);
@@ -91,7 +90,12 @@ public function render(MvcEvent $e)
}
// 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(
View
8 tests/ZendTest/Mvc/Controller/ActionControllerTest.php
@@ -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());
}
}
View
77 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);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.