You are here

public function CallCenter::makeCall in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpspec/prophecy/src/Prophecy/Call/CallCenter.php \Prophecy\Call\CallCenter::makeCall()

Makes and records specific method call for object prophecy.

Parameters

ObjectProphecy $prophecy:

string $methodName:

array $arguments:

Return value

mixed Returns null if no promise for prophecy found or promise return value.

Throws

\Prophecy\Exception\Call\UnexpectedCallException If no appropriate method prophecy found

File

vendor/phpspec/prophecy/src/Prophecy/Call/CallCenter.php, line 55

Class

CallCenter
Calls receiver & manager.

Namespace

Prophecy\Call

Code

public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments) {
  $backtrace = debug_backtrace();
  $file = $line = null;
  if (isset($backtrace[2]) && isset($backtrace[2]['file'])) {
    $file = $backtrace[2]['file'];
    $line = $backtrace[2]['line'];
  }

  // If no method prophecies defined, then it's a dummy, so we'll just return null
  if ('__destruct' === $methodName || 0 == count($prophecy
    ->getMethodProphecies())) {
    $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line);
    return null;
  }

  // There are method prophecies, so it's a fake/stub. Searching prophecy for this call
  $matches = array();
  foreach ($prophecy
    ->getMethodProphecies($methodName) as $methodProphecy) {
    if (0 < ($score = $methodProphecy
      ->getArgumentsWildcard()
      ->scoreArguments($arguments))) {
      $matches[] = array(
        $score,
        $methodProphecy,
      );
    }
  }

  // If fake/stub doesn't have method prophecy for this call - throw exception
  if (!count($matches)) {
    throw $this
      ->createUnexpectedCallException($prophecy, $methodName, $arguments);
  }

  // Sort matches by their score value
  @usort($matches, function ($match1, $match2) {
    return $match2[0] - $match1[0];
  });

  // If Highest rated method prophecy has a promise - execute it or return null instead
  $returnValue = null;
  $exception = null;
  if ($promise = $matches[0][1]
    ->getPromise()) {
    try {
      $returnValue = $promise
        ->execute($arguments, $prophecy, $matches[0][1]);
    } catch (\Exception $e) {
      $exception = $e;
    }
  }
  $this->recordedCalls[] = new Call($methodName, $arguments, $returnValue, $exception, $file, $line);
  if (null !== $exception) {
    throw $exception;
  }
  return $returnValue;
}