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
This commit is contained in:
Nick Beeuwsaert 2014-06-27 15:21:07 +00:00
parent 7cef19fe96
commit f9aa954562
2 changed files with 31 additions and 16 deletions

View file

@ -240,24 +240,24 @@ class TestObjectWithToString {
class TestObjectWithoutToString { class TestObjectWithoutToString {
} }
/** /**
* Class TestElementEventListener * Class TestViewEventListener
* *
* An event listener to test cakePHP events * An event listener to test cakePHP events
*/ */
class TestElementEventListener implements CakeEventListener { class TestViewEventListener implements CakeEventListener {
public $beforeRenderIsElement = false; public $beforeRenderViewType;
public $afterRenderIsElement = false; public $afterRenderViewType;
public function implementedEvents() { public function implementedEvents() {
return array( return array(
"Element.beforeRender"=>"beforeRender", "View.beforeRender"=>"beforeRender",
"Element.afterRender" =>"afterRender" "View.afterRender" =>"afterRender",
); );
} }
public function beforeRender($event) { 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) { 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 * @return void
*/ */
public function testElementEvent(){ public function testViewEvent(){
$View = new View($this->PostsController); $View = new View($this->PostsController);
$listener = new TestElementEventListener(); $View->autoLayout = false;
$listener = new TestViewEventListener();
$View->getEventManager()->attach($listener); $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)); $View->element('test_element', array(), array("callbacks"=>true));
$this->assertEquals(true, $listener->beforeRenderIsElement); $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
$this->assertEquals(true, $listener->afterRenderIsElement);
$this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
$this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
} }
/** /**

View file

@ -813,7 +813,14 @@ class View extends Object {
} }
$this->viewVars = $data + $this->viewVars; $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. * Magic accessor for helpers. Provides access to attributes that were deprecated.
* *
@ -1207,13 +1214,13 @@ class View extends Object {
$this->_currentType = self::TYPE_ELEMENT; $this->_currentType = self::TYPE_ELEMENT;
if ($options['callbacks']) { 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)); $element = $this->_render($file, array_merge($this->viewVars, $data));
if ($options['callbacks']) { 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->_currentType = $restore;