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

Commit 81ed6ca

Browse files
committed
Correct the fix so the behaviour of the original code is not changed
Added tests to verify the change.
1 parent c711fbd commit 81ed6ca

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Model/ViewModel.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@ public function clearOptions()
239239
public function getVariable($name, $default = null)
240240
{
241241
$name = (string) $name;
242-
if (isset($this->variables[$name])) {
243-
return $this->variables[$name];
242+
243+
if (is_array($this->variables)) {
244+
if (array_key_exists($name, $this->variables)) {
245+
return $this->variables[$name];
246+
}
247+
} elseif ($this->variables->offsetExists($name)) {
248+
return $this->variables->offsetGet($name);
244249
}
245250

246251
return $default;

test/Model/ViewModelTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,56 @@ public function testCloneWithArray()
361361
$this->assertEquals('foo', $model1->getVariable('a'));
362362
$this->assertEquals('bar', $model2->getVariable('a'));
363363
}
364+
365+
public function variableValue()
366+
{
367+
return [
368+
// variables default expected
369+
370+
// if it is set always get the value
371+
[['foo' => 'bar'], 'baz', 'bar'],
372+
[['foo' => 'bar'], null, 'bar'],
373+
[new ArrayObject(['foo' => 'bar']), 'baz', 'bar'],
374+
[new ArrayObject(['foo' => 'bar']), null, 'bar'],
375+
376+
// if it is null always get null value
377+
[['foo' => null], null, null],
378+
[['foo' => null], 'baz', null],
379+
[new ArrayObject(['foo' => null]), null, null],
380+
[new ArrayObject(['foo' => null]), 'baz', null],
381+
382+
// when it is not set always get default value
383+
[[], 'baz', 'baz'],
384+
[new ArrayObject(), 'baz', 'baz'],
385+
];
386+
}
387+
388+
/**
389+
* @dataProvider variableValue
390+
*
391+
* @param array|ArrayObject $variables
392+
* @param string|null $default
393+
* @param string|null $expected
394+
*/
395+
public function testGetVariableSetByConstruct($variables, $default, $expected)
396+
{
397+
$model = new ViewModel($variables);
398+
399+
self::assertSame($expected, $model->getVariable('foo', $default));
400+
}
401+
402+
/**
403+
* @dataProvider variableValue
404+
*
405+
* @param array|ArrayObject $variables
406+
* @param string|null $default
407+
* @param string|null $expected
408+
*/
409+
public function testGetVariableSetBySetter($variables, $default, $expected)
410+
{
411+
$model = new ViewModel();
412+
$model->setVariables($variables);
413+
414+
self::assertSame($expected, $model->getVariable('foo', $default));
415+
}
364416
}

0 commit comments

Comments
 (0)