comparison vendor/phpunit/phpunit-mock-objects/src/Matcher/ConsecutiveParameters.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\IsEqual;
14 use PHPUnit\Framework\ExpectationFailedException;
15 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
16
17 /**
18 * Invocation matcher which looks for sets of specific parameters in the invocations.
19 *
20 * Checks the parameters of the incoming invocations, the parameter list is
21 * checked against the defined constraints in $parameters. If the constraint
22 * is met it will return true in matches().
23 *
24 * It takes a list of match groups and and increases a call index after each invocation.
25 * So the first invocation uses the first group of constraints, the second the next and so on.
26 */
27 class ConsecutiveParameters extends StatelessInvocation
28 {
29 /**
30 * @var array
31 */
32 private $parameterGroups = [];
33
34 /**
35 * @var array
36 */
37 private $invocations = [];
38
39 /**
40 * @param array $parameterGroups
41 *
42 * @throws \PHPUnit\Framework\Exception
43 */
44 public function __construct(array $parameterGroups)
45 {
46 foreach ($parameterGroups as $index => $parameters) {
47 foreach ($parameters as $parameter) {
48 if (!$parameter instanceof Constraint) {
49 $parameter = new IsEqual($parameter);
50 }
51
52 $this->parameterGroups[$index][] = $parameter;
53 }
54 }
55 }
56
57 /**
58 * @return string
59 */
60 public function toString()
61 {
62 return 'with consecutive parameters';
63 }
64
65 /**
66 * @param BaseInvocation $invocation
67 *
68 * @return bool
69 *
70 * @throws \PHPUnit\Framework\ExpectationFailedException
71 */
72 public function matches(BaseInvocation $invocation)
73 {
74 $this->invocations[] = $invocation;
75 $callIndex = \count($this->invocations) - 1;
76
77 $this->verifyInvocation($invocation, $callIndex);
78
79 return false;
80 }
81
82 public function verify()
83 {
84 foreach ($this->invocations as $callIndex => $invocation) {
85 $this->verifyInvocation($invocation, $callIndex);
86 }
87 }
88
89 /**
90 * Verify a single invocation
91 *
92 * @param BaseInvocation $invocation
93 * @param int $callIndex
94 *
95 * @throws ExpectationFailedException
96 */
97 private function verifyInvocation(BaseInvocation $invocation, $callIndex)
98 {
99 if (isset($this->parameterGroups[$callIndex])) {
100 $parameters = $this->parameterGroups[$callIndex];
101 } else {
102 // no parameter assertion for this call index
103 return;
104 }
105
106 if ($invocation === null) {
107 throw new ExpectationFailedException(
108 'Mocked method does not exist.'
109 );
110 }
111
112 if (\count($invocation->getParameters()) < \count($parameters)) {
113 throw new ExpectationFailedException(
114 \sprintf(
115 'Parameter count for invocation %s is too low.',
116 $invocation->toString()
117 )
118 );
119 }
120
121 foreach ($parameters as $i => $parameter) {
122 $parameter->evaluate(
123 $invocation->getParameters()[$i],
124 \sprintf(
125 'Parameter %s for invocation #%d %s does not match expected ' .
126 'value.',
127 $i,
128 $callIndex,
129 $invocation->toString()
130 )
131 );
132 }
133 }
134 }