Skip to content

Commit

Permalink
fixes view vars being set
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmackay committed Jul 10, 2015
1 parent bd66f5d commit 14da30b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/View/ViewVarsTrait.php
Expand Up @@ -42,6 +42,7 @@ trait ViewVarsTrait
public function getView($viewClass = null)
{
if ($viewClass === null && $this->View) {
$this->View->viewVars = $this->viewVars;
return $this->View;
}

Expand All @@ -65,6 +66,7 @@ public function getView($viewClass = null)
}

if ($this->View && $this->View instanceof $className) {
$this->View->viewVars = $this->viewVars;
return $this->View;
}

Expand Down Expand Up @@ -93,7 +95,7 @@ public function createView($viewClass = null)
}

$viewOptions = [];
foreach ($this->_validViewOptions as $option) {
foreach ($this->viewOptions() as $option) {
if (property_exists($this, $option)) {
$viewOptions[$option] = $this->{$option};
}
Expand Down
29 changes: 23 additions & 6 deletions tests/TestCase/View/ViewVarsTraitTest.php
Expand Up @@ -13,6 +13,7 @@
*/
namespace Cake\Test\TestCase\View;

use Cake\Controller\Controller;
use Cake\TestSuite\TestCase;
use Cake\View\ViewVarsTrait;

Expand All @@ -32,7 +33,7 @@ public function setUp()
{
parent::setUp();

$this->subject = $this->getObjectForTrait('Cake\View\ViewVarsTrait');
$this->subject = new Controller;
}

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ public function testAddOneViewOption()
$option = 'newOption';
$this->subject->viewOptions($option);

$this->assertContains($option, $this->subject->_validViewOptions);
$this->assertContains($option, $this->subject->viewOptions());
}

/**
Expand All @@ -110,7 +111,7 @@ public function testAddOneViewOption()
*/
public function testAddTwoViewOption()
{
$this->subject->_validViewOptions = ['oldOption'];
$this->subject->viewOptions(['oldOption'], false);
$option = ['newOption', 'anotherOption'];
$result = $this->subject->viewOptions($option);
$expects = ['oldOption', 'newOption', 'anotherOption'];
Expand All @@ -126,7 +127,7 @@ public function testAddTwoViewOption()
*/
public function testReadingViewOptions()
{
$expected = $this->subject->_validViewOptions = ['one', 'two', 'three'];
$expected = $this->subject->viewOptions(['one', 'two', 'three'], false);
$result = $this->subject->viewOptions();

$this->assertEquals($expected, $result);
Expand All @@ -139,7 +140,7 @@ public function testReadingViewOptions()
*/
public function testMergeFalseViewOptions()
{
$this->subject->_validViewOptions = ['one', 'two', 'three'];
$this->subject->viewOptions(['one', 'two', 'three'], false);
$expected = ['four', 'five', 'six'];
$result = $this->subject->viewOptions($expected, false);

Expand All @@ -153,12 +154,28 @@ public function testMergeFalseViewOptions()
*/
public function testUndefinedValidViewOptions()
{
$result = $this->subject->viewOptions();
$result = $this->subject->viewOptions([], false);

$this->assertTrue(is_array($result));
$this->assertTrue(empty($result));
}

/**
* test that getView() updates viewVars of View instance on each call.
*
* @return void
*/
public function testUptoDateViewVars()
{
$expected = ['one' => 'one'];
$this->subject->set($expected);
$this->assertEquals($expected, $this->subject->getView()->viewVars);

$expected = ['one' => 'one', 'two' => 'two'];
$this->subject->set($expected);
$this->assertEquals($expected, $this->subject->getView()->viewVars);
}

/**
* test getView() throws exception if view class cannot be found
*
Expand Down

0 comments on commit 14da30b

Please sign in to comment.