From f9aa954562f8d6ea21e42be40b72c8afa33ff393 Mon Sep 17 00:00:00 2001 From: Nick Beeuwsaert Date: Fri, 27 Jun 2014 15:21:07 +0000 Subject: [PATCH] Changed Event names back, added view type getter Changed `Element.beforeRender` and `Element.afterRender` back to `View.beforeRender` and `View.afterRender` Also added a getter for `View::_currentType`. Added more tests to make sure that things were getting reset right And also that View::render and View::element events were working As expected Changed Event names back, added view type getter Changed `Element.beforeRender` and `Element.afterRender` back to `View.beforeRender` and `View.afterRender` Also added a getter and setter for `View::_currentType`. Added more tests to make sure that things were getting reset right And also that View::render and View::element events were working As expected --- lib/Cake/Test/Case/View/ViewTest.php | 32 +++++++++++++++++----------- lib/Cake/View/View.php | 15 +++++++++---- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Test/Case/View/ViewTest.php b/lib/Cake/Test/Case/View/ViewTest.php index a6263201a..68fd1583f 100644 --- a/lib/Cake/Test/Case/View/ViewTest.php +++ b/lib/Cake/Test/Case/View/ViewTest.php @@ -240,24 +240,24 @@ class TestObjectWithToString { class TestObjectWithoutToString { } /** - * Class TestElementEventListener + * Class TestViewEventListener * * An event listener to test cakePHP events */ -class TestElementEventListener implements CakeEventListener { - public $beforeRenderIsElement = false; - public $afterRenderIsElement = false; +class TestViewEventListener implements CakeEventListener { + public $beforeRenderViewType; + public $afterRenderViewType; public function implementedEvents() { return array( - "Element.beforeRender"=>"beforeRender", - "Element.afterRender" =>"afterRender" + "View.beforeRender"=>"beforeRender", + "View.afterRender" =>"afterRender", ); } public function beforeRender($event) { - $this->beforeRenderIsElement = View::TYPE_ELEMENT == PHPUnit_Framework_Assert::readAttribute($event->subject(), "_currentType"); + $this->beforeRenderViewType = $event->subject()->getCurrentType(); } public function afterRender($event) { - $this->afterRenderIsElement = View::TYPE_ELEMENT == PHPUnit_Framework_Assert::readAttribute($event->subject(), "_currentType"); + $this->afterRenderViewType = $event->subject()->getCurrentType(); } } /** @@ -835,15 +835,23 @@ class ViewTest extends CakeTestCase { * * @return void */ - public function testElementEvent(){ + public function testViewEvent(){ $View = new View($this->PostsController); - $listener = new TestElementEventListener(); + $View->autoLayout = false; + $listener = new TestViewEventListener(); $View->getEventManager()->attach($listener); + $View->render('index'); + $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType); + $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType); + + $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW); $View->element('test_element', array(), array("callbacks"=>true)); - $this->assertEquals(true, $listener->beforeRenderIsElement); - $this->assertEquals(true, $listener->afterRenderIsElement); + $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW); + + $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType); + $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType); } /** diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index 900f98c35..d0fcd933e 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -813,7 +813,14 @@ class View extends Object { } $this->viewVars = $data + $this->viewVars; } - +/** + * Retreived the current view type + * + * @return string + */ + public function getCurrentType(){ + return $this->_currentType; + } /** * Magic accessor for helpers. Provides access to attributes that were deprecated. * @@ -1207,14 +1214,14 @@ class View extends Object { $this->_currentType = self::TYPE_ELEMENT; if ($options['callbacks']) { - $this->getEventManager()->dispatch(new CakeEvent('Element.beforeRender', $this, array($file))); + $this->getEventManager()->dispatch(new CakeEvent('View.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->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element))); + } $this->_currentType = $restore; $this->_current = $current;