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: * Invocation matcher which looks for specific parameters in the invocations. Chris@0: * Chris@0: * Checks the parameters of all incoming invocations, the parameter list is Chris@0: * checked against the defined constraints in $parameters. If the constraint Chris@0: * is met it will return true in matches(). Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation Chris@0: { Chris@0: /** Chris@0: * @var PHPUnit_Framework_Constraint[] Chris@0: */ Chris@0: protected $parameters = array(); Chris@0: Chris@0: /** Chris@0: * @var PHPUnit_Framework_MockObject_Invocation Chris@0: */ Chris@0: protected $invocation; Chris@0: Chris@0: /** Chris@0: * @param array $parameters Chris@0: */ Chris@0: public function __construct(array $parameters) Chris@0: { Chris@0: foreach ($parameters as $parameter) { Chris@0: if (!($parameter instanceof PHPUnit_Framework_Constraint)) { Chris@0: $parameter = new PHPUnit_Framework_Constraint_IsEqual( Chris@0: $parameter Chris@0: ); Chris@0: } Chris@0: Chris@0: $this->parameters[] = $parameter; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function toString() Chris@0: { Chris@0: $text = 'with parameter'; Chris@0: Chris@0: foreach ($this->parameters as $index => $parameter) { Chris@0: if ($index > 0) { Chris@0: $text .= ' and'; Chris@0: } Chris@0: Chris@0: $text .= ' ' . $index . ' ' . $parameter->toString(); Chris@0: } Chris@0: Chris@0: return $text; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @return bool Chris@0: */ Chris@0: public function matches(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: $this->invocation = $invocation; Chris@0: Chris@0: return $this->verify(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the invocation $invocation matches the current rules. If it Chris@0: * does the matcher will get the invoked() method called which should check Chris@0: * if an expectation is met. Chris@0: * Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * Object containing information on a mocked or stubbed method which Chris@0: * was invoked. Chris@0: * @return bool Chris@0: * @throws PHPUnit_Framework_ExpectationFailedException Chris@0: */ Chris@0: public function verify() Chris@0: { Chris@0: if ($this->invocation === null) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: 'Mocked method does not exist.' Chris@0: ); Chris@0: } Chris@0: Chris@0: if (count($this->invocation->parameters) < count($this->parameters)) { Chris@0: $message = 'Parameter count for invocation %s is too low.'; Chris@0: Chris@0: // The user called `->with($this->anything())`, but may have meant Chris@0: // `->withAnyParameters()`. Chris@0: // Chris@0: // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199 Chris@0: if (count($this->parameters) === 1 && Chris@0: get_class($this->parameters[0]) === 'PHPUnit_Framework_Constraint_IsAnything') { Chris@0: $message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead."; Chris@0: } Chris@0: Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf($message, $this->invocation->toString()) Chris@0: ); Chris@0: } Chris@0: Chris@0: foreach ($this->parameters as $i => $parameter) { Chris@0: $parameter->evaluate( Chris@0: $this->invocation->parameters[$i], Chris@0: sprintf( Chris@0: 'Parameter %s for invocation %s does not match expected ' . Chris@0: 'value.', Chris@0: $i, Chris@0: $this->invocation->toString() Chris@0: ) Chris@0: ); Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: }