Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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