Created
October 1, 2012 08:46
-
-
Save makasim/3810381 to your computer and use it in GitHub Desktop.
stub helper using trait
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
<?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