Skip to content

Commit

Permalink
Merge pull request #35 from Codeception/revert-34-verify-mock-objects
Browse files Browse the repository at this point in the history
Revert "Added ability isolate and verify mock objects within a specification"
  • Loading branch information
DavertMik committed Oct 17, 2016
2 parents e8abceb + 09b6c23 commit 5fb1d68
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 242 deletions.
100 changes: 20 additions & 80 deletions src/Codeception/Specify.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

use Codeception\Specify\Config;
use Codeception\Specify\ConfigBuilder;
use Codeception\Specify\ObjectProperty;

trait Specify
{
trait Specify {

private $beforeSpecify = array();
private $afterSpecify = array();
Expand All @@ -29,7 +27,7 @@ private function specifyInit()
if (!$this->specifyConfig) $this->specifyConfig = Config::create();
}

function specify($specification, \Closure $callable = null, $params = [])
function specify($specification, \Closure $callable = null, $params = [])
{
if (!$callable) return;
$this->specifyInit();
Expand All @@ -40,7 +38,7 @@ function specify($specification, \Closure $callable = null, $params = [])

$this->setName($newName);

$properties = $this->getSpecifyObjectProperties();
$properties = get_object_vars($this);

// prepare for execution
$throws = $this->getSpecifyExpectedException($params);
Expand All @@ -60,12 +58,13 @@ function specify($specification, \Closure $callable = null, $params = [])
if ($closure instanceof \Closure) $closure->__invoke();
}
}

$this->specifyExecute($test, $throws, $example);

// restore object properties
$this->specifyRestoreProperties($properties);

foreach ($properties as $property => $val) {
if ($this->specifyConfig->propertyIgnored($property)) continue;
$this->$property = $val;
}
if (!empty($this->afterSpecify) && is_array($this->afterSpecify)) {
foreach ($this->afterSpecify as $closure) {
if ($closure instanceof \Closure) $closure->__invoke();
Expand Down Expand Up @@ -121,10 +120,8 @@ private function specifyExecute($test, $throws = false, $examples = array())
}

$result = $this->getTestResultObject();

try {
call_user_func_array($test, $examples);
$this->specifyCheckMockObjects();
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
if ($throws !== get_class($e)){
$result->addFailure(clone($this), $e, $result->time());
Expand Down Expand Up @@ -182,86 +179,29 @@ function cleanSpecify()
}

/**
* @param ObjectProperty[] $properties
* @param $properties
* @return array
*/
private function specifyCloneProperties($properties)
{
foreach ($properties as $property) {
$propertyName = $property->getName();
$propertyValue = $property->getValue();

if ($this->specifyConfig->classIgnored($propertyValue)) {
foreach ($properties as $property => $val) {
if ($this->specifyConfig->propertyIgnored($property)) {
continue;
}
if ($this->specifyConfig->classIgnored($val)) {
continue;
}

if ($this->specifyConfig->propertyIsShallowCloned($propertyName)) {
if (is_object($propertyValue)) {
$property->setValue(clone $propertyValue);
if ($this->specifyConfig->propertyIsShallowCloned($property)) {
if (is_object($val)) {
$this->$property = clone $val;
} else {
$property->setValue($propertyValue);
$this->$property = $val;
}
}

if ($this->specifyConfig->propertyIsDeeplyCloned($propertyName)) {
$property->setValue($this->copier->copy($propertyValue));
if ($this->specifyConfig->propertyIsDeeplyCloned($property)) {
$this->$property = $this->copier->copy($val);
}
}
}

/**
* @param ObjectProperty[] $properties
*/
private function specifyRestoreProperties($properties)
{
foreach ($properties as $property) {
$property->restoreValue();
}
}

/**
* @return ObjectProperty[]
*/
private function getSpecifyObjectProperties()
{
$properties = [];

foreach (get_object_vars($this) as $property => $value) {
if ($this->specifyConfig->propertyIgnored($property)) {
continue;
}

$properties[] = new ObjectProperty($this, $property, $value);
}

// isolate mockObjects property from PHPUnit_Framework_TestCase
if (($phpUnitReflection = $this->specifyGetPhpUnitReflection()) !== null) {
$properties[] = $mockObjects = new ObjectProperty(
$this, $phpUnitReflection->getProperty('mockObjects')
);

// remove all mock objects inherited from parent scope(s)
$mockObjects->setValue([]);
}

return $properties;
}

private function specifyCheckMockObjects()
{
if (($phpUnitReflection = $this->specifyGetPhpUnitReflection()) !== null) {
$verifyMockObjects = $phpUnitReflection->getMethod('verifyMockObjects');
$verifyMockObjects->setAccessible(true);
$verifyMockObjects->invoke($this);
}
}

/**
* @return \ReflectionClass|null
*/
private function specifyGetPhpUnitReflection()
{
if ($this instanceof \PHPUnit_Framework_TestCase) {
return new \ReflectionClass('\PHPUnit_Framework_TestCase');
}
}
}
78 changes: 0 additions & 78 deletions src/Codeception/Specify/ObjectProperty.php

This file was deleted.

66 changes: 0 additions & 66 deletions tests/ObjectPropertyTest.php

This file was deleted.

18 changes: 0 additions & 18 deletions tests/SpecifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,6 @@ public function testExamplesIndexInName()
});
}

public function testMockObjectsIsolation()
{
$mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
$mock->expects($this->once())->method('testMockObjectsIsolation');

$this->specify('this should fail', function () {
$mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
$mock->expects($this->exactly(100500))->method('testMockObjectsIsolation');
}, ['throws' => 'PHPUnit_Framework_ExpectationFailedException']);

$this->specify('this should not fail', function () {
$mock = $this->getMock(get_class($this), ['testMockObjectsIsolation']);
$mock->expects($this->never())->method('testMockObjectsIsolation');
});

$mock->testMockObjectsIsolation();
}

// public function testFail()
// {
// $this->specify('this will fail', function(){
Expand Down

0 comments on commit 5fb1d68

Please sign in to comment.