From 7cef19fe96725c4320d10a2b1891f627aba45c4a Mon Sep 17 00:00:00 2001 From: Nick Beeuwsaert Date: Wed, 25 Jun 2014 22:08:59 -0500 Subject: [PATCH] Changed `View::element()` to have an appropriately named event And also made it so when the event fires the subjects type is `View::TYPE_ELEMENT`. Wrote some tests which I hope are done right I don't really know the bes way to write a test for a event listener --- lib/Cake/Test/Case/View/ViewTest.php | 40 +++++++++++++++++++++++++++- lib/Cake/View/View.php | 17 ++++++------ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Test/Case/View/ViewTest.php b/lib/Cake/Test/Case/View/ViewTest.php index 7ad86dd15..a6263201a 100644 --- a/lib/Cake/Test/Case/View/ViewTest.php +++ b/lib/Cake/Test/Case/View/ViewTest.php @@ -22,6 +22,8 @@ App::uses('Controller', 'Controller'); App::uses('CacheHelper', 'View/Helper'); App::uses('HtmlHelper', 'View/Helper'); App::uses('ErrorHandler', 'Error'); +App::uses('CakeEventManager', 'Event'); +App::uses('CakeEventListener', 'Event'); /** * ViewPostsController class @@ -237,7 +239,27 @@ class TestObjectWithToString { */ class TestObjectWithoutToString { } - +/** + * Class TestElementEventListener + * + * An event listener to test cakePHP events + */ +class TestElementEventListener implements CakeEventListener { + public $beforeRenderIsElement = false; + public $afterRenderIsElement = false; + public function implementedEvents() { + return array( + "Element.beforeRender"=>"beforeRender", + "Element.afterRender" =>"afterRender" + ); + } + public function beforeRender($event) { + $this->beforeRenderIsElement = View::TYPE_ELEMENT == PHPUnit_Framework_Assert::readAttribute($event->subject(), "_currentType"); + } + public function afterRender($event) { + $this->afterRenderIsElement = View::TYPE_ELEMENT == PHPUnit_Framework_Assert::readAttribute($event->subject(), "_currentType"); + } +} /** * ViewTest class * @@ -806,6 +828,22 @@ class ViewTest extends CakeTestCase { Cache::clear(true, 'test_view'); Cache::drop('test_view'); + } + +/** + * Test element events + * + * @return void + */ + public function testElementEvent(){ + $View = new View($this->PostsController); + $listener = new TestElementEventListener(); + + $View->getEventManager()->attach($listener); + + $View->element('test_element', array(), array("callbacks"=>true)); + $this->assertEquals(true, $listener->beforeRenderIsElement); + $this->assertEquals(true, $listener->afterRenderIsElement); } /** diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index e1711beb5..900f98c35 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -1202,22 +1202,23 @@ class View extends Object { * @return string */ protected function _renderElement($file, $data, $options) { - if ($options['callbacks']) { - $this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file))); - } - $current = $this->_current; $restore = $this->_currentType; - $this->_currentType = self::TYPE_ELEMENT; + + if ($options['callbacks']) { + $this->getEventManager()->dispatch(new CakeEvent('Element.beforeRender', $this, array($file))); + } + $element = $this->_render($file, array_merge($this->viewVars, $data)); + if ($options['callbacks']) { + $this->getEventManager()->dispatch(new CakeEvent('Element.afterRender', $this, array($file, $element))); + } + $this->_currentType = $restore; $this->_current = $current; - if ($options['callbacks']) { - $this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element))); - } if (isset($options['cache'])) { Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']); }