Skip to content

Instantly share code, notes, and snippets.

@makasim
Created October 1, 2012 08:46
Show Gist options
  • Save makasim/3810381 to your computer and use it in GitHub Desktop.
Save makasim/3810381 to your computer and use it in GitHub Desktop.
stub helper using trait
<?php
namespace Rj\CoreBundle\Test\Phpunit\Helper;
/**
* Usage:
*
* ```php
* <?php
* $fooStub = $this->getStub('FooClass', array(
* 'barMethod' => $stub
* ));
* ```
*
* More info you could in issue: {@link https://github.com/sebastianbergmann/phpunit/issues/550}
*
* @author Kotlyar Maksim <[email protected]>
* @since 10/1/12
*/
trait Stub
{
public function getStub($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
{
$mock = $this->getMock(
$originalClassName,
array_keys($methods),
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload
);
foreach ($methods as $name => $stub) {
if (false == $stub instanceof \PHPUnit_Framework_MockObject_Stub) {
$stub = $this->returnValue($stub);
}
$mock
->expects($this->any())
->method($name)
->will($stub)
;
}
return $mock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment