Skip to content
Permalink
Browse files

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
  • Loading branch information...
NickBeeuwsaert committed Jun 26, 2014
1 parent e8ee25f commit 7cef19fe96725c4320d10a2b1891f627aba45c4a
Showing with 48 additions and 9 deletions.
  1. +39 −1 lib/Cake/Test/Case/View/ViewTest.php
  2. +9 −8 lib/Cake/View/View.php
@@ -22,6 +22,8 @@
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 @@ public function __toString() {
*/
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 @@ public function testElementCache() {
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);
}
/**
@@ -1202,22 +1202,23 @@ protected function _elementCache($name, $data, $options) {
* @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']);
}

0 comments on commit 7cef19f

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