Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Implementation of the Builder pattern for Mock objects. Chris@0: * Chris@0: * @since File available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_MockBuilder Chris@0: { Chris@0: /** Chris@0: * @var PHPUnit_Framework_TestCase Chris@0: */ Chris@0: private $testCase; Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: private $type; Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $methods = array(); Chris@0: Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: private $mockClassName = ''; Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $constructorArgs = array(); Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $originalConstructor = true; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $originalClone = true; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $autoload = true; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $cloneArguments = false; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $callOriginalMethods = false; Chris@0: Chris@0: /** Chris@0: * @var object Chris@0: */ Chris@0: private $proxyTarget = null; Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_TestCase $testCase Chris@0: * @param array|string $type Chris@0: */ Chris@0: public function __construct(PHPUnit_Framework_TestCase $testCase, $type) Chris@0: { Chris@0: $this->testCase = $testCase; Chris@0: $this->type = $type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a mock object using a fluent interface. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockObject Chris@0: */ Chris@0: public function getMock() Chris@0: { Chris@0: return $this->testCase->getMock( Chris@0: $this->type, Chris@0: $this->methods, Chris@0: $this->constructorArgs, Chris@0: $this->mockClassName, Chris@0: $this->originalConstructor, Chris@0: $this->originalClone, Chris@0: $this->autoload, Chris@0: $this->cloneArguments, Chris@0: $this->callOriginalMethods, Chris@0: $this->proxyTarget Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a mock object for an abstract class using a fluent interface. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockObject Chris@0: */ Chris@0: public function getMockForAbstractClass() Chris@0: { Chris@0: return $this->testCase->getMockForAbstractClass( Chris@0: $this->type, Chris@0: $this->constructorArgs, Chris@0: $this->mockClassName, Chris@0: $this->originalConstructor, Chris@0: $this->originalClone, Chris@0: $this->autoload, Chris@0: $this->methods, Chris@0: $this->cloneArguments Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a mock object for a trait using a fluent interface. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockObject Chris@0: */ Chris@0: public function getMockForTrait() Chris@0: { Chris@0: return $this->testCase->getMockForTrait( Chris@0: $this->type, Chris@0: $this->constructorArgs, Chris@0: $this->mockClassName, Chris@0: $this->originalConstructor, Chris@0: $this->originalClone, Chris@0: $this->autoload, Chris@0: $this->methods, Chris@0: $this->cloneArguments Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Specifies the subset of methods to mock. Default is to mock all of them. Chris@0: * Chris@0: * @param array|null $methods Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function setMethods($methods) Chris@0: { Chris@0: $this->methods = $methods; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Specifies the arguments for the constructor. Chris@0: * Chris@0: * @param array $args Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function setConstructorArgs(array $args) Chris@0: { Chris@0: $this->constructorArgs = $args; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Specifies the name for the mock class. Chris@0: * Chris@0: * @param string $name Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function setMockClassName($name) Chris@0: { Chris@0: $this->mockClassName = $name; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the invocation of the original constructor. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function disableOriginalConstructor() Chris@0: { Chris@0: $this->originalConstructor = false; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables the invocation of the original constructor. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 1.2.0 Chris@0: */ Chris@0: public function enableOriginalConstructor() Chris@0: { Chris@0: $this->originalConstructor = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the invocation of the original clone constructor. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function disableOriginalClone() Chris@0: { Chris@0: $this->originalClone = false; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables the invocation of the original clone constructor. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 1.2.0 Chris@0: */ Chris@0: public function enableOriginalClone() Chris@0: { Chris@0: $this->originalClone = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the use of class autoloading while creating the mock object. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: */ Chris@0: public function disableAutoload() Chris@0: { Chris@0: $this->autoload = false; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables the use of class autoloading while creating the mock object. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 1.2.0 Chris@0: */ Chris@0: public function enableAutoload() Chris@0: { Chris@0: $this->autoload = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the cloning of arguments passed to mocked methods. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 1.2.0 Chris@0: */ Chris@0: public function disableArgumentCloning() Chris@0: { Chris@0: $this->cloneArguments = false; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables the cloning of arguments passed to mocked methods. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 1.2.0 Chris@0: */ Chris@0: public function enableArgumentCloning() Chris@0: { Chris@0: $this->cloneArguments = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables the invocation of the original methods. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 2.0.0 Chris@0: */ Chris@0: public function enableProxyingToOriginalMethods() Chris@0: { Chris@0: $this->callOriginalMethods = true; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Disables the invocation of the original methods. Chris@0: * Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 2.0.0 Chris@0: */ Chris@0: public function disableProxyingToOriginalMethods() Chris@0: { Chris@0: $this->callOriginalMethods = false; Chris@0: $this->proxyTarget = null; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the proxy target. Chris@0: * Chris@0: * @param object $object Chris@0: * @return PHPUnit_Framework_MockObject_MockBuilder Chris@0: * @since Method available since Release 2.0.0 Chris@0: */ Chris@0: public function setProxyTarget($object) Chris@0: { Chris@0: $this->proxyTarget = $object; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: }