comparison vendor/phpunit/phpunit-mock-objects/src/Matcher/Parameters.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2 /*
3 * This file is part of the phpunit-mock-objects 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 namespace PHPUnit\Framework\MockObject\Matcher;
11
12 use PHPUnit\Framework\Constraint\Constraint;
13 use PHPUnit\Framework\Constraint\IsAnything;
14 use PHPUnit\Framework\Constraint\IsEqual;
15 use PHPUnit\Framework\ExpectationFailedException;
16 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
17
18 /**
19 * Invocation matcher which looks for specific parameters in the invocations.
20 *
21 * Checks the parameters of all incoming invocations, the parameter list is
22 * checked against the defined constraints in $parameters. If the constraint
23 * is met it will return true in matches().
24 */
25 class Parameters extends StatelessInvocation
26 {
27 /**
28 * @var Constraint[]
29 */
30 private $parameters = [];
31
32 /**
33 * @var BaseInvocation
34 */
35 private $invocation;
36
37 /**
38 * @var ExpectationFailedException
39 */
40 private $parameterVerificationResult;
41
42 /**
43 * @param array $parameters
44 *
45 * @throws \PHPUnit\Framework\Exception
46 */
47 public function __construct(array $parameters)
48 {
49 foreach ($parameters as $parameter) {
50 if (!($parameter instanceof Constraint)) {
51 $parameter = new IsEqual(
52 $parameter
53 );
54 }
55
56 $this->parameters[] = $parameter;
57 }
58 }
59
60 /**
61 * @return string
62 */
63 public function toString()
64 {
65 $text = 'with parameter';
66
67 foreach ($this->parameters as $index => $parameter) {
68 if ($index > 0) {
69 $text .= ' and';
70 }
71
72 $text .= ' ' . $index . ' ' . $parameter->toString();
73 }
74
75 return $text;
76 }
77
78 /**
79 * @param BaseInvocation $invocation
80 *
81 * @return bool
82 *
83 * @throws \Exception
84 */
85 public function matches(BaseInvocation $invocation)
86 {
87 $this->invocation = $invocation;
88 $this->parameterVerificationResult = null;
89
90 try {
91 $this->parameterVerificationResult = $this->verify();
92
93 return $this->parameterVerificationResult;
94 } catch (ExpectationFailedException $e) {
95 $this->parameterVerificationResult = $e;
96
97 throw $this->parameterVerificationResult;
98 }
99 }
100
101 /**
102 * Checks if the invocation $invocation matches the current rules. If it
103 * does the matcher will get the invoked() method called which should check
104 * if an expectation is met.
105 *
106 * @return bool
107 *
108 * @throws ExpectationFailedException
109 */
110 public function verify()
111 {
112 if (isset($this->parameterVerificationResult)) {
113 return $this->guardAgainstDuplicateEvaluationOfParameterConstraints();
114 }
115
116 if ($this->invocation === null) {
117 throw new ExpectationFailedException('Mocked method does not exist.');
118 }
119
120 if (\count($this->invocation->getParameters()) < \count($this->parameters)) {
121 $message = 'Parameter count for invocation %s is too low.';
122
123 // The user called `->with($this->anything())`, but may have meant
124 // `->withAnyParameters()`.
125 //
126 // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
127 if (\count($this->parameters) === 1 &&
128 \get_class($this->parameters[0]) === IsAnything::class) {
129 $message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
130 }
131
132 throw new ExpectationFailedException(
133 \sprintf($message, $this->invocation->toString())
134 );
135 }
136
137 foreach ($this->parameters as $i => $parameter) {
138 $parameter->evaluate(
139 $this->invocation->getParameters()[$i],
140 \sprintf(
141 'Parameter %s for invocation %s does not match expected ' .
142 'value.',
143 $i,
144 $this->invocation->toString()
145 )
146 );
147 }
148
149 return true;
150 }
151
152 /**
153 * @return bool
154 *
155 * @throws ExpectationFailedException
156 */
157 private function guardAgainstDuplicateEvaluationOfParameterConstraints()
158 {
159 if ($this->parameterVerificationResult instanceof \Exception) {
160 throw $this->parameterVerificationResult;
161 }
162
163 return (bool) $this->parameterVerificationResult;
164 }
165 }