diff --git a/CHANGELOG.md b/CHANGELOG.md index dbf3e20..507555c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### 0.4.0 +* Fixes cloning properties in examples. Issue #7 *2014-10-15* * added global and local specify configs, for disabling cloning properties and changing cloning methods *2014-10-15* diff --git a/src/Codeception/Specify.php b/src/Codeception/Specify.php index 683d89d..42b13df 100644 --- a/src/Codeception/Specify.php +++ b/src/Codeception/Specify.php @@ -36,32 +36,32 @@ function specify($specification, \Closure $callable = null, $params = []) $name = $this->getName(); $this->setName($this->getName().' | '.$specification); - // copy current object properties $properties = get_object_vars($this); - foreach ($properties as $property => $val) { - if ($this->specifyConfig->propertyIgnored($property)) continue; - if ($this->specifyConfig->classIgnored($val)) continue; - - if ($this->specifyConfig->propertyIsShallowCloned($property)) { - if (is_object($val)) { - $this->$property = clone $val; - } - } - if ($this->specifyConfig->propertyIsDeeplyCloned($property)) { - $this->$property = $this->copier->copy($val); - } - } - // prepare for execution $throws = $this->getSpecifyExpectedException($params); $examples = $this->getSpecifyExamples($params); foreach ($examples as $example) { + // copy current object properties + foreach ($properties as $property => $val) { + if ($this->specifyConfig->propertyIgnored($property)) continue; + if ($this->specifyConfig->classIgnored($val)) continue; + + if ($this->specifyConfig->propertyIsShallowCloned($property)) { + if (is_object($val)) { + $this->$property = clone $val; + } + } + if ($this->specifyConfig->propertyIsDeeplyCloned($property)) { + $this->$property = $this->copier->copy($val); + } + } + if ($this->beforeSpecify instanceof \Closure) $this->beforeSpecify->__invoke(); $this->specifyExecute($test, $throws, $example); - // restore class properties + // restore object properties foreach ($properties as $property => $val) { if (in_array($property, $this->specifyConfig->ignore)) continue; $this->$property = $val; @@ -69,6 +69,7 @@ function specify($specification, \Closure $callable = null, $params = []) if ($this->afterSpecify instanceof \Closure) $this->afterSpecify->__invoke(); } + // restore test name $this->setName($name); } @@ -80,7 +81,7 @@ function specify($specification, \Closure $callable = null, $params = []) private function getSpecifyExamples($params) { if (isset($params['examples'])) { - if (!is_array($params['examples'])) throw new \RuntimeException("Examples should be array"); + if (!is_array($params['examples'])) throw new \RuntimeException("Examples should be an array"); return $params['examples']; } return [[]]; diff --git a/tests/SpecifyTest.php b/tests/SpecifyTest.php index ba3819e..4081ce4 100644 --- a/tests/SpecifyTest.php +++ b/tests/SpecifyTest.php @@ -144,7 +144,24 @@ public function testConfiguration() }); $this->assertEquals("davert", $this->a->prop->prop); + } + /** + * @Issue https://github.com/Codeception/Specify/issues/6 + */ + function testPropertyRestore() + { + $this->testOne = new testOne(); + $this->testOne->prop = ['hello', 'world']; + + $this->specify('array contains hello+world', function ($testData) { + $this->testOne->prop = ['bye', 'world']; + $this->assertContains($testData, $this->testOne->prop); + }, ['examples' => [ + ['bye'], + ['world'], + ]]); + $this->assertEquals(['hello', 'world'], $this->testOne->prop); } }