Skip to content

Commit

Permalink
Changed Event names back, added view type getter
Browse files Browse the repository at this point in the history
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 f9aa954
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
32 changes: 20 additions & 12 deletions lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -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();
}
}
/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions lib/Cake/View/View.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f9aa954

Please sign in to comment.