Merge "Hoist validation errors from hidden fields to the top of the form"
[lhc/web/wiklou.git] / tests / phpunit / includes / ConsecutiveParametersMatcher.php
1 <?php
2 // @codingStandardsIgnoreFile
3 /*
4 * This file is part of the PHPUnit_MockObject package.
5 *
6 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 /**
13 * Invocation matcher which looks for sets of specific parameters in the invocations.
14 *
15 * Checks the parameters of the incoming invocations, the parameter list is
16 * checked against the defined constraints in $parameters. If the constraint
17 * is met it will return true in matches().
18 *
19 * It takes a list of match groups and and increases a call index after each invocation.
20 * So the first invocation uses the first group of constraints, the second the next and so on.
21 */
22 class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
23 {
24 /**
25 * @var array
26 */
27 private $_parameterGroups = array();
28
29 /**
30 * @var array
31 */
32 private $_invocations = array();
33
34 /**
35 * @param array $parameterGroups
36 */
37 public function __construct(array $parameterGroups)
38 {
39 foreach ($parameterGroups as $index => $parameters) {
40 foreach ($parameters as $parameter) {
41 if (!($parameter instanceof \PHPUnit_Framework_Constraint)) {
42 $parameter = new \PHPUnit_Framework_Constraint_IsEqual($parameter);
43 }
44 $this->_parameterGroups[$index][] = $parameter;
45 }
46 }
47 }
48
49 /**
50 * @return string
51 */
52 public function toString()
53 {
54 $text = 'with consecutive parameters';
55
56 return $text;
57 }
58
59 /**
60 * @param PHPUnit_Framework_MockObject_Invocation $invocation
61 * @return bool
62 */
63 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
64 {
65 $this->_invocations[] = $invocation;
66 $callIndex = count($this->_invocations) - 1;
67 $this->verifyInvocation($invocation, $callIndex);
68
69 return false;
70 }
71
72 public function verify()
73 {
74 foreach ($this->_invocations as $callIndex => $invocation) {
75 $this->verifyInvocation($invocation, $callIndex);
76 }
77 }
78
79 /**
80 * Verify a single invocation
81 *
82 * @param PHPUnit_Framework_MockObject_Invocation $invocation
83 * @param int $callIndex
84 * @throws PHPUnit_Framework_ExpectationFailedException
85 */
86 private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex)
87 {
88
89 if (isset($this->_parameterGroups[$callIndex])) {
90 $parameters = $this->_parameterGroups[$callIndex];
91 } else {
92 // no parameter assertion for this call index
93 return;
94 }
95
96 if ($invocation === null) {
97 throw new PHPUnit_Framework_ExpectationFailedException(
98 'Mocked method does not exist.'
99 );
100 }
101
102 if (count($invocation->parameters) < count($parameters)) {
103 throw new PHPUnit_Framework_ExpectationFailedException(
104 sprintf(
105 'Parameter count for invocation %s is too low.',
106 $invocation->toString()
107 )
108 );
109 }
110
111 foreach ($parameters as $i => $parameter) {
112 $parameter->evaluate(
113 $invocation->parameters[$i],
114 sprintf(
115 'Parameter %s for invocation #%d %s does not match expected ' .
116 'value.',
117 $i,
118 $callIndex,
119 $invocation->toString()
120 )
121 );
122 }
123 }
124 }