| 148 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Mockery
|
|
|
4 |
*
|
|
|
5 |
* LICENSE
|
|
|
6 |
*
|
|
|
7 |
* This source file is subject to the new BSD license that is bundled
|
|
|
8 |
* with this package in the file LICENSE.txt.
|
|
|
9 |
* It is also available through the world-wide-web at this URL:
|
|
|
10 |
* http://github.com/padraic/mockery/blob/master/LICENSE
|
|
|
11 |
* If you did not receive a copy of the license and are unable to
|
|
|
12 |
* obtain it through the world-wide-web, please send an email
|
|
|
13 |
* to padraic@php.net so we can send you a copy immediately.
|
|
|
14 |
*
|
|
|
15 |
* @category Mockery
|
|
|
16 |
* @package Mockery
|
|
|
17 |
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
|
|
|
18 |
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
namespace Mockery;
|
|
|
22 |
|
|
|
23 |
class Configuration
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* Boolean assertion of whether we can mock methods which do not actually
|
|
|
27 |
* exist for the given class or object (ignored for unreal mocks)
|
|
|
28 |
*
|
|
|
29 |
* @var bool
|
|
|
30 |
*/
|
|
|
31 |
protected $_allowMockingNonExistentMethod = true;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Boolean assertion of whether we ignore unnecessary mocking of methods,
|
|
|
35 |
* i.e. when method expectations are made, set using a zeroOrMoreTimes()
|
|
|
36 |
* constraint, and then never called. Essentially such expectations are
|
|
|
37 |
* not required and are just taking up test space.
|
|
|
38 |
*
|
|
|
39 |
* @var bool
|
|
|
40 |
*/
|
|
|
41 |
protected $_allowMockingMethodsUnnecessarily = true;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @var QuickDefinitionsConfiguration
|
|
|
45 |
*/
|
|
|
46 |
protected $_quickDefinitionsConfiguration;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Parameter map for use with PHP internal classes.
|
|
|
50 |
*
|
|
|
51 |
* @var array
|
|
|
52 |
*/
|
|
|
53 |
protected $_internalClassParamMap = array();
|
|
|
54 |
|
|
|
55 |
protected $_constantsMap = array();
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Boolean assertion is reflection caching enabled or not. It should be
|
|
|
59 |
* always enabled, except when using PHPUnit's --static-backup option.
|
|
|
60 |
*
|
|
|
61 |
* @see https://github.com/mockery/mockery/issues/268
|
|
|
62 |
*/
|
|
|
63 |
protected $_reflectionCacheEnabled = true;
|
|
|
64 |
|
|
|
65 |
public function __construct()
|
|
|
66 |
{
|
|
|
67 |
$this->_quickDefinitionsConfiguration = new QuickDefinitionsConfiguration();
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Custom object formatters
|
|
|
72 |
*
|
|
|
73 |
* @var array
|
|
|
74 |
*/
|
|
|
75 |
protected $_objectFormatters = array();
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Default argument matchers
|
|
|
79 |
*
|
|
|
80 |
* @var array
|
|
|
81 |
*/
|
|
|
82 |
protected $_defaultMatchers = array();
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Set boolean to allow/prevent mocking of non-existent methods
|
|
|
86 |
*
|
|
|
87 |
* @param bool $flag
|
|
|
88 |
*/
|
|
|
89 |
public function allowMockingNonExistentMethods($flag = true)
|
|
|
90 |
{
|
|
|
91 |
$this->_allowMockingNonExistentMethod = (bool) $flag;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Return flag indicating whether mocking non-existent methods allowed
|
|
|
96 |
*
|
|
|
97 |
* @return bool
|
|
|
98 |
*/
|
|
|
99 |
public function mockingNonExistentMethodsAllowed()
|
|
|
100 |
{
|
|
|
101 |
return $this->_allowMockingNonExistentMethod;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Set boolean to allow/prevent unnecessary mocking of methods
|
|
|
106 |
*
|
|
|
107 |
* @param bool $flag
|
|
|
108 |
*
|
|
|
109 |
* @deprecated since 1.4.0
|
|
|
110 |
*/
|
|
|
111 |
public function allowMockingMethodsUnnecessarily($flag = true)
|
|
|
112 |
{
|
|
|
113 |
@trigger_error(sprintf("The %s method is deprecated and will be removed in a future version of Mockery", __METHOD__), E_USER_DEPRECATED);
|
|
|
114 |
|
|
|
115 |
$this->_allowMockingMethodsUnnecessarily = (bool) $flag;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Return flag indicating whether mocking non-existent methods allowed
|
|
|
120 |
*
|
|
|
121 |
* @return bool
|
|
|
122 |
*
|
|
|
123 |
* @deprecated since 1.4.0
|
|
|
124 |
*/
|
|
|
125 |
public function mockingMethodsUnnecessarilyAllowed()
|
|
|
126 |
{
|
|
|
127 |
@trigger_error(sprintf("The %s method is deprecated and will be removed in a future version of Mockery", __METHOD__), E_USER_DEPRECATED);
|
|
|
128 |
|
|
|
129 |
return $this->_allowMockingMethodsUnnecessarily;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Set a parameter map (array of param signature strings) for the method
|
|
|
134 |
* of an internal PHP class.
|
|
|
135 |
*
|
|
|
136 |
* @param string $class
|
|
|
137 |
* @param string $method
|
|
|
138 |
* @param array $map
|
|
|
139 |
*/
|
|
|
140 |
public function setInternalClassMethodParamMap($class, $method, array $map)
|
|
|
141 |
{
|
|
|
142 |
if (\PHP_MAJOR_VERSION > 7) {
|
|
|
143 |
throw new \LogicException('Internal class parameter overriding is not available in PHP 8. Incompatible signatures have been reclassified as fatal errors.');
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
if (!isset($this->_internalClassParamMap[strtolower($class)])) {
|
|
|
147 |
$this->_internalClassParamMap[strtolower($class)] = array();
|
|
|
148 |
}
|
|
|
149 |
$this->_internalClassParamMap[strtolower($class)][strtolower($method)] = $map;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Remove all overridden parameter maps from internal PHP classes.
|
|
|
154 |
*/
|
|
|
155 |
public function resetInternalClassMethodParamMaps()
|
|
|
156 |
{
|
|
|
157 |
$this->_internalClassParamMap = array();
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Get the parameter map of an internal PHP class method
|
|
|
162 |
*
|
|
|
163 |
* @return array|null
|
|
|
164 |
*/
|
|
|
165 |
public function getInternalClassMethodParamMap($class, $method)
|
|
|
166 |
{
|
|
|
167 |
if (isset($this->_internalClassParamMap[strtolower($class)][strtolower($method)])) {
|
|
|
168 |
return $this->_internalClassParamMap[strtolower($class)][strtolower($method)];
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public function getInternalClassMethodParamMaps()
|
|
|
173 |
{
|
|
|
174 |
return $this->_internalClassParamMap;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public function setConstantsMap(array $map)
|
|
|
178 |
{
|
|
|
179 |
$this->_constantsMap = $map;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
public function getConstantsMap()
|
|
|
183 |
{
|
|
|
184 |
return $this->_constantsMap;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Returns the quick definitions configuration
|
|
|
189 |
*/
|
|
|
190 |
public function getQuickDefinitions(): QuickDefinitionsConfiguration
|
|
|
191 |
{
|
|
|
192 |
return $this->_quickDefinitionsConfiguration;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Disable reflection caching
|
|
|
197 |
*
|
|
|
198 |
* It should be always enabled, except when using
|
|
|
199 |
* PHPUnit's --static-backup option.
|
|
|
200 |
*
|
|
|
201 |
* @see https://github.com/mockery/mockery/issues/268
|
|
|
202 |
*/
|
|
|
203 |
public function disableReflectionCache()
|
|
|
204 |
{
|
|
|
205 |
$this->_reflectionCacheEnabled = false;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Enable reflection caching
|
|
|
210 |
*
|
|
|
211 |
* It should be always enabled, except when using
|
|
|
212 |
* PHPUnit's --static-backup option.
|
|
|
213 |
*
|
|
|
214 |
* @see https://github.com/mockery/mockery/issues/268
|
|
|
215 |
*/
|
|
|
216 |
public function enableReflectionCache()
|
|
|
217 |
{
|
|
|
218 |
$this->_reflectionCacheEnabled = true;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Is reflection cache enabled?
|
|
|
223 |
*/
|
|
|
224 |
public function reflectionCacheEnabled()
|
|
|
225 |
{
|
|
|
226 |
return $this->_reflectionCacheEnabled;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
public function setObjectFormatter($class, $formatterCallback)
|
|
|
230 |
{
|
|
|
231 |
$this->_objectFormatters[$class] = $formatterCallback;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
public function getObjectFormatter($class, $defaultFormatter)
|
|
|
235 |
{
|
|
|
236 |
$parentClass = $class;
|
|
|
237 |
do {
|
|
|
238 |
$classes[] = $parentClass;
|
|
|
239 |
$parentClass = get_parent_class($parentClass);
|
|
|
240 |
} while ($parentClass);
|
|
|
241 |
$classesAndInterfaces = array_merge($classes, class_implements($class));
|
|
|
242 |
foreach ($classesAndInterfaces as $type) {
|
|
|
243 |
if (isset($this->_objectFormatters[$type])) {
|
|
|
244 |
return $this->_objectFormatters[$type];
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
return $defaultFormatter;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* @param string $class
|
|
|
252 |
* @param string $matcherClass
|
|
|
253 |
*/
|
|
|
254 |
public function setDefaultMatcher($class, $matcherClass)
|
|
|
255 |
{
|
|
|
256 |
if (!is_a($matcherClass, \Mockery\Matcher\MatcherAbstract::class, true) &&
|
|
|
257 |
!is_a($matcherClass, \Hamcrest\Matcher::class, true) &&
|
|
|
258 |
!is_a($matcherClass, \Hamcrest_Matcher::class, true)
|
|
|
259 |
) {
|
|
|
260 |
throw new \InvalidArgumentException(
|
|
|
261 |
"Matcher class must be either Hamcrest matcher or extend \Mockery\Matcher\MatcherAbstract, " .
|
|
|
262 |
"'$matcherClass' given."
|
|
|
263 |
);
|
|
|
264 |
}
|
|
|
265 |
$this->_defaultMatchers[$class] = $matcherClass;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
public function getDefaultMatcher($class)
|
|
|
269 |
{
|
|
|
270 |
$parentClass = $class;
|
|
|
271 |
do {
|
|
|
272 |
$classes[] = $parentClass;
|
|
|
273 |
$parentClass = get_parent_class($parentClass);
|
|
|
274 |
} while ($parentClass);
|
|
|
275 |
$classesAndInterfaces = array_merge($classes, class_implements($class));
|
|
|
276 |
foreach ($classesAndInterfaces as $type) {
|
|
|
277 |
if (isset($this->_defaultMatchers[$type])) {
|
|
|
278 |
return $this->_defaultMatchers[$type];
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
return null;
|
|
|
282 |
}
|
|
|
283 |
}
|