Skip to content

Commit

Permalink
Lazily Create Mock Object Generator
Browse files Browse the repository at this point in the history
Fixes sebastianbergmann#1164

Instead of setting up the mock object generator in TestCase's
constructor, it's created whenever a call to `getMockObjectGenerator` is
made.
  • Loading branch information
chrisguitarguy committed Mar 7, 2014
1 parent dcd869a commit 92f463d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert imple
/**
* @var array
*/
private $mockObjectGenerator;
private $mockObjectGenerator = null;

/**
* @var integer
Expand Down Expand Up @@ -313,7 +313,6 @@ public function __construct($name = null, array $data = array(), $dataName = '')

$this->data = $data;
$this->dataName = $dataName;
$this->mockObjectGenerator = new PHPUnit_Framework_MockObject_Generator;
}

/**
Expand Down Expand Up @@ -1298,7 +1297,7 @@ protected function setLocale()
*/
public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false)
{
$mockObject = $this->mockObjectGenerator->getMock(
$mockObject = $this->getMockObjectGenerator()->getMock(
$originalClassName,
$methods,
$arguments,
Expand Down Expand Up @@ -1379,7 +1378,7 @@ protected function getMockClass($originalClassName, $methods = array(), array $a
*/
public function getMockForAbstractClass($originalClassName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = array(), $cloneArguments = false)
{
$mockObject = $this->mockObjectGenerator->getMockForAbstractClass(
$mockObject = $this->getMockObjectGenerator()->getMockForAbstractClass(
$originalClassName,
$arguments,
$mockClassName,
Expand Down Expand Up @@ -1417,7 +1416,7 @@ protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClas

if (!class_exists($originalClassName)) {
eval(
$this->mockObjectGenerator->generateClassFromWsdl(
$this->getMockObjectGenerator()->generateClassFromWsdl(
$wsdlFile, $originalClassName, $methods, $options
)
);
Expand Down Expand Up @@ -1453,7 +1452,7 @@ protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClas
*/
public function getMockForTrait($traitName, array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = array(), $cloneArguments = false)
{
$mockObject = $this->mockObjectGenerator->getMockForTrait(
$mockObject = $this->getMockObjectGenerator()->getMockForTrait(
$traitName,
$arguments,
$mockClassName,
Expand Down Expand Up @@ -1485,7 +1484,7 @@ public function getMockForTrait($traitName, array $arguments = array(), $mockCla
*/
protected function getObjectForTrait($traitName, array $arguments = array(), $traitClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = false)
{
return $this->mockObjectGenerator->getObjectForTrait(
return $this->getMockObjectGenerator()->getObjectForTrait(
$traitName,
$arguments,
$traitClassName,
Expand Down Expand Up @@ -1920,4 +1919,18 @@ public function hookAfterMethod($name)
{
$this->afterMethods[] = $name;
}

/**
* Get the mock object generator, creating it if it doesn't exist.
*
* @return PHPUnit_Framework_MockObject_Generator
*/
protected function getMockObjectGenerator()
{
if (null === $this->mockObjectGenerator) {
$this->mockObjectGenerator = new PHPUnit_Framework_MockObject_Generator;
}

return $this->mockObjectGenerator;
}
}

0 comments on commit 92f463d

Please sign in to comment.