Skip to content

Commit

Permalink
Merge pull request #252 from fredemmott/datetime-override
Browse files Browse the repository at this point in the history
Datetime override
  • Loading branch information
davedevelopment committed Jan 19, 2014
2 parents 067918f + 16dcec7 commit 5f146b7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/Mockery/Adapter/Phpunit/TestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e,

public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) {}

public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) {}


public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) {}

Expand Down
7 changes: 7 additions & 0 deletions library/Mockery/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public function setInternalClassMethodParamMap($class, $method, array $map)
}
$this->_internalClassParamMap[strtolower($class)][strtolower($method)] = $map;
}

/**
* Remove all overriden parameter maps from internal PHP classes.
*/
public function resetInternalClassMethodParamMaps() {
$this->_internalClassParamMap = array();
}

/**
* Get the parameter map of an internal PHP class method
Expand Down
57 changes: 57 additions & 0 deletions tests/Mockery/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,37 @@ public function testMethodParamsPassedByReferenceHaveReferencePreserved()
$this->assertEquals(1, $b);
}

/**
* Meant to test the same logic as
* testCanOverrideExpectedParametersOfExtensionPHPClassesToPreserveRefs,
* but:
* - doesn't require an extension
* - isn't actually known to be used
*/
public function testCanOverrideExpectedParametersOfInternalPHPClassesToPreserveRefs()
{
\Mockery::getConfiguration()->setInternalClassMethodParamMap(
'DateTime', 'modify', array('&$string')
);
// @ used to avoid E_STRICT for incompatible signature
@$m = $this->container->mock('DateTime');
$this->assertInstanceOf("Mockery\MockInterface", $m, "Mocking failed, remove @ error suppresion to debug");
$m->shouldReceive('modify')->with(
\Mockery::on(function(&$string) {$string = 'foo'; return true;})
);
$data ='bar';
$m->modify($data);
$this->assertEquals('foo', $data);
$this->container->mockery_verify();
\Mockery::resetContainer();
\Mockery::getConfiguration()->resetInternalClassMethodParamMaps();
}

/**
* Real world version of
* testCanOverrideExpectedParametersOfInternalPHPClassesToPreserveRefs
*/
public function testCanOverrideExpectedParametersOfExtensionPHPClassesToPreserveRefs()
{
if (!class_exists('MongoCollection', false)) $this->markTestSkipped('ext/mongo not installed');
\Mockery::getConfiguration()->setInternalClassMethodParamMap(
Expand All @@ -705,6 +735,33 @@ public function testCanOverrideExpectedParametersOfInternalPHPClassesToPreserveR
$this->assertEquals(123, $data['_id']);
$this->container->mockery_verify();
\Mockery::resetContainer();
\Mockery::getConfiguration()->resetInternalClassMethodParamMaps();
}

public function testCanCreateNonOverridenInstanceOfPreviouslyOverridenInternalClasses()
{
\Mockery::getConfiguration()->setInternalClassMethodParamMap(
'DateTime', 'modify', array('&$string')
);
// @ used to avoid E_STRICT for incompatible signature
@$m = $this->container->mock('DateTime');
$this->assertInstanceOf("Mockery\MockInterface", $m, "Mocking failed, remove @ error suppresion to debug");
$rc = new ReflectionClass($m);
$rm = $rc->getMethod('modify');
$params = $rm->getParameters();
$this->assertTrue($params[0]->isPassedByReference());

\Mockery::getConfiguration()->resetInternalClassMethodParamMaps();

$m = $this->container->mock('DateTime');
$this->assertInstanceOf("Mockery\MockInterface", $m, "Mocking failed");
$rc = new ReflectionClass($m);
$rm = $rc->getMethod('modify');
$params = $rm->getParameters();
$this->assertFalse($params[0]->isPassedByReference());

\Mockery::resetContainer();
\Mockery::getConfiguration()->resetInternalClassMethodParamMaps();
}

/**
Expand Down

0 comments on commit 5f146b7

Please sign in to comment.