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