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: * Builder for mocked or stubbed invocations. Chris@0: * Chris@0: * Provides methods for building expectations without having to resort to Chris@0: * instantiating the various matchers manually. These methods also form a Chris@0: * more natural way of reading the expectation. This class should be together Chris@0: * with the test case PHPUnit_Framework_MockObject_TestCase. Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_Builder_InvocationMocker implements PHPUnit_Framework_MockObject_Builder_MethodNameMatch Chris@0: { Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Stub_MatcherCollection Chris@0: */ Chris@0: protected $collection; Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Matcher Chris@0: */ Chris@0: protected $matcher; Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection Chris@0: * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher Chris@0: */ Chris@0: public function __construct(PHPUnit_Framework_MockObject_Stub_MatcherCollection $collection, PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher) Chris@0: { Chris@0: $this->collection = $collection; Chris@0: $this->matcher = new PHPUnit_Framework_MockObject_Matcher( Chris@0: $invocationMatcher Chris@0: ); Chris@0: Chris@0: $this->collection->addMatcher($this->matcher); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return PHPUnit_Framework_MockObject_Matcher Chris@0: */ Chris@0: public function getMatcher() Chris@0: { Chris@0: return $this->matcher; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $id Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function id($id) Chris@0: { Chris@0: $this->collection->registerId($id, $this); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Stub $stub Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function will(PHPUnit_Framework_MockObject_Stub $stub) Chris@0: { Chris@0: $this->matcher->stub = $stub; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $value Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturn($value) Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_Return( Chris@0: $value Chris@0: ); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param array $valueMap Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturnMap(array $valueMap) Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap( Chris@0: $valueMap Chris@0: ); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $argumentIndex Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturnArgument($argumentIndex) Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument( Chris@0: $argumentIndex Chris@0: ); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param callable $callback Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturnCallback($callback) Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback( Chris@0: $callback Chris@0: ); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturnSelf() Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf(); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $value, ... Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willReturnOnConsecutiveCalls() Chris@0: { Chris@0: $args = func_get_args(); Chris@0: Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param Exception $exception Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function willThrowException(Exception $exception) Chris@0: { Chris@0: $stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception); Chris@0: Chris@0: return $this->will($stub); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $id Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function after($id) Chris@0: { Chris@0: $this->matcher->afterMatchBuilderId = $id; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate that a parameters matcher can be defined, throw exceptions otherwise. Chris@0: * Chris@0: * @throws PHPUnit_Framework_Exception Chris@0: */ Chris@0: private function canDefineParameters() Chris@0: { Chris@0: if ($this->matcher->methodNameMatcher === null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'Method name matcher is not defined, cannot define parameter ' . Chris@0: ' matcher without one' Chris@0: ); Chris@0: } Chris@0: Chris@0: if ($this->matcher->parametersMatcher !== null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'Parameter matcher is already defined, cannot redefine' Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed $argument, ... Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function with() Chris@0: { Chris@0: $args = func_get_args(); Chris@0: Chris@0: $this->canDefineParameters(); Chris@0: Chris@0: $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_Parameters($args); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param mixed ...$argument Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function withConsecutive() Chris@0: { Chris@0: Chris@0: $args = func_get_args(); Chris@0: Chris@0: $this->canDefineParameters(); Chris@0: Chris@0: $this->matcher->parametersMatcher = Chris@0: new PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters($args); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function withAnyParameters() Chris@0: { Chris@0: $this->canDefineParameters(); Chris@0: Chris@0: $this->matcher->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_Constraint|string $constraint Chris@0: * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker Chris@0: */ Chris@0: public function method($constraint) Chris@0: { Chris@0: if ($this->matcher->methodNameMatcher !== null) { Chris@0: throw new PHPUnit_Framework_Exception( Chris@0: 'Method name matcher is already defined, cannot redefine' Chris@0: ); Chris@0: } Chris@0: Chris@0: $this->matcher->methodNameMatcher = new PHPUnit_Framework_MockObject_Matcher_MethodName($constraint); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: }