Eloquent\Phony\Verification\GeneratorVerifier::checkReceivedException PHP Method

checkReceivedException() public method

Checks if the subject received an exception of the supplied type.
public checkReceivedException ( Eloquent\Phony\Matcher\Matcher | Exceptio\Exception | Erro\Error | string | null $type = null ) : Eloquent\Phony\Event\EventCollection | null
$type Eloquent\Phony\Matcher\Matcher | Exceptio\Exception | Erro\Error | string | null An exception to match, the type of exception, or null for any exception.
return Eloquent\Phony\Event\EventCollection | null The result.
    public function checkReceivedException($type = null)
    {
        $cardinality = $this->resetCardinality();
        $isCall = $this->subject instanceof Call;
        $matchingEvents = array();
        $matchCount = 0;
        $eventCount = 0;
        $isTypeSupported = false;
        if (!$type) {
            $isTypeSupported = true;
            foreach ($this->calls as $call) {
                $isMatchingCall = false;
                foreach ($call->iterableEvents() as $event) {
                    if ($event instanceof ReceivedExceptionEvent) {
                        ++$eventCount;
                        $matchingEvents[] = $event;
                        $isMatchingCall = true;
                        if ($isCall) {
                            ++$matchCount;
                        }
                    }
                }
                if (!$isCall && $isMatchingCall) {
                    ++$matchCount;
                }
            }
        } elseif (is_string($type)) {
            $isTypeSupported = true;
            foreach ($this->calls as $call) {
                $isMatchingCall = false;
                foreach ($call->iterableEvents() as $event) {
                    if ($event instanceof ReceivedExceptionEvent) {
                        ++$eventCount;
                        if (is_a($event->exception(), $type)) {
                            $matchingEvents[] = $event;
                            $isMatchingCall = true;
                            if ($isCall) {
                                ++$matchCount;
                            }
                        }
                    }
                }
                if (!$isCall && $isMatchingCall) {
                    ++$matchCount;
                }
            }
        } elseif (is_object($type)) {
            if ($type instanceof Throwable || $type instanceof Exception) {
                $isTypeSupported = true;
                $type = $this->matcherFactory->equalTo($type, true);
            } elseif ($this->matcherFactory->isMatcher($type)) {
                $isTypeSupported = true;
                $type = $this->matcherFactory->adapt($type);
            }
            if ($isTypeSupported) {
                foreach ($this->calls as $call) {
                    $isMatchingCall = false;
                    foreach ($call->iterableEvents() as $event) {
                        if ($event instanceof ReceivedExceptionEvent) {
                            ++$eventCount;
                            if ($type->matches($event->exception())) {
                                $matchingEvents[] = $event;
                                $isMatchingCall = true;
                                if ($isCall) {
                                    ++$matchCount;
                                }
                            }
                        }
                    }
                    if (!$isCall && $isMatchingCall) {
                        ++$matchCount;
                    }
                }
            }
        }
        if (!$isTypeSupported) {
            throw new InvalidArgumentException(sprintf('Unable to match exceptions against %s.', $this->assertionRenderer->renderValue($type)));
        }
        if ($isCall) {
            $totalCount = $eventCount;
        } else {
            $totalCount = $this->callCount;
        }
        if ($cardinality->matches($matchCount, $totalCount)) {
            return $this->assertionRecorder->createSuccess($matchingEvents);
        }
    }