comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Matcher/Parameters.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Invocation matcher which looks for specific parameters in the invocations.
13 *
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().
17 *
18 * @since Class available since Release 1.0.0
19 */
20 class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
21 {
22 /**
23 * @var PHPUnit_Framework_Constraint[]
24 */
25 protected $parameters = array();
26
27 /**
28 * @var PHPUnit_Framework_MockObject_Invocation
29 */
30 protected $invocation;
31
32 /**
33 * @param array $parameters
34 */
35 public function __construct(array $parameters)
36 {
37 foreach ($parameters as $parameter) {
38 if (!($parameter instanceof PHPUnit_Framework_Constraint)) {
39 $parameter = new PHPUnit_Framework_Constraint_IsEqual(
40 $parameter
41 );
42 }
43
44 $this->parameters[] = $parameter;
45 }
46 }
47
48 /**
49 * @return string
50 */
51 public function toString()
52 {
53 $text = 'with parameter';
54
55 foreach ($this->parameters as $index => $parameter) {
56 if ($index > 0) {
57 $text .= ' and';
58 }
59
60 $text .= ' ' . $index . ' ' . $parameter->toString();
61 }
62
63 return $text;
64 }
65
66 /**
67 * @param PHPUnit_Framework_MockObject_Invocation $invocation
68 * @return bool
69 */
70 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
71 {
72 $this->invocation = $invocation;
73
74 return $this->verify();
75 }
76
77 /**
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.
81 *
82 * @param PHPUnit_Framework_MockObject_Invocation $invocation
83 * Object containing information on a mocked or stubbed method which
84 * was invoked.
85 * @return bool
86 * @throws PHPUnit_Framework_ExpectationFailedException
87 */
88 public function verify()
89 {
90 if ($this->invocation === null) {
91 throw new PHPUnit_Framework_ExpectationFailedException(
92 'Mocked method does not exist.'
93 );
94 }
95
96 if (count($this->invocation->parameters) < count($this->parameters)) {
97 $message = 'Parameter count for invocation %s is too low.';
98
99 // The user called `->with($this->anything())`, but may have meant
100 // `->withAnyParameters()`.
101 //
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.";
106 }
107
108 throw new PHPUnit_Framework_ExpectationFailedException(
109 sprintf($message, $this->invocation->toString())
110 );
111 }
112
113 foreach ($this->parameters as $i => $parameter) {
114 $parameter->evaluate(
115 $this->invocation->parameters[$i],
116 sprintf(
117 'Parameter %s for invocation %s does not match expected ' .
118 'value.',
119 $i,
120 $this->invocation->toString()
121 )
122 );
123 }
124
125 return true;
126 }
127 }