diff --git a/composer.json b/composer.json index e5d00fe2..bf89ef7e 100644 --- a/composer.json +++ b/composer.json @@ -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" }, @@ -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", @@ -62,7 +62,7 @@ "extra": { "branch-alias": { "dev-master": "2.5-dev", - "dev-develop": "2.6-dev" + "dev-develop": "3.0-dev" } }, "autoload-dev": { diff --git a/src/View.php b/src/View.php index 325972cc..90eca859 100644 --- a/src/View.php +++ b/src/View.php @@ -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( @@ -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 @@ -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); } /** diff --git a/test/EventManagerIntrospectionTrait.php b/test/EventManagerIntrospectionTrait.php new file mode 100644 index 00000000..11b69d48 --- /dev/null +++ b/test/EventManagerIntrospectionTrait.php @@ -0,0 +1,56 @@ +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; + } + } + } + } +} diff --git a/test/Helper/Navigation/AbstractHelperTest.php b/test/Helper/Navigation/AbstractHelperTest.php index 0a4eed6c..e53f8778 100644 --- a/test/Helper/Navigation/AbstractHelperTest.php +++ b/test/Helper/Navigation/AbstractHelperTest.php @@ -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() diff --git a/test/Helper/Navigation/AbstractTest.php b/test/Helper/Navigation/AbstractTest.php index ea6acfce..dbcf3e74 100644 --- a/test/Helper/Navigation/AbstractTest.php +++ b/test/Helper/Navigation/AbstractTest.php @@ -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 diff --git a/test/Helper/UrlIntegrationTest.php b/test/Helper/UrlIntegrationTest.php index 9af27624..6db936eb 100644 --- a/test/Helper/UrlIntegrationTest.php +++ b/test/Helper/UrlIntegrationTest.php @@ -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' => [ diff --git a/test/Strategy/FeedStrategyTest.php b/test/Strategy/FeedStrategyTest.php index c25f51bd..f3c67d36 100644 --- a/test/Strategy/FeedStrategyTest.php +++ b/test/Strategy/FeedStrategyTest.php @@ -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; @@ -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'); @@ -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'); @@ -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); } } diff --git a/test/Strategy/JsonStrategyTest.php b/test/Strategy/JsonStrategyTest.php index c51e96ff..975991db 100644 --- a/test/Strategy/JsonStrategyTest.php +++ b/test/Strategy/JsonStrategyTest.php @@ -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; @@ -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'); @@ -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'); @@ -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() diff --git a/test/Strategy/PhpRendererStrategyTest.php b/test/Strategy/PhpRendererStrategyTest.php index 4aa6cd35..19e3a175 100644 --- a/test/Strategy/PhpRendererStrategyTest.php +++ b/test/Strategy/PhpRendererStrategyTest.php @@ -15,9 +15,12 @@ use Zend\View\Renderer\PhpRenderer; use Zend\View\Strategy\PhpRendererStrategy; use Zend\View\ViewEvent; +use ZendTest\View\EventManagerIntrospectionTrait; class PhpRendererStrategyTest extends TestCase { + use EventManagerIntrospectionTrait; + public function setUp() { $this->renderer = new PhpRenderer; @@ -108,20 +111,19 @@ public function testContentPlaceholdersListIsMutable() 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'); @@ -131,20 +133,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'); @@ -154,15 +155,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); } }