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

Commit

Permalink
Merge branch 'hotfix/check-for-arrayvariables-in-viewmodel' of https:…
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Aug 2, 2012
4 parents 0cab32d + 8adef43 + 69d1518 + 93921b3 commit 3a1f2fd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Model/ViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ public function setVariables($variables, $overwrite = false)
}

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

$this->variables = $variables;
return $this;
}
Expand Down
36 changes: 36 additions & 0 deletions test/Model/TestAsset/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_View
*/

namespace ZendTest\View\Model\TestAsset;

use Iterator;

class Variable implements Iterator
{
public function current()
{
}

public function key()
{
}

public function next()
{
}

public function rewind()
{
}

public function valid()
{
}
}
8 changes: 8 additions & 0 deletions test/Model/ViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Zend\View\Model\ViewModel;
use Zend\View\Variables as ViewVariables;
use ZendTest\View\Model\TestAsset\Variable;

/**
* @category Zend
Expand Down Expand Up @@ -53,6 +54,13 @@ public function testAllowsPassingTraversableArgumentsToVariablesAndOptionsInCons
$this->assertSame(iterator_to_array($options), $model->getOptions());
}

public function testAllowsPassingNonArrayAccessObjectsAsArrayInConstructor()
{
$vars = array('foo' => new Variable);
$model = new ViewModel($vars);
$this->assertSame($vars, $model->getVariables());
}

public function testCanSetVariablesSingly()
{
$model = new ViewModel(array('foo' => 'bar'));
Expand Down

0 comments on commit 3a1f2fd

Please sign in to comment.