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);
}
}