Skip to content

Commit

Permalink
'assertNoExceptionThrown' returns closure result
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Oct 13, 2021
1 parent fe00749 commit 9549d2b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
32 changes: 23 additions & 9 deletions src/AssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function assertExceptionThrown(
{
$exceptionResults = self::getThrownExceptionResult($closure);

if ($exceptionResults === null) {
if ($exceptionResults instanceof ClosureResult || $exceptionResults === null) {
throw new AssertionFailedError(
sprintf(
"Exception '%s' expected, none thrown!",
Expand All @@ -28,7 +28,7 @@ public static function assertExceptionThrown(
);
}

if ($exceptionResults->type !== $expectedType) {
if ($exceptionResults instanceof ThrownResult && $exceptionResults->type !== $expectedType) {
throw new AssertionFailedError(
sprintf(
"Exception '%s' expected, '%s' thrown!\nMessage: %s",
Expand All @@ -40,39 +40,53 @@ public static function assertExceptionThrown(

/**
* @param Closure $closure
* @return mixed
* @see Exceptions::assertNoExceptionThrown for more information!
*/
public static function assertNoExceptionThrown(
Closure $closure
): void
)
{
$exceptionResult = self::getThrownExceptionResult($closure);

if ($exceptionResult !== null) {
if ($exceptionResult instanceof ThrownResult) {
throw new AssertionFailedError(
sprintf(
"No exception expected, but '%s' was thrown!\nMessage: %s",
$exceptionResult->type, $exceptionResult->message
)
);
}

if($exceptionResult instanceof ClosureResult) {
return $exceptionResult->value;
}

return null;
}

// TODO: assertExceptionCode, assertExceptionMessage

/**
* @param Closure $closure
* @return ThrownResult|null
* @return ThrownResult|ClosureResult|null
*/
private static function getThrownExceptionResult(Closure $closure): ?ThrownResult
private static function getThrownExceptionResult(Closure $closure)
{
try {
$closure();
$closureResult = new ClosureResult();
$closureResult->value = $closure();

if($closureResult->value === null) {
// Hello PHPStan :) After I stop support for
// PHP 7.4 I can remove this if group...
return null;
}

return $closureResult;
} catch (Throwable $t) {
return ThrownResult::createFromThrowable($t);
}

return null;
}

}
9 changes: 9 additions & 0 deletions src/ClosureResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Zrnik\PHPUnit;

class ClosureResult
{
/** @var mixed $value */
public $value;
}
12 changes: 8 additions & 4 deletions src/Exceptions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Zrnik\PHPUnit;

Expand Down Expand Up @@ -33,19 +33,23 @@ public function assertExceptionThrown(
/**
* This method will run the closure and
* will fail the test, it ANY exception
* was thrown.
* was thrown. If no exceptions thrown,
* returns closure return data.
*
* @param Closure $closure
* @return mixed
*/
public function assertNoExceptionThrown(
Closure $closure
): void
)
{
AssertException::assertNoExceptionThrown(
$closureResult = AssertException::assertNoExceptionThrown(
$closure
);

$this->addToAssertionCount(1);

return $closureResult;
}

}

0 comments on commit 9549d2b

Please sign in to comment.