Skip to content
Permalink
Browse files

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
  • Loading branch information...
NickBeeuwsaert committed Jun 27, 2014
1 parent 7cef19f commit f9aa954562f8d6ea21e42be40b72c8afa33ff393
Showing with 31 additions and 16 deletions.
  1. +20 −12 lib/Cake/Test/Case/View/ViewTest.php
  2. +11 −4 lib/Cake/View/View.php
@@ -240,24 +240,24 @@ public function __toString() {
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 @@ public function testElementCache() {
*
* @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);
}
/**
@@ -813,7 +813,14 @@ public function set($one, $two = null) {
}
$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 @@ protected function _renderElement($file, $data, $options) {
$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;

0 comments on commit f9aa954

Please sign in to comment.
You can’t perform that action at this time.