]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Framework/MockObject/Matcher.php
Release 6.2.1
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Framework / MockObject / Matcher.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2010-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package    PHPUnit_MockObject
38  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
39  * @copyright  2010-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
41  * @link       http://github.com/sebastianbergmann/phpunit-mock-objects
42  * @since      File available since Release 1.0.0
43  */
44
45 /**
46  * Main matcher which defines a full expectation using method, parameter and 
47  * invocation matchers.
48  * This matcher encapsulates all the other matchers and allows the builder to
49  * set the specific matchers when the appropriate methods are called (once(),
50  * where() etc.).
51  *
52  * All properties are public so that they can easily be accessed by the builder.
53  *
54  * @package    PHPUnit_MockObject
55  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
56  * @copyright  2010-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58  * @version    Release: 1.0.9
59  * @link       http://github.com/sebastianbergmann/phpunit-mock-objects
60  * @since      Class available since Release 1.0.0
61  */
62 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
63 {
64     /**
65      * @var PHPUnit_Framework_MockObject_Matcher_Invocation
66      */
67     public $invocationMatcher;
68
69     /**
70      * @var mixed
71      */
72     public $afterMatchBuilderId = NULL;
73
74     /**
75      * @var boolean
76      */
77     public $afterMatchBuilderIsInvoked = FALSE;
78
79     /**
80      * @var PHPUnit_Framework_MockObject_Matcher_MethodName
81      */
82     public $methodNameMatcher = NULL;
83
84     /**
85      * @var PHPUnit_Framework_MockObject_Matcher_Parameters
86      */
87     public $parametersMatcher = NULL;
88
89     /**
90      * @var PHPUnit_Framework_MockObject_Stub
91      */
92     public $stub = NULL;
93
94     /**
95      * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
96      */
97     public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
98     {
99         $this->invocationMatcher = $invocationMatcher;
100     }
101
102     /**
103      * @return string
104      */
105     public function toString()
106     {
107         $list = array();
108
109         if ($this->invocationMatcher !== NULL) {
110             $list[] = $this->invocationMatcher->toString();
111         }
112
113         if ($this->methodNameMatcher !== NULL) {
114             $list[] = 'where ' . $this->methodNameMatcher->toString();
115         }
116
117         if ($this->parametersMatcher !== NULL) {
118             $list[] = 'and ' . $this->parametersMatcher->toString();
119         }
120
121         if ($this->afterMatchBuilderId !== NULL) {
122             $list[] = 'after ' . $this->afterMatchBuilderId;
123         }
124
125         if ($this->stub !== NULL) {
126             $list[] = 'will ' . $this->stub->toString();
127         }
128
129         return join(' ', $list);
130     }
131
132     /**
133      * @param  PHPUnit_Framework_MockObject_Invocation $invocation
134      * @return mixed
135      */
136     public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
137     {
138         if ($this->invocationMatcher === NULL) {
139             throw new PHPUnit_Framework_Exception(
140               'No invocation matcher is set'
141             );
142         }
143
144         if ($this->methodNameMatcher === NULL) {
145             throw new PHPUnit_Framework_Exception('No method matcher is set');
146         }
147
148         if ($this->afterMatchBuilderId !== NULL) {
149             $builder = $invocation->object
150                                   ->__phpunit_getInvocationMocker()
151                                   ->lookupId($this->afterMatchBuilderId);
152
153             if (!$builder) {
154                 throw new PHPUnit_Framework_Exception(
155                   sprintf(
156                     'No builder found for match builder identification <%s>',
157
158                     $this->afterMatchBuilderId
159                   )
160                 );
161             }
162
163             $matcher = $builder->getMatcher();
164
165             if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
166                 $this->afterMatchBuilderIsInvoked = TRUE;
167             }
168         }
169
170         $this->invocationMatcher->invoked($invocation);
171
172         try {
173             if ( $this->parametersMatcher !== NULL &&
174                 !$this->parametersMatcher->matches($invocation)) {
175                 $this->parametersMatcher->verify();
176             }
177         }
178
179         catch (PHPUnit_Framework_ExpectationFailedException $e) {
180             throw new PHPUnit_Framework_ExpectationFailedException(
181               sprintf(
182                 "Expectation failed for %s when %s\n%s",
183
184                 $this->methodNameMatcher->toString(),
185                 $this->invocationMatcher->toString(),
186                 $e->getDescription()
187               ),
188               $e->getComparisonFailure()
189             );
190         }
191
192         if ($this->stub) {
193             return $this->stub->invoke($invocation);
194         }
195
196         return NULL;
197     }
198
199     /**
200      * @param  PHPUnit_Framework_MockObject_Invocation $invocation
201      * @return boolean
202      */
203     public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
204     {
205         if ($this->afterMatchBuilderId !== NULL) {
206             $builder = $invocation->object
207                                   ->__phpunit_getInvocationMocker()
208                                   ->lookupId($this->afterMatchBuilderId);
209
210             if (!$builder) {
211                 throw new PHPUnit_Framework_Exception(
212                   sprintf(
213                     'No builder found for match builder identification <%s>',
214
215                     $this->afterMatchBuilderId
216                   )
217                 );
218             }
219
220             $matcher = $builder->getMatcher();
221
222             if (!$matcher) {
223                 return FALSE;
224             }
225
226             if (!$matcher->invocationMatcher->hasBeenInvoked()) {
227                 return FALSE;
228             }
229         }
230
231         if ($this->invocationMatcher === NULL) {
232             throw new PHPUnit_Framework_Exception(
233               'No invocation matcher is set'
234             );
235         }
236
237         if ($this->methodNameMatcher === NULL) {
238             throw new PHPUnit_Framework_Exception('No method matcher is set');
239         }
240
241         if (!$this->invocationMatcher->matches($invocation)) {
242             return FALSE;
243         }
244
245         try {
246             if (!$this->methodNameMatcher->matches($invocation)) {
247                 return FALSE;
248             }
249         }
250
251         catch (PHPUnit_Framework_ExpectationFailedException $e) {
252             throw new PHPUnit_Framework_ExpectationFailedException(
253               sprintf(
254                 "Expectation failed for %s when %s\n%s",
255
256                 $this->methodNameMatcher->toString(),
257                 $this->invocationMatcher->toString(),
258                 $e->getDescription()
259               ),
260               $e->getComparisonFailure()
261             );
262         }
263
264         return TRUE;
265     }
266
267     /**
268      * @throws PHPUnit_Framework_Exception
269      * @throws PHPUnit_Framework_ExpectationFailedException
270      */
271     public function verify()
272     {
273         if ($this->invocationMatcher === NULL) {
274             throw new PHPUnit_Framework_Exception(
275               'No invocation matcher is set'
276             );
277         }
278
279         if ($this->methodNameMatcher === NULL) {
280             throw new PHPUnit_Framework_Exception('No method matcher is set');
281         }
282
283         try {
284             $this->invocationMatcher->verify();
285
286             if ($this->parametersMatcher !== NULL) {
287                 $this->parametersMatcher->verify();
288             }
289         }
290
291         catch (PHPUnit_Framework_ExpectationFailedException $e) {
292             throw new PHPUnit_Framework_ExpectationFailedException(
293               sprintf(
294                 "Expectation failed for %s when %s.\n%s",
295
296                 $this->methodNameMatcher->toString(),
297                 $this->invocationMatcher->toString(),
298                 $e->getDescription()
299               )
300             );
301         }
302     }
303 }