Skip to content

Commit f040e2e

Browse files
committed
fix #7
1 parent 2f12070 commit f040e2e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 0.4.0
44

5+
* Fixes cloning properties in examples. Issue #7 *2014-10-15*
56
* added global and local specify configs, for disabling cloning properties and changing cloning methods *2014-10-15*
67

78

src/Codeception/Specify.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,40 @@ function specify($specification, \Closure $callable = null, $params = [])
3636
$name = $this->getName();
3737
$this->setName($this->getName().' | '.$specification);
3838

39-
// copy current object properties
4039
$properties = get_object_vars($this);
41-
foreach ($properties as $property => $val) {
42-
if ($this->specifyConfig->propertyIgnored($property)) continue;
43-
if ($this->specifyConfig->classIgnored($val)) continue;
44-
45-
if ($this->specifyConfig->propertyIsShallowCloned($property)) {
46-
if (is_object($val)) {
47-
$this->$property = clone $val;
48-
}
49-
}
50-
if ($this->specifyConfig->propertyIsDeeplyCloned($property)) {
51-
$this->$property = $this->copier->copy($val);
52-
}
53-
}
54-
5540

5641
// prepare for execution
5742
$throws = $this->getSpecifyExpectedException($params);
5843
$examples = $this->getSpecifyExamples($params);
5944

6045
foreach ($examples as $example) {
46+
// copy current object properties
47+
foreach ($properties as $property => $val) {
48+
if ($this->specifyConfig->propertyIgnored($property)) continue;
49+
if ($this->specifyConfig->classIgnored($val)) continue;
50+
51+
if ($this->specifyConfig->propertyIsShallowCloned($property)) {
52+
if (is_object($val)) {
53+
$this->$property = clone $val;
54+
}
55+
}
56+
if ($this->specifyConfig->propertyIsDeeplyCloned($property)) {
57+
$this->$property = $this->copier->copy($val);
58+
}
59+
}
60+
6161
if ($this->beforeSpecify instanceof \Closure) $this->beforeSpecify->__invoke();
6262
$this->specifyExecute($test, $throws, $example);
6363

64-
// restore class properties
64+
// restore object properties
6565
foreach ($properties as $property => $val) {
6666
if (in_array($property, $this->specifyConfig->ignore)) continue;
6767
$this->$property = $val;
6868
}
6969
if ($this->afterSpecify instanceof \Closure) $this->afterSpecify->__invoke();
7070
}
7171

72+
// restore test name
7273
$this->setName($name);
7374
}
7475

@@ -80,7 +81,7 @@ function specify($specification, \Closure $callable = null, $params = [])
8081
private function getSpecifyExamples($params)
8182
{
8283
if (isset($params['examples'])) {
83-
if (!is_array($params['examples'])) throw new \RuntimeException("Examples should be array");
84+
if (!is_array($params['examples'])) throw new \RuntimeException("Examples should be an array");
8485
return $params['examples'];
8586
}
8687
return [[]];

tests/SpecifyTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,24 @@ public function testConfiguration()
144144
});
145145

146146
$this->assertEquals("davert", $this->a->prop->prop);
147+
}
147148

149+
/**
150+
* @Issue https://github.com/Codeception/Specify/issues/6
151+
*/
152+
function testPropertyRestore()
153+
{
154+
$this->testOne = new testOne();
155+
$this->testOne->prop = ['hello', 'world'];
156+
157+
$this->specify('array contains hello+world', function ($testData) {
158+
$this->testOne->prop = ['bye', 'world'];
159+
$this->assertContains($testData, $this->testOne->prop);
160+
}, ['examples' => [
161+
['bye'],
162+
['world'],
163+
]]);
164+
$this->assertEquals(['hello', 'world'], $this->testOne->prop);
148165
}
149166

150167
}

0 commit comments

Comments
 (0)