Skip to content

Commit

Permalink
fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Oct 15, 2014
1 parent 2f12070 commit f040e2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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*


Expand Down
35 changes: 18 additions & 17 deletions src/Codeception/Specify.php
Expand Up @@ -36,39 +36,40 @@ 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;
}
if ($this->afterSpecify instanceof \Closure) $this->afterSpecify->__invoke();
}

// restore test name
$this->setName($name);
}

Expand All @@ -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 [[]];
Expand Down
17 changes: 17 additions & 0 deletions tests/SpecifyTest.php
Expand Up @@ -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);
}

}
Expand Down

0 comments on commit f040e2e

Please sign in to comment.