Skip to content

Commit c7e7b3b

Browse files
committed
added cloneOnly option to config
1 parent 01e9dad commit c7e7b3b

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

RoboFile.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ public function release()
99

1010
$version = file_get_contents('VERSION');
1111

12-
// adding changelog and pushing it
13-
$this->taskGit()
14-
->add('CHANGELOG.md')
15-
->commit('updated changelog')
16-
->push()
17-
->run();
18-
1912
// create GitHub release
2013
$this->taskGitHubRelease($version)
2114
->uri('Codeception/Specify')

src/Codeception/Specify.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function specify($specification, \Closure $callable = null, $params = [])
5151

5252
// restore object properties
5353
foreach ($properties as $property => $val) {
54-
if (in_array($property, $this->specifyConfig->ignore)) continue;
54+
if ($this->specifyConfig->propertyIgnored($property)) continue;
5555
$this->$property = $val;
5656
}
5757
if ($this->afterSpecify instanceof \Closure) $this->afterSpecify->__invoke();
@@ -120,6 +120,7 @@ private function specifyExecute($test, $throws = false, $examples = array())
120120

121121
public function specifyConfig()
122122
{
123+
if (!$this->specifyConfig) $this->specifyConfig = Config::create();
123124
return new ConfigBuilder($this->specifyConfig);
124125
}
125126

src/Codeception/Specify/Config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ class Config
4545
public $ignore_classes = array();
4646
public $shallow = array();
4747
public $deep = array();
48+
public $only = null;
4849

4950
public function propertyIgnored($property)
5051
{
52+
if ($this->only) {
53+
return !in_array($property, $this->only);
54+
}
5155
return in_array($property, $this->ignore);
5256
}
5357

@@ -59,6 +63,9 @@ public function classIgnored($value)
5963

6064
public function propertyIsShallowCloned($property)
6165
{
66+
if ($this->only and !$this->is_deep) {
67+
return in_array($property, $this->only);
68+
}
6269
if (!$this->is_deep and !in_array($property, $this->deep)) {
6370
return true;
6471
}
@@ -67,6 +74,9 @@ public function propertyIsShallowCloned($property)
6774

6875
public function propertyIsDeeplyCloned($property)
6976
{
77+
if ($this->only and $this->is_deep) {
78+
return in_array($property, $this->only);
79+
}
7080
if ($this->is_deep and !in_array($property, $this->shallow)) {
7181
return true;
7282
}
@@ -140,6 +150,7 @@ static function create()
140150
$config->ignore_classes = self::$ignoredClasses;
141151
$config->shallow = array();
142152
$config->deep = array();
153+
$config->only = null;
143154
return $config;
144155
}
145156
}

src/Codeception/Specify/ConfigBuilder.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,25 @@ public function shallowClone($properties = true)
148148
$this->config->shallow = array_merge($this->config->shallow, $properties);
149149
return $this;
150150
}
151+
152+
/**
153+
* Clone only specific properties
154+
*
155+
* ```php
156+
* <?php
157+
* $this->specifyConfig()->cloneOnly('user', 'post');
158+
* ?>
159+
* ```
160+
*
161+
* @param $properties
162+
* @return $this
163+
*/
164+
public function cloneOnly($properties)
165+
{
166+
if (!is_array($properties)) {
167+
$properties = func_get_args();
168+
}
169+
$this->config->only = $properties;
170+
return $this;
171+
}
151172
}

tests/ConfigTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@ public function testCloneModes()
3030
$this->assertTrue($this->config->propertyIsDeeplyCloned('user'));
3131
}
3232

33+
public function testConfigOnly()
34+
{
35+
$this->config->deep = ['user', 'post', 'tag'];
36+
$this->config->only = ['user'];
37+
// $this->assertFalse($this->config->propertyIgnored('user'));
38+
// $this->assertTrue($this->config->propertyIgnored('post'));
39+
}
40+
3341
}
3442

tests/SpecifyTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ public function testConfiguration()
146146
$this->assertEquals("davert", $this->a->prop->prop);
147147
}
148148

149+
public function testCloneOnly()
150+
{
151+
$this->specifyConfig()
152+
->cloneOnly('user');
153+
154+
$this->user = "bob";
155+
$this->a = "rob";
156+
$this->specify("user should be jon", function() {
157+
$this->user = "jon";
158+
$this->a = 'alice';
159+
});
160+
$this->assertEquals('bob', $this->user);
161+
$this->assertEquals('alice', $this->a);
162+
}
163+
149164
/**
150165
* @Issue https://github.com/Codeception/Specify/issues/6
151166
*/

0 commit comments

Comments
 (0)