Skip to content

Commit 2615d16

Browse files
committed
Added support of cloning private properties of class that uses the Specify trait
1 parent 9550fff commit 2615d16

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/Codeception/Specify.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,16 @@ private function specifyRestoreProperties($properties)
223223
*/
224224
private function getSpecifyObjectProperties()
225225
{
226+
$objectReflection = new \ReflectionObject($this);
227+
$propertiesToClone = $objectReflection->getProperties();
228+
229+
if (($classProperties = $this->specifyGetClassPrivateProperties()) !== []) {
230+
$propertiesToClone = array_merge($propertiesToClone, $classProperties);
231+
}
232+
226233
$properties = [];
227-
$myReflection = new \ReflectionObject($this);
228234

229-
foreach ($myReflection->getProperties() as $property) {
235+
foreach ($propertiesToClone as $property) {
230236
if ($this->specifyConfig->propertyIgnored($property->getName())) {
231237
continue;
232238
}
@@ -256,6 +262,20 @@ private function specifyCheckMockObjects()
256262
}
257263
}
258264

265+
private function specifyGetClassPrivateProperties()
266+
{
267+
static $properties = [];
268+
269+
if (!isset($properties[__CLASS__])) {
270+
$reflection = new \ReflectionClass(__CLASS__);
271+
272+
$properties[__CLASS__] = (get_class($this) !== __CLASS__)
273+
? $reflection->getProperties(\ReflectionProperty::IS_PRIVATE) : [];
274+
}
275+
276+
return $properties[__CLASS__];
277+
}
278+
259279
/**
260280
* @return \ReflectionClass|null
261281
*/

tests/SpecifyTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class SpecifyTest extends \SpecifyUnitTest
44
{
55
protected $user;
66
protected $a;
7-
protected $private = false;
7+
8+
private $private = false;
89

910
public function testSpecification()
1011
{
@@ -231,12 +232,16 @@ function testPropertyRestore()
231232

232233
$this->assertEquals(['hello', 'world'], $this->testOne->prop);
233234
$this->assertFalse($this->private);
235+
$this->assertTrue($this->getPrivateProperty());
234236

235237
$this->specify('property $private should be restored properly', function() {
236238
$this->private = 'i\'m protected';
239+
$this->setPrivateProperty('i\'m private');
240+
$this->assertEquals('i\'m private', $this->getPrivateProperty());
237241
});
238242

239243
$this->assertFalse($this->private);
244+
$this->assertTrue($this->getPrivateProperty());
240245
}
241246

242247
public function testExamplesIndexInName()

tests/_support/SpecifyUnitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,19 @@ class SpecifyUnitTest extends \PHPUnit_Framework_TestCase
55
use Codeception\Specify;
66

77
private $private = true;
8+
9+
/**
10+
* @param mixed $private
11+
*/
12+
protected function setPrivateProperty($private)
13+
{
14+
$this->private = $private;
15+
}
16+
/**
17+
* @return mixed
18+
*/
19+
protected function getPrivateProperty()
20+
{
21+
return $this->private;
22+
}
823
}

0 commit comments

Comments
 (0)