Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Model/ViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ public function __unset($name)
unset($this->variables[$name]);
}

/**
* Called after this view model is cloned.
* Clones variables property so variables changes done in the new instance don't change the old one.
*
* @return void
*/
public function __clone()
{
if (is_object($this->variables)) {
$this->variables = clone $this->variables;
}
}

/**
* Set a single option
*
Expand Down
21 changes: 21 additions & 0 deletions test/Model/ViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,25 @@ public function testGetChildrenByCaptureToNonRecursive()

$this->assertEmpty($model->getChildrenByCaptureTo('bar', false));
}

public function testCloneCopiesVariables()
{
$model1 = new ViewModel();
$model1->setVariables(['a' => 'foo']);
$model2 = clone $model1;
$model2->setVariables(['a' => 'bar']);

$this->assertEquals('foo', $model1->getVariable('a'));
$this->assertEquals('bar', $model2->getVariable('a'));
}

public function testCloneWithArray()
{
$model1 = new ViewModel(['a' => 'foo']);
$model2 = clone $model1;
$model2->setVariables(['a' => 'bar']);

$this->assertEquals('foo', $model1->getVariable('a'));
$this->assertEquals('bar', $model2->getVariable('a'));
}
}