| 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/Util/Filter.php';
|
|
|
47 |
require_once 'PHPUnit/Framework/MockObject/Builder/InvocationMocker.php';
|
|
|
48 |
require_once 'PHPUnit/Framework/MockObject/Builder/Match.php';
|
|
|
49 |
require_once 'PHPUnit/Framework/MockObject/Builder/Namespace.php';
|
|
|
50 |
require_once 'PHPUnit/Framework/MockObject/Matcher.php';
|
|
|
51 |
require_once 'PHPUnit/Framework/MockObject/Stub.php';
|
|
|
52 |
require_once 'PHPUnit/Framework/MockObject/Invocation.php';
|
|
|
53 |
require_once 'PHPUnit/Framework/MockObject/Invokable.php';
|
|
|
54 |
|
|
|
55 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Mocker for invocations which are sent from
|
|
|
59 |
* PHPUnit_Framework_MockObject_MockObject objects.
|
|
|
60 |
*
|
|
|
61 |
* Keeps track of all expectations and stubs as well as registering
|
|
|
62 |
* identifications for builders.
|
|
|
63 |
*
|
|
|
64 |
* @category Testing
|
|
|
65 |
* @package PHPUnit
|
|
|
66 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
67 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
68 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
69 |
* @version Release: 3.4.15
|
|
|
70 |
* @link http://www.phpunit.de/
|
|
|
71 |
* @since Class available since Release 3.0.0
|
|
|
72 |
*/
|
|
|
73 |
class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
|
|
|
74 |
{
|
|
|
75 |
/**
|
|
|
76 |
* @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
|
|
|
77 |
*/
|
|
|
78 |
protected $matchers = array();
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* @var PHPUnit_Framework_MockObject_Builder_Match[]
|
|
|
82 |
*/
|
|
|
83 |
protected $builderMap = array();
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
|
|
|
87 |
*/
|
|
|
88 |
public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
|
|
|
89 |
{
|
|
|
90 |
$this->matchers[] = $matcher;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* @param mixed $id
|
|
|
95 |
* @return boolean|null
|
|
|
96 |
*/
|
|
|
97 |
public function lookupId($id)
|
|
|
98 |
{
|
|
|
99 |
if (isset($this->builderMap[$id])) {
|
|
|
100 |
return $this->builderMap[$id];
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return NULL;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* @param mixed $id
|
|
|
108 |
* @param PHPUnit_Framework_MockObject_Builder_Match $builder
|
|
|
109 |
* @throws PHPUnit_Framework_Exception
|
|
|
110 |
*/
|
|
|
111 |
public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
|
|
|
112 |
{
|
|
|
113 |
if (isset($this->builderMap[$id])) {
|
|
|
114 |
throw new PHPUnit_Framework_Exception(
|
|
|
115 |
'Match builder with id <' . $id . '> is already registered.'
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$this->builderMap[$id] = $builder;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
|
|
|
124 |
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
|
|
|
125 |
*/
|
|
|
126 |
public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
|
|
|
127 |
{
|
|
|
128 |
return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
|
|
|
129 |
$this, $matcher
|
|
|
130 |
);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
|
|
135 |
* @return mixed
|
|
|
136 |
*/
|
|
|
137 |
public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
|
|
|
138 |
{
|
|
|
139 |
$exception = NULL;
|
|
|
140 |
$hasReturnValue = FALSE;
|
|
|
141 |
|
|
|
142 |
if (strtolower($invocation->methodName) == '__tostring') {
|
|
|
143 |
$returnValue = '';
|
|
|
144 |
} else {
|
|
|
145 |
$returnValue = NULL;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
foreach ($this->matchers as $match) {
|
|
|
149 |
try {
|
|
|
150 |
if ($match->matches($invocation)) {
|
|
|
151 |
$value = $match->invoked($invocation);
|
|
|
152 |
|
|
|
153 |
if (!$hasReturnValue) {
|
|
|
154 |
$returnValue = $value;
|
|
|
155 |
$hasReturnValue = TRUE;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
catch (Exception $e) {
|
|
|
161 |
$exception = $e;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if ($exception !== NULL) {
|
|
|
166 |
throw $exception;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
return $returnValue;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
|
|
174 |
* @return boolean
|
|
|
175 |
*/
|
|
|
176 |
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
|
|
177 |
{
|
|
|
178 |
foreach($this->matchers as $matcher) {
|
|
|
179 |
if (!$matcher->matches($invocation)) {
|
|
|
180 |
return FALSE;
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
return TRUE;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* @return boolean
|
|
|
189 |
*/
|
|
|
190 |
public function verify()
|
|
|
191 |
{
|
|
|
192 |
foreach($this->matchers as $matcher) {
|
|
|
193 |
$matcher->verify();
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
?>
|