Skip to content

Commit

Permalink
Modifying View to accept HelperCollection. Removing methods that are …
Browse files Browse the repository at this point in the history
…no longer needed.
  • Loading branch information
markstory committed Aug 11, 2010
1 parent f8ecb10 commit a232077
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
23 changes: 5 additions & 18 deletions cake/libs/view/view.php
Expand Up @@ -466,8 +466,9 @@ public function renderLayout($content_for_layout, $layout = null) {
if (!isset($dataForLayout['title_for_layout'])) {
$dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
}

if (empty($this->loaded) && !empty($this->helpers)) {

$attached = $this->Helpers->attached();
if (empty($attached) && !empty($this->helpers)) {
$loadHelpers = true;
} else {
$loadHelpers = false;
Expand All @@ -488,18 +489,6 @@ public function renderLayout($content_for_layout, $layout = null) {
return $this->output;
}

/**
* Fire a callback on all loaded Helpers. All helpers must implement this method,
* it is not checked before being called. You can add additional helper callbacks in AppHelper.
*
* @param string $callback name of callback fire.
* @access protected
* @return void
*/
function _triggerHelpers($callback) {
$this->Helpers->trigger($callback, array(&$this));
}

/**
* Render cached view. Works in concert with CacheHelper and Dispatcher to
* render cached view files.
Expand Down Expand Up @@ -698,10 +687,8 @@ public function loadHelpers() {
* @return string Rendered output
*/
protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
$loadedHelpers = array();

$attached = $this->Helpers->attached();
if (count($attached) == 0 && $loadHelpers === true) {
if (count($attached) === 0 && $loadHelpers === true) {
$this->loadHelpers();
$this->Helpers->trigger('beforeRender', array(&$this));
unset($attached);
Expand All @@ -722,7 +709,7 @@ protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $ca

$out = ob_get_clean();
$caching = (
isset($this->loaded['cache']) &&
isset($this->Helpers->Cache) &&
(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
);

Expand Down
53 changes: 23 additions & 30 deletions cake/tests/cases/libs/view/view.test.php
Expand Up @@ -579,16 +579,20 @@ function testLoadHelpers() {
* @return void
*/
function testHelperCallbackTriggering() {
$mockHelper = $this->getMock('Helper', array(), array(), 'CallbackMockHelper');
$this->PostsController->helpers = array('Session', 'Html', 'CallbackMock');

$View = new View($this->PostsController);
$View->loadHelpers();

$View->Helpers->CallbackMock->expects($this->once())->method('beforeRender');
$View->Helpers->CallbackMock->expects($this->once())->method('afterRender');
$View->Helpers->CallbackMock->expects($this->once())->method('beforeLayout');
$View->Helpers->CallbackMock->expects($this->once())->method('afterLayout');
$View->helpers = array('Html', 'Session');
$View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));

$View->Helpers->expects($this->at(0))->method('trigger')
->with('beforeRender', new PHPUnit_Framework_Constraint_IsAnything());
$View->Helpers->expects($this->at(1))->method('trigger')
->with('afterRender', new PHPUnit_Framework_Constraint_IsAnything());

$View->Helpers->expects($this->at(2))->method('trigger')
->with('beforeLayout', new PHPUnit_Framework_Constraint_IsAnything());
$View->Helpers->expects($this->at(3))->method('trigger')
->with('afterLayout', new PHPUnit_Framework_Constraint_IsAnything());

$View->render('index');
}

Expand All @@ -601,8 +605,8 @@ function testHelperCallbackTriggering() {
function testBeforeLayout() {
$this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
$View = new View($this->PostsController);
$out = $View->render('index');
$this->assertEqual($View->loaded['testAfter']->property, 'Valuation');
$View->render('index');
$this->assertEqual($View->Helpers->TestAfter->property, 'Valuation');
}

/**
Expand Down Expand Up @@ -637,32 +641,18 @@ function testRenderLoadHelper() {
$result = $View->render_($View->getViewFileName('index'), array());
$this->assertEqual($result, 'posts index');

$helpers = $View->loaded;
$this->assertTrue($helpers['html'] instanceof HtmlHelper);
$this->assertTrue($helpers['form'] instanceof FormHelper);
$this->assertTrue($helpers['form']->Html instanceof HtmlHelper);
$this->assertTrue($helpers['number'] instanceof NumberHelper);
$attached = $View->Helpers->attached();
$this->assertEquals($attached, array('Session', 'Html', 'Form', 'Number'));

$this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
$View = new TestView($this->PostsController);

$result = $View->render_($View->getViewFileName('index'), array());
$this->assertEqual($result, 'posts index');

$helpers = $View->loaded;
$this->assertTrue($helpers['html'] instanceof HtmlHelper);
$this->assertTrue($helpers['form'] instanceof FormHelper);
$this->assertTrue($helpers['form']->Html instanceof HtmlHelper);
$this->assertTrue($helpers['number'] instanceof NumberHelper);
$this->assertTrue($helpers['pluggedHelper']->OtherHelper instanceof OtherHelperHelper);

$this->assertTrue($View->Html instanceof HtmlHelper);
$this->assertTrue($View->Form instanceof FormHelper);
$this->assertTrue($View->Form->Html instanceof HtmlHelper);
$this->assertTrue($View->PluggedHelper->OtherHelper instanceof OtherHelperHelper);
$this->assertReference($View->Form, $View->loaded['form']);
$this->assertReference($View->Html, $View->loaded['html']);
$this->assertReference($View->PluggedHelper->OtherHelper, $View->loaded['otherHelper']);
$attached = $View->Helpers->attached();
$expected = array('Html', 'Form', 'Number', 'PluggedHelper');
$this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
}

/**
Expand Down Expand Up @@ -718,6 +708,9 @@ function testRenderLayoutWithMockCacheHelper() {
$Controller = new ViewPostsController();
$Controller->cacheAction = '1 day';
$View = new View($Controller);
$View->helpers = array('Cache', 'Html', 'Session');
$View->loadHelpers();

$View->Helpers->Cache = $this->getMock('CacheHelper');
$View->Helpers->Cache->expects($this->exactly(2))->method('cache');

Expand Down

0 comments on commit a232077

Please sign in to comment.