Complex classes like Mock often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Mock implements MockInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Stores an array of all expectation directors for this mock |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $_mockery_expectations = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Stores an inital number of expectations that can be manipulated |
||
| 36 | * while using the getter method. |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $_mockery_expectations_count = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Flag to indicate whether we can ignore method calls missing from our |
||
| 44 | * expectations |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $_mockery_ignoreMissing = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Flag to indicate whether we can defer method calls missing from our |
||
| 52 | * expectations |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $_mockery_deferMissing = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Flag to indicate whether this mock was verified |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $_mockery_verified = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Given name of the mock |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $_mockery_name = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Order number of allocation |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | protected $_mockery_allocatedOrder = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Current ordered number |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | protected $_mockery_currentOrder = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Ordered groups |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $_mockery_groups = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Mock container containing this mock object |
||
| 95 | * |
||
| 96 | * @var \Mockery\Container |
||
| 97 | */ |
||
| 98 | protected $_mockery_container = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Instance of a core object on which methods are called in the event |
||
| 102 | * it has been set, and an expectation for one of the object's methods |
||
| 103 | * does not exist. This implements a simple partial mock proxy system. |
||
| 104 | * |
||
| 105 | * @var object |
||
| 106 | */ |
||
| 107 | protected $_mockery_partial = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Flag to indicate we should ignore all expectations temporarily. Used |
||
| 111 | * mainly to prevent expectation matching when in the middle of a mock |
||
| 112 | * object recording session. |
||
| 113 | * |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $_mockery_disableExpectationMatching = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Stores all stubbed public methods separate from any on-object public |
||
| 120 | * properties that may exist. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $_mockery_mockableProperties = array(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $_mockery_mockableMethods = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Just a local cache for this mock's target's methods |
||
| 133 | * |
||
| 134 | * @var ReflectionMethod[] |
||
| 135 | */ |
||
| 136 | protected static $_mockery_methods; |
||
| 137 | |||
| 138 | protected $_mockery_allowMockingProtectedMethods = false; |
||
| 139 | |||
| 140 | protected $_mockery_receivedMethodCalls; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
||
| 144 | * @var mixed |
||
| 145 | */ |
||
| 146 | protected $_mockery_defaultReturnValue = null; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * We want to avoid constructors since class is copied to Generator.php |
||
| 150 | * for inclusion on extending class definitions. |
||
| 151 | * |
||
| 152 | * @param \Mockery\Container $container |
||
| 153 | * @param object $partialObject |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
||
| 157 | { |
||
| 158 | if (is_null($container)) { |
||
| 159 | $container = new \Mockery\Container; |
||
| 160 | } |
||
| 161 | $this->_mockery_container = $container; |
||
| 162 | if (!is_null($partialObject)) { |
||
| 163 | $this->_mockery_partial = $partialObject; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed()) { |
||
| 167 | foreach ($this->mockery_getMethods() as $method) { |
||
| 168 | if ($method->isPublic() && !$method->isStatic()) { |
||
| 169 | $this->_mockery_mockableMethods[] = $method->getName(); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set expected method calls |
||
| 177 | * |
||
| 178 | * @param string $methodName,... one or many methods that are expected to be called in this mock |
||
| 179 | * @return \Mockery\Expectation |
||
| 180 | */ |
||
| 181 | public function shouldReceive($methodName) |
||
| 182 | { |
||
| 183 | if (func_num_args() < 1) { |
||
| 184 | throw new \InvalidArgumentException("At least one method name is required"); |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach (func_get_args() as $method) { |
||
| 188 | if ("" == $method) { |
||
| 189 | throw new \InvalidArgumentException("Received empty method name"); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** @var array $nonPublicMethods */ |
||
| 194 | $nonPublicMethods = $this->getNonPublicMethods(); |
||
| 195 | |||
| 196 | $self = $this; |
||
| 197 | $allowMockingProtectedMethods = $this->_mockery_allowMockingProtectedMethods; |
||
| 198 | |||
| 199 | $lastExpectation = \Mockery::parseShouldReturnArgs( |
||
| 200 | $this, func_get_args(), function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) { |
||
| 201 | $rm = $self->mockery_getMethod($method); |
||
| 202 | if ($rm) { |
||
| 203 | if ($rm->isPrivate()) { |
||
| 204 | throw new \InvalidArgumentException("$method() cannot be mocked as it is a private method"); |
||
| 205 | } |
||
| 206 | if (!$allowMockingProtectedMethods && $rm->isProtected()) { |
||
| 207 | throw new \InvalidArgumentException("$method() cannot be mocked as it is a protected method and mocking protected methods is not enabled for the currently used mock object."); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $director = $self->mockery_getExpectationsFor($method); |
||
| 212 | if (!$director) { |
||
| 213 | $director = new \Mockery\ExpectationDirector($method, $self); |
||
| 214 | $self->mockery_setExpectationsFor($method, $director); |
||
| 215 | } |
||
| 216 | $expectation = new \Mockery\Expectation($self, $method); |
||
| 217 | $director->addExpectation($expectation); |
||
| 218 | return $expectation; |
||
| 219 | } |
||
| 220 | ); |
||
| 221 | return $lastExpectation; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Shortcut method for setting an expectation that a method should not be called. |
||
| 226 | * |
||
| 227 | * @param string $methodName,... one or many methods that are expected not to be called in this mock |
||
| 228 | * @return \Mockery\Expectation |
||
| 229 | */ |
||
| 230 | public function shouldNotReceive($methodName) |
||
| 231 | { |
||
| 232 | $expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args()); |
||
| 233 | $expectation->never(); |
||
| 234 | return $expectation; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Allows additional methods to be mocked that do not explicitly exist on mocked class |
||
| 239 | * @param String $method name of the method to be mocked |
||
| 240 | * @return Mock |
||
| 241 | */ |
||
| 242 | public function shouldAllowMockingMethod($method) |
||
| 243 | { |
||
| 244 | $this->_mockery_mockableMethods[] = $method; |
||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set mock to ignore unexpected methods and return Undefined class |
||
| 250 | * @param mixed $returnValue the default return value for calls to missing functions on this mock |
||
| 251 | * @return Mock |
||
| 252 | */ |
||
| 253 | public function shouldIgnoreMissing($returnValue = null) |
||
| 254 | { |
||
| 255 | $this->_mockery_ignoreMissing = true; |
||
| 256 | $this->_mockery_defaultReturnValue = $returnValue; |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function asUndefined() |
||
| 261 | { |
||
| 262 | $this->_mockery_ignoreMissing = true; |
||
| 263 | $this->_mockery_defaultReturnValue = new \Mockery\Undefined; |
||
| 264 | return $this; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return Mock |
||
| 269 | */ |
||
| 270 | public function shouldAllowMockingProtectedMethods() |
||
| 271 | { |
||
| 272 | $this->_mockery_allowMockingProtectedMethods = true; |
||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Set mock to defer unexpected methods to it's parent |
||
| 279 | * |
||
| 280 | * This is particularly useless for this class, as it doesn't have a parent, |
||
| 281 | * but included for completeness |
||
| 282 | * |
||
| 283 | * @return Mock |
||
| 284 | */ |
||
| 285 | public function shouldDeferMissing() |
||
| 286 | { |
||
| 287 | $this->_mockery_deferMissing = true; |
||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Create an obviously worded alias to shouldDeferMissing() |
||
| 293 | * |
||
| 294 | * @return Mock |
||
| 295 | */ |
||
| 296 | public function makePartial() |
||
| 297 | { |
||
| 298 | return $this->shouldDeferMissing(); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Accepts a closure which is executed with an object recorder which proxies |
||
| 303 | * to the partial source object. The intent being to record the |
||
| 304 | * interactions of a concrete object as a set of expectations on the |
||
| 305 | * current mock object. The partial may then be passed to a second process |
||
| 306 | * to see if it fulfils the same (or exact same) contract as the original. |
||
| 307 | * |
||
| 308 | * @param Closure $closure |
||
| 309 | */ |
||
| 310 | public function shouldExpect(\Closure $closure) |
||
| 311 | { |
||
| 312 | $recorder = new \Mockery\Recorder($this, $this->_mockery_partial); |
||
| 313 | $this->_mockery_disableExpectationMatching = true; |
||
| 314 | $closure($recorder); |
||
| 315 | $this->_mockery_disableExpectationMatching = false; |
||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * In the event shouldReceive() accepting one or more methods/returns, |
||
| 321 | * this method will switch them from normal expectations to default |
||
| 322 | * expectations |
||
| 323 | * |
||
| 324 | * @return self |
||
| 325 | */ |
||
| 326 | public function byDefault() |
||
| 327 | { |
||
| 328 | foreach ($this->_mockery_expectations as $director) { |
||
| 329 | $exps = $director->getExpectations(); |
||
| 330 | foreach ($exps as $exp) { |
||
| 331 | $exp->byDefault(); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Capture calls to this mock |
||
| 339 | */ |
||
| 340 | public function __call($method, array $args) |
||
| 341 | { |
||
| 342 | return $this->_mockery_handleMethodCall($method, $args); |
||
| 343 | } |
||
| 344 | |||
| 345 | public static function __callStatic($method, array $args) |
||
| 346 | { |
||
| 347 | return self::_mockery_handleStaticMethodCall($method, $args); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Forward calls to this magic method to the __call method |
||
| 352 | */ |
||
| 353 | public function __toString() |
||
| 354 | { |
||
| 355 | return $this->__call('__toString', array()); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Iterate across all expectation directors and validate each |
||
| 360 | * |
||
| 361 | * @throws \Mockery\CountValidator\Exception |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | public function mockery_verify() |
||
| 365 | { |
||
| 366 | if ($this->_mockery_verified) { |
||
| 367 | return true; |
||
| 368 | } |
||
| 369 | if (isset($this->_mockery_ignoreVerification) |
||
| 370 | && $this->_mockery_ignoreVerification == true) { |
||
| 371 | return true; |
||
| 372 | } |
||
| 373 | $this->_mockery_verified = true; |
||
| 374 | foreach ($this->_mockery_expectations as $director) { |
||
| 375 | $director->verify(); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Tear down tasks for this mock |
||
| 381 | * |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | public function mockery_teardown() |
||
| 385 | { |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Fetch the next available allocation order number |
||
| 390 | * |
||
| 391 | * @return int |
||
| 392 | */ |
||
| 393 | public function mockery_allocateOrder() |
||
| 394 | { |
||
| 395 | $this->_mockery_allocatedOrder += 1; |
||
| 396 | return $this->_mockery_allocatedOrder; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Set ordering for a group |
||
| 401 | * |
||
| 402 | * @param mixed $group |
||
| 403 | * @param int $order |
||
| 404 | */ |
||
| 405 | public function mockery_setGroup($group, $order) |
||
| 406 | { |
||
| 407 | $this->_mockery_groups[$group] = $order; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Fetch array of ordered groups |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | public function mockery_getGroups() |
||
| 416 | { |
||
| 417 | return $this->_mockery_groups; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set current ordered number |
||
| 422 | * |
||
| 423 | * @param int $order |
||
| 424 | */ |
||
| 425 | public function mockery_setCurrentOrder($order) |
||
| 426 | { |
||
| 427 | $this->_mockery_currentOrder = $order; |
||
| 428 | return $this->_mockery_currentOrder; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get current ordered number |
||
| 433 | * |
||
| 434 | * @return int |
||
| 435 | */ |
||
| 436 | public function mockery_getCurrentOrder() |
||
| 437 | { |
||
| 438 | return $this->_mockery_currentOrder; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Validate the current mock's ordering |
||
| 443 | * |
||
| 444 | * @param string $method |
||
| 445 | * @param int $order |
||
| 446 | * @throws \Mockery\Exception |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | public function mockery_validateOrder($method, $order) |
||
| 450 | { |
||
| 451 | if ($order < $this->_mockery_currentOrder) { |
||
| 452 | $exception = new \Mockery\Exception\InvalidOrderException( |
||
| 453 | 'Method ' . __CLASS__ . '::' . $method . '()' |
||
| 454 | . ' called out of order: expected order ' |
||
| 455 | . $order . ', was ' . $this->_mockery_currentOrder |
||
| 456 | ); |
||
| 457 | $exception->setMock($this) |
||
| 458 | ->setMethodName($method) |
||
| 459 | ->setExpectedOrder($order) |
||
| 460 | ->setActualOrder($this->_mockery_currentOrder); |
||
| 461 | throw $exception; |
||
| 462 | } |
||
| 463 | $this->mockery_setCurrentOrder($order); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the count of expectations for this mock |
||
| 468 | * |
||
| 469 | * @return int |
||
| 470 | */ |
||
| 471 | public function mockery_getExpectationCount() |
||
| 472 | { |
||
| 473 | $count = $this->_mockery_expectations_count; |
||
| 474 | foreach ($this->_mockery_expectations as $director) { |
||
| 475 | $count += $director->getExpectationCount(); |
||
| 476 | } |
||
| 477 | return $count; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Return the expectations director for the given method |
||
| 482 | * |
||
| 483 | * @var string $method |
||
| 484 | * @return \Mockery\ExpectationDirector|null |
||
| 485 | */ |
||
| 486 | public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
||
| 487 | { |
||
| 488 | $this->_mockery_expectations[$method] = $director; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Return the expectations director for the given method |
||
| 493 | * |
||
| 494 | * @var string $method |
||
| 495 | * @return \Mockery\ExpectationDirector|null |
||
| 496 | */ |
||
| 497 | public function mockery_getExpectationsFor($method) |
||
| 498 | { |
||
| 499 | if (isset($this->_mockery_expectations[$method])) { |
||
| 500 | return $this->_mockery_expectations[$method]; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Find an expectation matching the given method and arguments |
||
| 506 | * |
||
| 507 | * @var string $method |
||
| 508 | * @var array $args |
||
| 509 | * @return \Mockery\Expectation|null |
||
| 510 | */ |
||
| 511 | public function mockery_findExpectation($method, array $args) |
||
| 512 | { |
||
| 513 | if (!isset($this->_mockery_expectations[$method])) { |
||
| 514 | return null; |
||
| 515 | } |
||
| 516 | $director = $this->_mockery_expectations[$method]; |
||
| 517 | |||
| 518 | return $director->findExpectation($args); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Return the container for this mock |
||
| 523 | * |
||
| 524 | * @return \Mockery\Container |
||
| 525 | */ |
||
| 526 | public function mockery_getContainer() |
||
| 527 | { |
||
| 528 | return $this->_mockery_container; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Return the name for this mock |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | public function mockery_getName() |
||
| 537 | { |
||
| 538 | return __CLASS__; |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | public function mockery_getMockableProperties() |
||
| 545 | { |
||
| 546 | return $this->_mockery_mockableProperties; |
||
| 547 | } |
||
| 548 | |||
| 549 | public function __isset($name) |
||
| 550 | { |
||
| 551 | if (false === stripos($name, '_mockery_') && method_exists(get_parent_class($this), '__isset')) { |
||
| 552 | return parent::__isset($name); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | |||
| 556 | public function mockery_getExpectations() |
||
| 557 | { |
||
| 558 | return $this->_mockery_expectations; |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Calls a parent class method and returns the result. Used in a passthru |
||
| 563 | * expectation where a real return value is required while still taking |
||
| 564 | * advantage of expectation matching and call count verification. |
||
| 565 | * |
||
| 566 | * @param string $name |
||
| 567 | * @param array $args |
||
| 568 | * @return mixed |
||
| 569 | */ |
||
| 570 | public function mockery_callSubjectMethod($name, array $args) |
||
| 571 | { |
||
| 572 | return call_user_func_array('parent::' . $name, $args); |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @return string[] |
||
| 577 | */ |
||
| 578 | public function mockery_getMockableMethods() |
||
| 579 | { |
||
| 580 | return $this->_mockery_mockableMethods; |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | public function mockery_isAnonymous() |
||
| 587 | { |
||
| 588 | $rfc = new \ReflectionClass($this); |
||
| 589 | $onlyImplementsMock = count($rfc->getInterfaces()) == 1; |
||
| 590 | return (false === $rfc->getParentClass()) && $onlyImplementsMock; |
||
| 591 | } |
||
| 592 | |||
| 593 | public function __wakeup() |
||
| 594 | { |
||
| 595 | /** |
||
| 596 | * This does not add __wakeup method support. It's a blind method and any |
||
| 597 | * expected __wakeup work will NOT be performed. It merely cuts off |
||
| 598 | * annoying errors where a __wakeup exists but is not essential when |
||
| 599 | * mocking |
||
| 600 | */ |
||
| 601 | } |
||
| 602 | |||
| 603 | public function __destruct() |
||
| 604 | { |
||
| 605 | /** |
||
| 606 | * Overrides real class destructor in case if class was created without original constructor |
||
| 607 | */ |
||
| 608 | } |
||
| 609 | |||
| 610 | public function mockery_getMethod($name) |
||
| 611 | { |
||
| 612 | foreach ($this->mockery_getMethods() as $method) { |
||
| 613 | if ($method->getName() == $name) { |
||
| 614 | return $method; |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | return null; |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param string $name Method name. |
||
| 623 | * |
||
| 624 | * @return mixed Generated return value based on the declared return value of the named method. |
||
| 625 | */ |
||
| 626 | public function mockery_returnValueForMethod($name) |
||
| 627 | { |
||
| 628 | if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) { |
||
| 629 | return; |
||
| 630 | } |
||
| 631 | |||
| 632 | $rm = $this->mockery_getMethod($name); |
||
| 633 | if (!$rm || !$rm->hasReturnType()) { |
||
| 634 | return; |
||
| 635 | } |
||
| 636 | |||
| 637 | $type = (string) $rm->getReturnType(); |
||
| 638 | switch ($type) { |
||
| 639 | case '': return; |
||
| 640 | case 'string': return ''; |
||
| 641 | case 'int': return 0; |
||
| 642 | case 'float': return 0.0; |
||
| 643 | case 'bool': return false; |
||
| 644 | case 'array': return []; |
||
| 645 | |||
| 646 | case 'callable': |
||
| 647 | case 'Closure': |
||
| 648 | return function () {}; |
||
| 649 | |||
| 650 | case 'Traversable': |
||
| 651 | case 'Generator': |
||
| 652 | // Remove eval() when minimum version >=5.5 |
||
| 653 | $generator = eval('return function () { yield; };'); |
||
| 654 | return $generator(); |
||
| 655 | |||
| 656 | case 'self': |
||
| 657 | return \Mockery::mock($rm->getDeclaringClass()->getName()); |
||
| 658 | |||
| 659 | default: |
||
| 660 | return \Mockery::mock($type); |
||
| 661 | } |
||
| 662 | } |
||
| 663 | |||
| 664 | public function shouldHaveReceived($method, $args = null) |
||
| 665 | { |
||
| 666 | $expectation = new \Mockery\VerificationExpectation($this, $method); |
||
| 667 | if (null !== $args) { |
||
| 668 | $expectation->withArgs($args); |
||
|
|
|||
| 669 | } |
||
| 670 | $expectation->atLeast()->once(); |
||
| 671 | $director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation); |
||
| 672 | $this->_mockery_expectations_count++; |
||
| 673 | $director->verify(); |
||
| 674 | return $director; |
||
| 675 | } |
||
| 676 | |||
| 677 | public function shouldNotHaveReceived($method, $args = null) |
||
| 678 | { |
||
| 679 | $expectation = new \Mockery\VerificationExpectation($this, $method); |
||
| 680 | if (null !== $args) { |
||
| 681 | $expectation->withArgs($args); |
||
| 682 | } |
||
| 683 | $expectation->never(); |
||
| 684 | $director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation); |
||
| 685 | $this->_mockery_expectations_count++; |
||
| 686 | $director->verify(); |
||
| 687 | return null; |
||
| 688 | } |
||
| 689 | |||
| 690 | protected static function _mockery_handleStaticMethodCall($method, array $args) |
||
| 691 | { |
||
| 692 | try { |
||
| 693 | $associatedRealObject = \Mockery::fetchMock(__CLASS__); |
||
| 694 | return $associatedRealObject->__call($method, $args); |
||
| 695 | } catch (\BadMethodCallException $e) { |
||
| 696 | throw new \BadMethodCallException( |
||
| 697 | 'Static method ' . $associatedRealObject->mockery_getName() . '::' . $method |
||
| 698 | . '() does not exist on this mock object' |
||
| 699 | ); |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | protected function _mockery_getReceivedMethodCalls() |
||
| 704 | { |
||
| 705 | return $this->_mockery_receivedMethodCalls ?: $this->_mockery_receivedMethodCalls = new \Mockery\ReceivedMethodCalls(); |
||
| 706 | } |
||
| 707 | |||
| 708 | protected function _mockery_handleMethodCall($method, array $args) |
||
| 709 | { |
||
| 710 | $this->_mockery_getReceivedMethodCalls()->push(new \Mockery\MethodCall($method, $args)); |
||
| 711 | |||
| 712 | $rm = $this->mockery_getMethod($method); |
||
| 713 | if ($rm && $rm->isProtected() && !$this->_mockery_allowMockingProtectedMethods) { |
||
| 714 | if ($rm->isAbstract()) { |
||
| 715 | return; |
||
| 716 | } |
||
| 717 | |||
| 718 | try { |
||
| 719 | $prototype = $rm->getPrototype(); |
||
| 720 | if ($prototype->isAbstract()) { |
||
| 721 | return; |
||
| 722 | } |
||
| 723 | } catch (\ReflectionException $re) { |
||
| 724 | // noop - there is no hasPrototype method |
||
| 725 | } |
||
| 726 | |||
| 727 | return call_user_func_array("parent::$method", $args); |
||
| 728 | } |
||
| 729 | |||
| 730 | if (isset($this->_mockery_expectations[$method]) |
||
| 731 | && !$this->_mockery_disableExpectationMatching) { |
||
| 732 | $handler = $this->_mockery_expectations[$method]; |
||
| 733 | |||
| 734 | try { |
||
| 735 | return $handler->call($args); |
||
| 736 | } catch (\Mockery\Exception\NoMatchingExpectationException $e) { |
||
| 737 | if (!$this->_mockery_ignoreMissing && !$this->_mockery_deferMissing) { |
||
| 738 | throw $e; |
||
| 739 | } |
||
| 740 | } |
||
| 741 | } |
||
| 742 | |||
| 743 | if (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) { |
||
| 744 | return call_user_func_array(array($this->_mockery_partial, $method), $args); |
||
| 745 | } elseif ($this->_mockery_deferMissing && is_callable("parent::$method") |
||
| 746 | && (!$this->hasMethodOverloadingInParentClass() || method_exists(get_parent_class($this), $method))) { |
||
| 747 | return call_user_func_array("parent::$method", $args); |
||
| 748 | } elseif ($method == '__toString') { |
||
| 749 | // __toString is special because we force its addition to the class API regardless of the |
||
| 750 | // original implementation. Thus, we should always return a string rather than honor |
||
| 751 | // _mockery_ignoreMissing and break the API with an error. |
||
| 752 | return sprintf("%s#%s", __CLASS__, spl_object_hash($this)); |
||
| 753 | } elseif ($this->_mockery_ignoreMissing) { |
||
| 754 | if (\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() || (method_exists($this->_mockery_partial, $method) || is_callable("parent::$method"))) { |
||
| 755 | if ($this->_mockery_defaultReturnValue instanceof \Mockery\Undefined) { |
||
| 756 | return call_user_func_array(array($this->_mockery_defaultReturnValue, $method), $args); |
||
| 757 | } elseif (null === $this->_mockery_defaultReturnValue) { |
||
| 758 | return $this->mockery_returnValueForMethod($method); |
||
| 759 | } else { |
||
| 760 | return $this->_mockery_defaultReturnValue; |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | $message = 'Method ' . __CLASS__ . '::' . $method . |
||
| 766 | '() does not exist on this mock object'; |
||
| 767 | |||
| 768 | if (!is_null($rm)) { |
||
| 769 | $message = 'Received ' . __CLASS__ . |
||
| 770 | '::' . $method . '(), but no expectations were specified'; |
||
| 771 | } |
||
| 772 | |||
| 773 | throw new \BadMethodCallException( |
||
| 774 | $message |
||
| 775 | ); |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Uses reflection to get the list of all |
||
| 780 | * methods within the current mock object |
||
| 781 | * |
||
| 782 | * @return array |
||
| 783 | */ |
||
| 784 | protected function mockery_getMethods() |
||
| 785 | { |
||
| 786 | if (static::$_mockery_methods) { |
||
| 787 | return static::$_mockery_methods; |
||
| 788 | } |
||
| 789 | |||
| 790 | if (isset($this->_mockery_partial)) { |
||
| 791 | $reflected = new \ReflectionObject($this->_mockery_partial); |
||
| 792 | } else { |
||
| 793 | $reflected = new \ReflectionClass($this); |
||
| 794 | } |
||
| 795 | |||
| 796 | return static::$_mockery_methods = $reflected->getMethods(); |
||
| 797 | } |
||
| 798 | |||
| 799 | private function hasMethodOverloadingInParentClass() |
||
| 800 | { |
||
| 801 | // if there's __call any name would be callable |
||
| 802 | return is_callable('parent::' . uniqid(__FUNCTION__)); |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @return array |
||
| 807 | */ |
||
| 808 | private function getNonPublicMethods() |
||
| 819 | } |
||
| 820 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: