Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * This file is part of the PHPUnit_MockObject package.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Invocation matcher which looks for specific parameters in the invocations.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Checks the parameters of all incoming invocations, the parameter list is
|
Chris@0
|
15 * checked against the defined constraints in $parameters. If the constraint
|
Chris@0
|
16 * is met it will return true in matches().
|
Chris@0
|
17 *
|
Chris@0
|
18 * @since Class available since Release 1.0.0
|
Chris@0
|
19 */
|
Chris@0
|
20 class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
|
Chris@0
|
21 {
|
Chris@0
|
22 /**
|
Chris@0
|
23 * @var PHPUnit_Framework_Constraint[]
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $parameters = array();
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * @var PHPUnit_Framework_MockObject_Invocation
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $invocation;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * @param array $parameters
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct(array $parameters)
|
Chris@0
|
36 {
|
Chris@0
|
37 foreach ($parameters as $parameter) {
|
Chris@0
|
38 if (!($parameter instanceof PHPUnit_Framework_Constraint)) {
|
Chris@0
|
39 $parameter = new PHPUnit_Framework_Constraint_IsEqual(
|
Chris@0
|
40 $parameter
|
Chris@0
|
41 );
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 $this->parameters[] = $parameter;
|
Chris@0
|
45 }
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * @return string
|
Chris@0
|
50 */
|
Chris@0
|
51 public function toString()
|
Chris@0
|
52 {
|
Chris@0
|
53 $text = 'with parameter';
|
Chris@0
|
54
|
Chris@0
|
55 foreach ($this->parameters as $index => $parameter) {
|
Chris@0
|
56 if ($index > 0) {
|
Chris@0
|
57 $text .= ' and';
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 $text .= ' ' . $index . ' ' . $parameter->toString();
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 return $text;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * @param PHPUnit_Framework_MockObject_Invocation $invocation
|
Chris@0
|
68 * @return bool
|
Chris@0
|
69 */
|
Chris@0
|
70 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
Chris@0
|
71 {
|
Chris@0
|
72 $this->invocation = $invocation;
|
Chris@0
|
73
|
Chris@0
|
74 return $this->verify();
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Checks if the invocation $invocation matches the current rules. If it
|
Chris@0
|
79 * does the matcher will get the invoked() method called which should check
|
Chris@0
|
80 * if an expectation is met.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param PHPUnit_Framework_MockObject_Invocation $invocation
|
Chris@0
|
83 * Object containing information on a mocked or stubbed method which
|
Chris@0
|
84 * was invoked.
|
Chris@0
|
85 * @return bool
|
Chris@0
|
86 * @throws PHPUnit_Framework_ExpectationFailedException
|
Chris@0
|
87 */
|
Chris@0
|
88 public function verify()
|
Chris@0
|
89 {
|
Chris@0
|
90 if ($this->invocation === null) {
|
Chris@0
|
91 throw new PHPUnit_Framework_ExpectationFailedException(
|
Chris@0
|
92 'Mocked method does not exist.'
|
Chris@0
|
93 );
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 if (count($this->invocation->parameters) < count($this->parameters)) {
|
Chris@0
|
97 $message = 'Parameter count for invocation %s is too low.';
|
Chris@0
|
98
|
Chris@0
|
99 // The user called `->with($this->anything())`, but may have meant
|
Chris@0
|
100 // `->withAnyParameters()`.
|
Chris@0
|
101 //
|
Chris@0
|
102 // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
|
Chris@0
|
103 if (count($this->parameters) === 1 &&
|
Chris@0
|
104 get_class($this->parameters[0]) === 'PHPUnit_Framework_Constraint_IsAnything') {
|
Chris@0
|
105 $message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 throw new PHPUnit_Framework_ExpectationFailedException(
|
Chris@0
|
109 sprintf($message, $this->invocation->toString())
|
Chris@0
|
110 );
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 foreach ($this->parameters as $i => $parameter) {
|
Chris@0
|
114 $parameter->evaluate(
|
Chris@0
|
115 $this->invocation->parameters[$i],
|
Chris@0
|
116 sprintf(
|
Chris@0
|
117 'Parameter %s for invocation %s does not match expected ' .
|
Chris@0
|
118 'value.',
|
Chris@0
|
119 $i,
|
Chris@0
|
120 $this->invocation->toString()
|
Chris@0
|
121 )
|
Chris@0
|
122 );
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 return true;
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|