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

Fix 'Call to a member function hasChildren() on boolean' #27

Merged
merged 1 commit into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/View/Console/DefaultRenderingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\View\Model\ConsoleModel as ConsoleViewModel;
use Zend\View\Model\ModelInterface;

class DefaultRenderingStrategy extends AbstractListenerAggregate
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function render(MvcEvent $e)
// marshal arguments
$response = $e->getResponse();

if (empty($result)) {
if (!$result instanceof ModelInterface) {
// There is absolutely no result, so there's nothing to display.
// We will return an empty response object
return $response;
Expand All @@ -51,6 +52,7 @@ public function render(MvcEvent $e)
// Collect results from child models
$responseText = '';
if ($result->hasChildren()) {
/* @var ModelInterface $child */
foreach ($result->getChildren() as $child) {
// Do not use ::getResult() method here as we cannot be sure if
// children are also console models.
Expand Down
45 changes: 38 additions & 7 deletions test/View/Console/DefaultRenderingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace ZendTest\Mvc\View\Console;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Console\Adapter\AbstractAdapter;
use Zend\EventManager\EventManager;
use Zend\Mvc\ApplicationInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\View\Console\DefaultRenderingStrategy;
use Zend\ServiceManager\ServiceManager;
Expand All @@ -36,6 +38,8 @@ public function testAttachesRendererAtExpectedPriority()
$expectedCallback = [$this->strategy, 'render'];
$expectedPriority = -10000;
$found = false;

/* @var \Zend\Stdlib\CallbackHandler $listener */
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
Expand All @@ -60,24 +64,22 @@ public function testCanDetachListenersFromEventManager()

public function testIgnoresNonConsoleModelNotContainingResultKeyWhenObtainingResult()
{
$console = $this->getMock('Zend\Console\Adapter\AbstractAdapter');
$console = $this->getMock(AbstractAdapter::class);
$console
->expects($this->any())
->method('encodeText')
->willReturnArgument(0)
;
->willReturnArgument(0);

//Register console service
$sm = new ServiceManager();
$sm->setService('console', $console);

/** @var \PHPUnit_Framework_MockObject_MockObject|\Zend\Mvc\ApplicationInterface $mockApplication */
$mockApplication = $this->getMock('Zend\Mvc\ApplicationInterface');
/* @var \PHPUnit_Framework_MockObject_MockObject|ApplicationInterface $mockApplication */
$mockApplication = $this->getMock(ApplicationInterface::class);
$mockApplication
->expects($this->any())
->method('getServiceManager')
->willReturn($sm)
;
->willReturn($sm);

$event = new MvcEvent();
$event->setApplication($mockApplication);
Expand All @@ -90,4 +92,33 @@ public function testIgnoresNonConsoleModelNotContainingResultKeyWhenObtainingRes
$content = $response->getContent();
$this->assertNotContains('Page not found', $content);
}

public function testIgnoresNonModel()
{
$console = $this->getMock(AbstractAdapter::class);
$console
->expects($this->any())
->method('encodeText')
->willReturnArgument(0);

//Register console service
$sm = new ServiceManager();
$sm->setService('console', $console);

/* @var \PHPUnit_Framework_MockObject_MockObject|ApplicationInterface $mockApplication */
$mockApplication = $this->getMock(ApplicationInterface::class);
$mockApplication
->expects($this->any())
->method('getServiceManager')
->willReturn($sm);

$event = new MvcEvent();
$event->setApplication($mockApplication);

$model = true;
$response = new Response();
$event->setResult($model);
$event->setResponse($response);
$this->assertSame($response, $this->strategy->render($event));
}
}