-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from ischenko/verify-mock-objects
Added ability isolate and verify mock objects within a specification
- Loading branch information
Showing
4 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
namespace Codeception\Specify; | ||
|
||
/** | ||
* Helper for manipulating by an object property. | ||
* | ||
* @author Roman Ishchenko <roman@ishchenko.ck.ua> | ||
*/ | ||
class ObjectProperty | ||
{ | ||
/** | ||
* @var mixed | ||
*/ | ||
private $_owner; | ||
|
||
/** | ||
* @var \ReflectionProperty|string | ||
*/ | ||
private $_property; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $_initValue; | ||
|
||
/** | ||
* ObjectProperty constructor. | ||
* | ||
* @param $owner | ||
* @param $property | ||
* @param $value | ||
*/ | ||
public function __construct($owner, $property, $value = null) | ||
{ | ||
$this->_owner = $owner; | ||
$this->_property = $property; | ||
|
||
if (!($this->_property instanceof \ReflectionProperty)) { | ||
$this->_property = new \ReflectionProperty($owner, $this->_property); | ||
} | ||
|
||
$this->_property->setAccessible(true); | ||
|
||
$this->_initValue = ($value === null ? $this->getValue() : $value); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->_property->getName(); | ||
} | ||
|
||
/** | ||
* Restores initial value | ||
*/ | ||
public function restoreValue() | ||
{ | ||
$this->setValue($this->_initValue); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->_property->getValue($this->_owner); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function setValue($value) | ||
{ | ||
$this->_property->setValue($this->_owner, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
require_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
class ObjectPropertyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $private = 'private'; | ||
|
||
public function testConstruction() | ||
{ | ||
$this->prop = 'test'; | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty($this, 'prop'); | ||
|
||
$this->assertEquals('prop', $prop->getName()); | ||
$this->assertEquals('test', $prop->getValue()); | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty($this, 'private'); | ||
|
||
$this->assertEquals('private', $prop->getName()); | ||
$this->assertEquals('private', $prop->getValue()); | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty( | ||
$this, new ReflectionProperty($this, 'private') | ||
); | ||
|
||
$this->assertEquals('private', $prop->getName()); | ||
$this->assertEquals('private', $prop->getValue()); | ||
} | ||
|
||
public function testRestore() | ||
{ | ||
$this->prop = 'test'; | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty($this, 'prop'); | ||
$prop->setValue('another value'); | ||
|
||
$this->assertEquals('another value', $this->prop); | ||
|
||
$prop->restoreValue(); | ||
|
||
$this->assertEquals('test', $this->prop); | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty($this, 'private'); | ||
$prop->setValue('another private value'); | ||
|
||
$this->assertEquals('another private value', $this->private); | ||
|
||
$prop->restoreValue(); | ||
|
||
$this->assertEquals('private', $this->private); | ||
|
||
$prop = new \Codeception\Specify\ObjectProperty($this, 'prop', 'testing'); | ||
|
||
$this->assertEquals('test', $prop->getValue()); | ||
|
||
$prop->setValue('Hello, World!'); | ||
|
||
$this->assertEquals($prop->getValue(), $this->prop); | ||
$this->assertEquals('Hello, World!', $prop->getValue()); | ||
|
||
$prop->restoreValue(); | ||
|
||
$this->assertEquals($prop->getValue(), $this->prop); | ||
$this->assertEquals('testing', $prop->getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters