Version 1
[yaffs-website] / vendor / phpunit / phpunit-mock-objects / src / Framework / MockObject / Matcher.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  * Main matcher which defines a full expectation using method, parameter and
13  * invocation matchers.
14  * This matcher encapsulates all the other matchers and allows the builder to
15  * set the specific matchers when the appropriate methods are called (once(),
16  * where() etc.).
17  *
18  * All properties are public so that they can easily be accessed by the builder.
19  *
20  * @since Class available since Release 1.0.0
21  */
22 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
23 {
24     /**
25      * @var PHPUnit_Framework_MockObject_Matcher_Invocation
26      */
27     public $invocationMatcher;
28
29     /**
30      * @var mixed
31      */
32     public $afterMatchBuilderId = null;
33
34     /**
35      * @var bool
36      */
37     public $afterMatchBuilderIsInvoked = false;
38
39     /**
40      * @var PHPUnit_Framework_MockObject_Matcher_MethodName
41      */
42     public $methodNameMatcher = null;
43
44     /**
45      * @var PHPUnit_Framework_MockObject_Matcher_Parameters
46      */
47     public $parametersMatcher = null;
48
49     /**
50      * @var PHPUnit_Framework_MockObject_Stub
51      */
52     public $stub = null;
53
54     /**
55      * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
56      */
57     public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
58     {
59         $this->invocationMatcher = $invocationMatcher;
60     }
61
62     /**
63      * @return string
64      */
65     public function toString()
66     {
67         $list = array();
68
69         if ($this->invocationMatcher !== null) {
70             $list[] = $this->invocationMatcher->toString();
71         }
72
73         if ($this->methodNameMatcher !== null) {
74             $list[] = 'where ' . $this->methodNameMatcher->toString();
75         }
76
77         if ($this->parametersMatcher !== null) {
78             $list[] = 'and ' . $this->parametersMatcher->toString();
79         }
80
81         if ($this->afterMatchBuilderId !== null) {
82             $list[] = 'after ' . $this->afterMatchBuilderId;
83         }
84
85         if ($this->stub !== null) {
86             $list[] = 'will ' . $this->stub->toString();
87         }
88
89         return implode(' ', $list);
90     }
91
92     /**
93      * @param  PHPUnit_Framework_MockObject_Invocation $invocation
94      * @return mixed
95      */
96     public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
97     {
98         if ($this->invocationMatcher === null) {
99             throw new PHPUnit_Framework_Exception(
100                 'No invocation matcher is set'
101             );
102         }
103
104         if ($this->methodNameMatcher === null) {
105             throw new PHPUnit_Framework_Exception('No method matcher is set');
106         }
107
108         if ($this->afterMatchBuilderId !== null) {
109             $builder = $invocation->object
110                                   ->__phpunit_getInvocationMocker()
111                                   ->lookupId($this->afterMatchBuilderId);
112
113             if (!$builder) {
114                 throw new PHPUnit_Framework_Exception(
115                     sprintf(
116                         'No builder found for match builder identification <%s>',
117                         $this->afterMatchBuilderId
118                     )
119                 );
120             }
121
122             $matcher = $builder->getMatcher();
123
124             if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
125                 $this->afterMatchBuilderIsInvoked = true;
126             }
127         }
128
129         $this->invocationMatcher->invoked($invocation);
130
131         try {
132             if ($this->parametersMatcher !== null &&
133                 !$this->parametersMatcher->matches($invocation)) {
134                 $this->parametersMatcher->verify();
135             }
136         } catch (PHPUnit_Framework_ExpectationFailedException $e) {
137             throw new PHPUnit_Framework_ExpectationFailedException(
138                 sprintf(
139                     "Expectation failed for %s when %s\n%s",
140                     $this->methodNameMatcher->toString(),
141                     $this->invocationMatcher->toString(),
142                     $e->getMessage()
143                 ),
144                 $e->getComparisonFailure()
145             );
146         }
147
148         if ($this->stub) {
149             return $this->stub->invoke($invocation);
150         }
151
152         return;
153     }
154
155     /**
156      * @param  PHPUnit_Framework_MockObject_Invocation $invocation
157      * @return bool
158      */
159     public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
160     {
161         if ($this->afterMatchBuilderId !== null) {
162             $builder = $invocation->object
163                                   ->__phpunit_getInvocationMocker()
164                                   ->lookupId($this->afterMatchBuilderId);
165
166             if (!$builder) {
167                 throw new PHPUnit_Framework_Exception(
168                     sprintf(
169                         'No builder found for match builder identification <%s>',
170                         $this->afterMatchBuilderId
171                     )
172                 );
173             }
174
175             $matcher = $builder->getMatcher();
176
177             if (!$matcher) {
178                 return false;
179             }
180
181             if (!$matcher->invocationMatcher->hasBeenInvoked()) {
182                 return false;
183             }
184         }
185
186         if ($this->invocationMatcher === null) {
187             throw new PHPUnit_Framework_Exception(
188                 'No invocation matcher is set'
189             );
190         }
191
192         if ($this->methodNameMatcher === null) {
193             throw new PHPUnit_Framework_Exception('No method matcher is set');
194         }
195
196         if (!$this->invocationMatcher->matches($invocation)) {
197             return false;
198         }
199
200         try {
201             if (!$this->methodNameMatcher->matches($invocation)) {
202                 return false;
203             }
204         } catch (PHPUnit_Framework_ExpectationFailedException $e) {
205             throw new PHPUnit_Framework_ExpectationFailedException(
206                 sprintf(
207                     "Expectation failed for %s when %s\n%s",
208                     $this->methodNameMatcher->toString(),
209                     $this->invocationMatcher->toString(),
210                     $e->getMessage()
211                 ),
212                 $e->getComparisonFailure()
213             );
214         }
215
216         return true;
217     }
218
219     /**
220      * @throws PHPUnit_Framework_Exception
221      * @throws PHPUnit_Framework_ExpectationFailedException
222      */
223     public function verify()
224     {
225         if ($this->invocationMatcher === null) {
226             throw new PHPUnit_Framework_Exception(
227                 'No invocation matcher is set'
228             );
229         }
230
231         if ($this->methodNameMatcher === null) {
232             throw new PHPUnit_Framework_Exception('No method matcher is set');
233         }
234
235         try {
236             $this->invocationMatcher->verify();
237
238             if ($this->parametersMatcher === null) {
239                 $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
240             }
241
242             $invocationIsAny   = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount';
243             $invocationIsNever = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_InvokedCount' && $this->invocationMatcher->isNever();
244             if (!$invocationIsAny && !$invocationIsNever) {
245                 $this->parametersMatcher->verify();
246             }
247         } catch (PHPUnit_Framework_ExpectationFailedException $e) {
248             throw new PHPUnit_Framework_ExpectationFailedException(
249                 sprintf(
250                     "Expectation failed for %s when %s.\n%s",
251                     $this->methodNameMatcher->toString(),
252                     $this->invocationMatcher->toString(),
253                     PHPUnit_Framework_TestFailure::exceptionToString($e)
254                 )
255             );
256         }
257     }
258
259     /**
260      * @since Method available since Release 1.2.4
261      */
262     public function hasMatchers()
263     {
264         if ($this->invocationMatcher !== null &&
265             !$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) {
266             return true;
267         }
268
269         return false;
270     }
271 }