3 * This file is part of the PHPUnit_MockObject package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Invocation matcher which looks for specific parameters in the invocations.
14 * Checks the parameters of all incoming invocations, the parameter list is
15 * checked against the defined constraints in $parameters. If the constraint
16 * is met it will return true in matches().
18 * @since Class available since Release 1.0.0
20 class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
23 * @var PHPUnit_Framework_Constraint[]
25 protected $parameters = array();
28 * @var PHPUnit_Framework_MockObject_Invocation
30 protected $invocation;
33 * @param array $parameters
35 public function __construct(array $parameters)
37 foreach ($parameters as $parameter) {
38 if (!($parameter instanceof PHPUnit_Framework_Constraint)) {
39 $parameter = new PHPUnit_Framework_Constraint_IsEqual(
44 $this->parameters[] = $parameter;
51 public function toString()
53 $text = 'with parameter';
55 foreach ($this->parameters as $index => $parameter) {
60 $text .= ' ' . $index . ' ' . $parameter->toString();
67 * @param PHPUnit_Framework_MockObject_Invocation $invocation
70 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
72 $this->invocation = $invocation;
74 return $this->verify();
78 * Checks if the invocation $invocation matches the current rules. If it
79 * does the matcher will get the invoked() method called which should check
80 * if an expectation is met.
82 * @param PHPUnit_Framework_MockObject_Invocation $invocation
83 * Object containing information on a mocked or stubbed method which
86 * @throws PHPUnit_Framework_ExpectationFailedException
88 public function verify()
90 if ($this->invocation === null) {
91 throw new PHPUnit_Framework_ExpectationFailedException(
92 'Mocked method does not exist.'
96 if (count($this->invocation->parameters) < count($this->parameters)) {
97 $message = 'Parameter count for invocation %s is too low.';
99 // The user called `->with($this->anything())`, but may have meant
100 // `->withAnyParameters()`.
102 // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
103 if (count($this->parameters) === 1 &&
104 get_class($this->parameters[0]) === 'PHPUnit_Framework_Constraint_IsAnything') {
105 $message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
108 throw new PHPUnit_Framework_ExpectationFailedException(
109 sprintf($message, $this->invocation->toString())
113 foreach ($this->parameters as $i => $parameter) {
114 $parameter->evaluate(
115 $this->invocation->parameters[$i],
117 'Parameter %s for invocation %s does not match expected ' .
120 $this->invocation->toString()