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

Commit

Permalink
Updated zend-view to eventmanager v3
Browse files Browse the repository at this point in the history
- Uses dev-develop until we tag EM3.
- Updates trigger and attach calls to follow v3.
- Updates tests:
  - strategy tests primarily needed tooling for doing assertions on listeners
    and priorities after attaching aggregates (and updating aggregates to attach
    using the new signature).
  - Navigation and UrlIntegration tests were marked as incomplete as they depend
    on zend-mvc for testing.
  • Loading branch information
weierophinney committed Sep 30, 2015
1 parent b44b831 commit 73b70d8
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 101 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-eventmanager": "~2.5",
"zendframework/zend-eventmanager": "dev-develop as 2.7.0",
"zendframework/zend-loader": "~2.5",
"zendframework/zend-stdlib": "~2.5"
},
Expand All @@ -30,7 +30,7 @@
"zendframework/zend-i18n": "~2.5",
"zendframework/zend-json": "~2.5",
"zendframework/zend-log": "~2.5",
"zendframework/zend-modulemanager": "~2.5",
"zendframework/zend-modulemanager": "dev-develop as 2.7.0",
"zendframework/zend-mvc": "~2.5",
"zendframework/zend-navigation": "~2.5",
"zendframework/zend-paginator": "~2.5",
Expand Down Expand Up @@ -62,7 +62,7 @@
"extra": {
"branch-alias": {
"dev-master": "2.5-dev",
"dev-develop": "2.6-dev"
"dev-develop": "3.0-dev"
}
},
"autoload-dev": {
Expand Down
11 changes: 7 additions & 4 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ public function render(Model $model)
{
$event = $this->getEvent();
$event->setModel($model);
$event->setName(ViewEvent::EVENT_RENDERER);
$events = $this->getEventManager();
$results = $events->trigger(ViewEvent::EVENT_RENDERER, $event, function ($result) {
$results = $events->triggerEventUntil(function ($result) {
return ($result instanceof Renderer);
});
}, $event);
$renderer = $results->last();
if (!$renderer instanceof Renderer) {
throw new Exception\RuntimeException(sprintf(
Expand All @@ -182,7 +183,8 @@ public function render(Model $model)
}

$event->setRenderer($renderer);
$events->trigger(ViewEvent::EVENT_RENDERER_POST, $event);
$event->setName(ViewEvent::EVENT_RENDERER_POST);
$events->triggerEvent($event);

// If EVENT_RENDERER or EVENT_RENDERER_POST changed the model, make sure
// we use this new model instead of the current $model
Expand Down Expand Up @@ -212,8 +214,9 @@ public function render(Model $model)
}

$event->setResult($rendered);
$event->setName(ViewEvent::EVENT_RESPONSE);

$events->trigger(ViewEvent::EVENT_RESPONSE, $event);
$events->triggerEvent($event);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions test/EventManagerIntrospectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\View;

use ReflectionProperty;
use Zend\EventManager\EventManager;

/**
* Offer methods for introspecting event manager events and listeners.
*/
trait EventManagerIntrospectionTrait
{
public function getEventsFromEventManager(EventManager $events)
{
$r = new ReflectionProperty($events, 'events');
$r->setAccessible(true);
$listeners = $r->getValue($events);
return array_keys($listeners);
}

public function getListenersForEvent($event, EventManager $events, $withPriority = false)
{
$r = new ReflectionProperty($events, 'events');
$r->setAccessible(true);
$listeners = $r->getValue($events);

if (! isset($listeners[$event])) {
return $this->traverseListeners([]);
}

return $this->traverseListeners($listeners[$event], $withPriority);
}

public function traverseListeners(array $queue, $withPriority = false)
{
krsort($queue, SORT_NUMERIC);

foreach ($queue as $priority => $listeners) {
$priority = (int) $priority;
foreach ($listeners as $listener) {
if ($withPriority) {
yield $priority => $listener;
} else {
yield $listener;
}
}
}
}
}
11 changes: 7 additions & 4 deletions test/Helper/Navigation/AbstractHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class AbstractHelperTest extends AbstractTest
protected function tearDown()
{
parent::tearDown();
$this->_helper->setDefaultAcl(null);
$this->_helper->setAcl(null);
$this->_helper->setDefaultRole(null);
$this->_helper->setRole(null);

if ($this->_helper) {
$this->_helper->setDefaultAcl(null);
$this->_helper->setAcl(null);
$this->_helper->setDefaultRole(null);
$this->_helper->setRole(null);
}
}

public function testHasACLChecksDefaultACL()
Expand Down
2 changes: 2 additions & 0 deletions test/Helper/Navigation/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ abstract class AbstractTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->markTestIncomplete('Cannot test until zend-mvc v3 eventmanager integration is complete.');

$cwd = __DIR__;

// read navigation config
Expand Down
2 changes: 2 additions & 0 deletions test/Helper/UrlIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class UrlIntegrationTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->markTestIncomplete('Cannot test until zend-mvc v3 eventmanager integration is complete.');

$config = [
'router' => [
'routes' => [
Expand Down
63 changes: 33 additions & 30 deletions test/Strategy/FeedStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
use Zend\View\Renderer\FeedRenderer;
use Zend\View\Strategy\FeedStrategy;
use Zend\View\ViewEvent;
use ZendTest\View\EventManagerIntrospectionTrait;

class FeedStrategyTest extends TestCase
{
use EventManagerIntrospectionTrait;

public function setUp()
{
$this->renderer = new FeedRenderer;
Expand Down Expand Up @@ -213,20 +216,19 @@ public function testReturnsNullWhenUnableToSelectRenderer()
public function testAttachesListenersAtExpectedPriorities()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$this->strategy->attach($events);

foreach (['renderer' => 'selectRenderer', 'response' => 'injectResponse'] as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = [$this->strategy, $method];
$listeners = $this->getListenersForEvent($event, $events, true);
$expectedListener = [$this->strategy, $method];
$expectedPriority = 1;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
foreach ($listeners as $priority => $listener) {
if ($listener === $expectedListener
&& $priority === $expectedPriority
) {
$found = true;
break;
}
}
$this->assertTrue($found, 'Listener not found');
Expand All @@ -236,20 +238,19 @@ public function testAttachesListenersAtExpectedPriorities()
public function testCanAttachListenersAtSpecifiedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy, 100);
$this->strategy->attach($events, 100);

foreach (['renderer' => 'selectRenderer', 'response' => 'injectResponse'] as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = [$this->strategy, $method];
$listeners = $this->getListenersForEvent($event, $events, true);
$expectedListener = [$this->strategy, $method];
$expectedPriority = 100;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
foreach ($listeners as $priority => $listener) {
if ($listener === $expectedListener
&& $priority === $expectedPriority
) {
$found = true;
break;
}
}
$this->assertTrue($found, 'Listener not found');
Expand All @@ -259,15 +260,17 @@ public function testCanAttachListenersAtSpecifiedPriority()
public function testDetachesListeners()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$listeners = $events->getListeners('renderer');
$this->assertEquals(1, count($listeners));
$listeners = $events->getListeners('response');
$this->assertEquals(1, count($listeners));
$events->detachAggregate($this->strategy);
$listeners = $events->getListeners('renderer');
$this->assertEquals(0, count($listeners));
$listeners = $events->getListeners('response');
$this->assertEquals(0, count($listeners));
$this->strategy->attach($events, 100);

$listeners = iterator_to_array($this->getListenersForEvent('renderer', $events));
$this->assertCount(1, $listeners);
$listeners = iterator_to_array($this->getListenersForEvent('response', $events));
$this->assertCount(1, $listeners);

$this->strategy->detach($events, 100);
$listeners = iterator_to_array($this->getListenersForEvent('renderer', $events));
$this->assertCount(0, $listeners);
$listeners = iterator_to_array($this->getListenersForEvent('response', $events));
$this->assertCount(0, $listeners);
}
}
63 changes: 33 additions & 30 deletions test/Strategy/JsonStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
use Zend\View\Strategy\JsonStrategy;
use Zend\View\ViewEvent;
use Zend\Stdlib\Parameters;
use ZendTest\View\EventManagerIntrospectionTrait;

class JsonStrategyTest extends TestCase
{
use EventManagerIntrospectionTrait;

public function setUp()
{
$this->renderer = new JsonRenderer;
Expand Down Expand Up @@ -160,20 +163,19 @@ public function testReturnsNullWhenCannotSelectRenderer()
public function testAttachesListenersAtExpectedPriorities()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$this->strategy->attach($events);

foreach (['renderer' => 'selectRenderer', 'response' => 'injectResponse'] as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = [$this->strategy, $method];
$listeners = $this->getListenersForEvent($event, $events, true);
$expectedListener = [$this->strategy, $method];
$expectedPriority = 1;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
foreach ($listeners as $priority => $listener) {
if ($listener === $expectedListener
&& $priority === $expectedPriority
) {
$found = true;
break;
}
}
$this->assertTrue($found, 'Listener not found');
Expand All @@ -183,20 +185,19 @@ public function testAttachesListenersAtExpectedPriorities()
public function testCanAttachListenersAtSpecifiedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy, 1000);
$this->strategy->attach($events, 1000);

foreach (['renderer' => 'selectRenderer', 'response' => 'injectResponse'] as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = [$this->strategy, $method];
$listeners = $this->getListenersForEvent($event, $events, true);
$expectedListener = [$this->strategy, $method];
$expectedPriority = 1000;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
foreach ($listeners as $priority => $listener) {
if ($listener === $expectedListener
&& $priority === $expectedPriority
) {
$found = true;
break;
}
}
$this->assertTrue($found, 'Listener not found');
Expand All @@ -206,16 +207,18 @@ public function testCanAttachListenersAtSpecifiedPriority()
public function testDetachesListeners()
{
$events = new EventManager();
$events->attachAggregate($this->strategy);
$listeners = $events->getListeners('renderer');
$this->assertEquals(1, count($listeners));
$listeners = $events->getListeners('response');
$this->assertEquals(1, count($listeners));
$events->detachAggregate($this->strategy);
$listeners = $events->getListeners('renderer');
$this->assertEquals(0, count($listeners));
$listeners = $events->getListeners('response');
$this->assertEquals(0, count($listeners));
$this->strategy->attach($events, 100);

$listeners = iterator_to_array($this->getListenersForEvent('renderer', $events));
$this->assertCount(1, $listeners);
$listeners = iterator_to_array($this->getListenersForEvent('response', $events));
$this->assertCount(1, $listeners);

$this->strategy->detach($events, 100);
$listeners = iterator_to_array($this->getListenersForEvent('renderer', $events));
$this->assertCount(0, $listeners);
$listeners = iterator_to_array($this->getListenersForEvent('response', $events));
$this->assertCount(0, $listeners);
}

public function testDefaultsToUtf8CharsetWhenCreatingJavascriptHeader()
Expand Down
Loading

0 comments on commit 73b70d8

Please sign in to comment.