mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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
This commit is contained in:
parent
e8ee25f40d
commit
7cef19fe96
2 changed files with 48 additions and 9 deletions
|
@ -22,6 +22,8 @@ App::uses('Controller', 'Controller');
|
||||||
App::uses('CacheHelper', 'View/Helper');
|
App::uses('CacheHelper', 'View/Helper');
|
||||||
App::uses('HtmlHelper', 'View/Helper');
|
App::uses('HtmlHelper', 'View/Helper');
|
||||||
App::uses('ErrorHandler', 'Error');
|
App::uses('ErrorHandler', 'Error');
|
||||||
|
App::uses('CakeEventManager', 'Event');
|
||||||
|
App::uses('CakeEventListener', 'Event');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ViewPostsController class
|
* ViewPostsController class
|
||||||
|
@ -237,7 +239,27 @@ class TestObjectWithToString {
|
||||||
*/
|
*/
|
||||||
class TestObjectWithoutToString {
|
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
|
* ViewTest class
|
||||||
*
|
*
|
||||||
|
@ -806,6 +828,22 @@ class ViewTest extends CakeTestCase {
|
||||||
|
|
||||||
Cache::clear(true, 'test_view');
|
Cache::clear(true, 'test_view');
|
||||||
Cache::drop('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 @@ class View extends Object {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderElement($file, $data, $options) {
|
protected function _renderElement($file, $data, $options) {
|
||||||
if ($options['callbacks']) {
|
|
||||||
$this->getEventManager()->dispatch(new CakeEvent('View.beforeRender', $this, array($file)));
|
|
||||||
}
|
|
||||||
|
|
||||||
$current = $this->_current;
|
$current = $this->_current;
|
||||||
$restore = $this->_currentType;
|
$restore = $this->_currentType;
|
||||||
|
|
||||||
$this->_currentType = self::TYPE_ELEMENT;
|
$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));
|
$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->_currentType = $restore;
|
||||||
$this->_current = $current;
|
$this->_current = $current;
|
||||||
|
|
||||||
if ($options['callbacks']) {
|
|
||||||
$this->getEventManager()->dispatch(new CakeEvent('View.afterRender', $this, array($file, $element)));
|
|
||||||
}
|
|
||||||
if (isset($options['cache'])) {
|
if (isset($options['cache'])) {
|
||||||
Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']);
|
Cache::write($this->elementCacheSettings['key'], $element, $this->elementCacheSettings['config']);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue