]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Framework/MockObject/Matcher.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Framework / MockObject / Matcher.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, 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  * @category   Testing
38  * @package    PHPUnit
39  * @author     Jan Borsodi <jb@ez.no>
40  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
42  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
43
44  * @link       http://www.phpunit.de/
45  * @since      File available since Release 3.0.0
46  */
47
48 require_once 'PHPUnit/Framework.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Framework/MockObject/Matcher/Invocation.php';
51 require_once 'PHPUnit/Framework/MockObject/Invocation.php';
52
53 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54
55 /**
56  * Main matcher which defines a full expectation using method, parameter and invocation matchers.
57  *
58  * This matcher encapsulates all the other matchers and allows the builder to set
59  * the specific matchers when the appropriate methods are called (once(), where()
60  * etc.).
61  *
62  * All properties are public so that they can easily be accessed by the builder.
63  *
64  * @category   Testing
65  * @package    PHPUnit
66  * @author     Jan Borsodi <jb@ez.no>
67  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
68  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
69  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
70  * @version    Release: 3.3.17
71  * @link       http://www.phpunit.de/
72  * @since      Class available since Release 3.0.0
73  */
74 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
75 {
76     public $invocationMatcher;
77
78     public $afterMatchBuilderId = NULL;
79
80     public $afterMatchBuilderIsInvoked = FALSE;
81
82     public $methodNameMatcher = NULL;
83
84     public $parametersMatcher = NULL;
85
86     public $stub = NULL;
87
88     public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
89     {
90         $this->invocationMatcher = $invocationMatcher;
91     }
92
93     public function toString()
94     {
95         $list = array();
96
97         if ($this->invocationMatcher !== NULL) {
98             $list[] = $this->invocationMatcher->toString();
99         }
100
101         if ($this->methodNameMatcher !== NULL) {
102             $list[] = 'where ' . $this->methodNameMatcher->toString();
103         }
104
105         if ($this->parametersMatcher !== NULL) {
106             $list[] = 'and ' . $this->parametersMatcher->toString();
107         }
108
109         if ($this->afterMatchBuilderId !== NULL) {
110             $list[] = 'after ' . $this->afterMatchBuilderId;
111         }
112
113         if ($this->stub !== NULL) {
114             $list[] = 'will ' . $this->stub->toString();
115         }
116
117         return join(' ', $list);
118     }
119
120     public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
121     {
122         if ($this->invocationMatcher === NULL) {
123             throw new RuntimeException('No invocation matcher is set');
124         }
125
126         if ($this->methodNameMatcher === NULL) {
127             throw new RuntimeException('No method matcher is set');
128         }
129
130         if ($this->afterMatchBuilderId !== NULL) {
131             $builder = $invocation->object->__phpunit_getInvocationMocker()->lookupId($this->afterMatchBuilderId);
132
133             if (!$builder) {
134                 throw new RuntimeException(
135                   sprintf(
136                     'No builder found for match builder identification <%s>',
137
138                     $this->afterMatchBuilderId
139                   )
140                 );
141             }
142
143             $matcher = $builder->getMatcher();
144
145             if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
146                 $this->afterMatchBuilderIsInvoked = TRUE;
147             }
148         }
149
150         $this->invocationMatcher->invoked($invocation);
151
152         try {
153             if ( $this->parametersMatcher !== NULL &&
154                 !$this->parametersMatcher->matches($invocation)) {
155                 $this->parametersMatcher->verify();
156             }
157         }
158
159         catch (PHPUnit_Framework_ExpectationFailedException $e) {
160             throw new PHPUnit_Framework_ExpectationFailedException(
161               sprintf(
162                 "Expectation failed for %s when %s\n%s",
163
164                 $this->methodNameMatcher->toString(),
165                 $this->invocationMatcher->toString(),
166                 $e->getDescription()
167               ),
168               $e->getComparisonFailure()
169             );
170         }
171
172         if ($this->stub) {
173             return $this->stub->invoke($invocation);
174         }
175
176         return NULL;
177     }
178
179     public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
180     {
181         if ($this->afterMatchBuilderId !== NULL) {
182             $builder = $invocation->object->__phpunit_getInvocationMocker()->lookupId($this->afterMatchBuilderId);
183
184             if (!$builder) {
185                 throw new RuntimeException(
186                   sprintf(
187                     'No builder found for match builder identification <%s>',
188
189                     $this->afterMatchBuilderId
190                   )
191                 );
192             }
193
194             $matcher = $builder->getMatcher();
195
196             if (!$matcher) {
197                 return FALSE;
198             }
199
200             if (!$matcher->invocationMatcher->hasBeenInvoked()) {
201                 return FALSE;
202             }
203         }
204
205         if ($this->invocationMatcher === NULL) {
206             throw new RuntimeException('No invocation matcher is set');
207         }
208
209         if ($this->methodNameMatcher === NULL) {
210             throw new RuntimeException('No method matcher is set');
211         }
212
213         if (!$this->invocationMatcher->matches($invocation)) {
214             return FALSE;
215         }
216
217         try {
218             if (!$this->methodNameMatcher->matches($invocation)) {
219                 return FALSE;
220             }
221         }
222
223         catch (PHPUnit_Framework_ExpectationFailedException $e) {
224             throw new PHPUnit_Framework_ExpectationFailedException(
225               sprintf(
226                 "Expectation failed for %s when %s\n%s",
227
228                 $this->methodNameMatcher->toString(),
229                 $this->invocationMatcher->toString(),
230                 $e->getDescription()
231               ),
232               $e->getComparisonFailure()
233             );
234         }
235
236         return TRUE;
237     }
238
239     public function verify()
240     {
241         if ($this->invocationMatcher === NULL) {
242             throw new RuntimeException('No invocation matcher is set');
243         }
244
245         if ($this->methodNameMatcher === NULL) {
246             throw new RuntimeException('No method matcher is set');
247         }
248
249         try {
250             $this->invocationMatcher->verify();
251
252             if ($this->parametersMatcher !== NULL) {
253                 $this->parametersMatcher->verify();
254             }
255         }
256
257         catch (PHPUnit_Framework_ExpectationFailedException $e) {
258             throw new PHPUnit_Framework_ExpectationFailedException(
259               sprintf(
260                 "Expectation failed for %s when %s.\n%s",
261
262                 $this->methodNameMatcher->toString(),
263                 $this->invocationMatcher->toString(),
264                 $e->getDescription()
265               )
266             );
267         }
268     }
269 }
270
271 require_once 'PHPUnit/Framework/MockObject/Matcher/AnyInvokedCount.php';
272 require_once 'PHPUnit/Framework/MockObject/Matcher/AnyParameters.php';
273 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtIndex.php';
274 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedAtLeastOnce.php';
275 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedCount.php';
276 require_once 'PHPUnit/Framework/MockObject/Matcher/InvokedRecorder.php';
277 require_once 'PHPUnit/Framework/MockObject/Matcher/MethodName.php';
278 require_once 'PHPUnit/Framework/MockObject/Matcher/Parameters.php';
279 require_once 'PHPUnit/Framework/MockObject/Matcher/StatelessInvocation.php';
280 ?>