Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#2074 from weierophinne…
Browse files Browse the repository at this point in the history
…y/hotfix/view-model-variables

ViewModel::setVariables() should not overwrite
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
34 changes: 19 additions & 15 deletions src/Model/ViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function __construct($variables = null, $options = null)
if (null === $variables) {
$variables = new ViewVariables();
}
$this->setVariables($variables);

// Initializing the variables container
$this->setVariables($variables, true);

if (null !== $options) {
$this->setOptions($options);
Expand Down Expand Up @@ -246,29 +248,31 @@ public function setVariable($name, $value)
* Can be an array or a Traversable + ArrayAccess object.
*
* @param array|ArrayAccess&Traversable $variables
* @param bool $overwrite Whether or not to overwrite the internal container with $variables
* @return ViewModel
*/
public function setVariables($variables)
public function setVariables($variables, $overwrite = false)
{
// Assumption is that renderers can handle arrays or ArrayAccess objects
if ($variables instanceof ArrayAccess && $variables instanceof Traversable) {
$this->variables = $variables;
return $this;
}

if ($variables instanceof Traversable) {
$variables = ArrayUtils::iteratorToArray($variables);
}

if (!is_array($variables)) {
if (!is_array($variables) && !$variables instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: expects an array, or Traversable ArrayAccess argument; received "%s"',
'%s: expects an array, or Traversable argument; received "%s"',
__METHOD__,
(is_object($variables) ? get_class($variables) : gettype($variables))
));
}

$this->variables = $variables;
if ($overwrite) {
if (!is_object($variables) && !$variables instanceof ArrayAccess) {
$variables = ArrayUtils::iteratorToArray($variables);
}
$this->variables = $variables;
return $this;
}

foreach ($variables as $key => $value) {
$this->setVariable($key, $value);
}

return $this;
}

Expand Down
20 changes: 14 additions & 6 deletions test/Model/ViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function testCanOverwriteVariablesSingly()
$this->assertEquals(array('foo' => 'baz'), $model->getVariables());
}

public function testSetVariablesOverwritesAllPreviouslyStored()
public function testSetVariablesMergesWithPreviouslyStoredVariables()
{
$model = new ViewModel(array('foo' => 'bar', 'bar' => 'baz'));
$model->setVariables(array('bar' => 'BAZBAT'));
$this->assertEquals(array('bar' => 'BAZBAT'), $model->getVariables());
$this->assertEquals(array('foo' => 'bar', 'bar' => 'BAZBAT'), $model->getVariables());
}

public function testCanSetOptionsSingly()
Expand Down Expand Up @@ -212,14 +212,22 @@ public function testAddChildAllowsSpecifyingCaptureToValue()
$this->assertEquals('foo', $child->captureTo());
}

public function testAllowsPassingViewVariablesContainerAsVariables()
public function testAllowsPassingViewVariablesContainerAsVariablesToConstructor()
{
$variables = new ViewVariables();
$model = new ViewModel();
$model->setVariables($variables);
$model = new ViewModel($variables);
$this->assertSame($variables, $model->getVariables());
}

public function testPassingOverwriteFlagWhenSettingVariablesOverwritesContainer()
{
$variables = new ViewVariables(array('foo' => 'bar'));
$model = new ViewModel($variables);
$overwrite = new ViewVariables(array('foo' => 'baz'));
$model->setVariables($overwrite, true);
$this->assertSame($overwrite, $model->getVariables());
}

public function testPropertyOverloadingGivesAccessToProperties()
{
$model = new ViewModel();
Expand All @@ -243,7 +251,7 @@ public function testPropertyOverloadingAllowsWritingPropertiesAfterSetVariablesH
$this->assertTrue(isset($model->bar));
$this->assertEquals('baz', $model->bar);
$variables = $model->getVariables();
$this->assertArrayHasKey('bar', $variables);
$this->assertTrue(isset($variables['bar']));
$this->assertEquals('baz', $variables['bar']);
}
}
5 changes: 4 additions & 1 deletion test/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function testRendersViewModelWithNoChildren()
$this->model->setVariables($variables);
$this->view->render($this->model);

$this->assertEquals(var_export($variables, true), $this->result->content);
foreach ($variables as $key => $value) {
$expect = sprintf("'%s' => '%s',", $key, $value);
$this->assertContains($expect, $this->result->content);
}
}

public function testRendersViewModelWithChildren()
Expand Down

0 comments on commit 0cab32d

Please sign in to comment.