Skip to content

Commit

Permalink
add tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
johanrosenson committed Feb 23, 2021
1 parent f638247 commit f1da0d7
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 19 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,28 @@ $this->assertExceptionThrown(\InvalidArgumentException::class, function () : voi
});
```

## assertNoExceptionsThrown

Asserts that no exceptions was thrown during the execution of the callback,
if any exceptions was thrown during the execution the assertion fails.

```php
$this->assertNoExceptionsThrown(function () : void {
// code that should not throw any exceptions
});
```

## assertExceptionNotThrown

Asserts that a specific exception was not thrown during the execution of the callback,
if the specified exception was thrown during execution the assertion fails.

**use with caution** - this will only assert that a specific exception was not thrown and
the assertion will pass for any other exceptions thrown, intentional or accidental.
In most cases it is probably better to `assertNoExceptionsThrown` instead.

```php
$this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void {
// code that should not throw the exception
});
```

## assertNoExceptionsThrown

Asserts that no exceptions was thrown during the execution of the callback,
if any exceptions was thrown during the execution the assertion fails.

```php
$this->assertNoExceptionsThrown(function () : void {
// code that should not throw any exceptions
});
```
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
"php": "^7.4|^8.0",
"phpunit/phpunit": "^9.5"
},
"require-dev": {
"symfony/var-dumper": "^5.2"
},
"autoload": {
"psr-4": {
"Devlop\\PHPUnit\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Devlop\\Buffer\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
Expand Down
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="devlop/phpunit-exception-assertions Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
15 changes: 7 additions & 8 deletions src/ExceptionAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Devlop\PHPUnit;

use PHPUnit\Framework\Assert;
use Throwable;

trait ExceptionAssertions
Expand All @@ -16,7 +17,7 @@ trait ExceptionAssertions
* @param callable $callback
* @return void
*/
public function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : Throwable
public static function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : void
{
$thrownException = null;

Expand All @@ -26,7 +27,7 @@ public function assertExceptionThrown(string $expectedException, callable $callb
$thrownException = $e;
}

$this->assertInstanceOf(
Assert::assertInstanceOf(
$expectedException,
$thrownException,
$thrownException === null
Expand All @@ -41,8 +42,6 @@ public function assertExceptionThrown(string $expectedException, callable $callb
if ($validator !== null) {
$validator($thrownException);
}

return $thrownException;
}

/**
Expand All @@ -54,7 +53,7 @@ public function assertExceptionThrown(string $expectedException, callable $callb
* @param callable $callback
* @return void
*/
public function assertExceptionNotThrown(string $expectedException, callable $callback) : void
public static function assertExceptionNotThrown(string $expectedException, callable $callback) : void
{
$thrownException = null;

Expand All @@ -64,7 +63,7 @@ public function assertExceptionNotThrown(string $expectedException, callable $ca
$thrownException = $e;
}

$this->assertNotInstanceOf(
Assert::assertNotInstanceOf(
$expectedException,
$thrownException,
\sprintf(
Expand All @@ -80,7 +79,7 @@ public function assertExceptionNotThrown(string $expectedException, callable $ca
* @param callable $callback
* @return void
*/
public function assertNoExceptionsThrown(callable $callback) : void
public static function assertNoExceptionsThrown(callable $callback) : void
{
$thrownException = null;

Expand All @@ -90,7 +89,7 @@ public function assertNoExceptionsThrown(callable $callback) : void
$thrownException = $e;
}

$this->assertNull(
Assert::assertNull(
$thrownException,
\sprintf(
'Failed asserting that no exceptions was thrown, exception of type "%1$s was thrown.',
Expand Down
73 changes: 73 additions & 0 deletions tests/AssertExceptionNotThrownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Devlop\PHPUnit\Tests;

use Devlop\PHPUnit\ExceptionAssertions;
use PHPUnit\Framework\TestCase;

final class AssertExceptionNotThrownTest extends TestCase
{
use ExceptionAssertions;

function test_assert_exception_not_thrown_does_not_pass_if_the_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionNotThrown(\RuntimeException::class, function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertFalse($assertionPasses);
}

function test_assert_exception_not_thrown_does_not_pass_if_parent_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionNotThrown(\Throwable::class, function () : void {
throw \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertFalse($assertionPasses);
}

function test_assert_exception_not_thrown_passes_if_no_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionNotThrown(\RuntimeException::class, function () : void {
// do nothing
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertTrue($assertionPasses);
}

function test_assert_exception_not_thrown_passes_if_another_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertTrue($assertionPasses);
}
}
73 changes: 73 additions & 0 deletions tests/AssertExceptionThrownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Devlop\PHPUnit\Tests;

use Devlop\PHPUnit\ExceptionAssertions;
use PHPUnit\Framework\TestCase;

final class AssertExceptionThrownTest extends TestCase
{
use ExceptionAssertions;

function test_assert_exception_thrown_does_not_pass_if_no_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionThrown(\Throwable::class, function () : void {
// do nothing
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertFalse($assertionPasses);
}

function test_assert_exception_thrown_does_not_pass_if_another_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionThrown(\InvalidArgumentException::class, function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertFalse($assertionPasses);
}

function test_assert_exception_thrown_passes_if_the_correct_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionThrown(\RuntimeException::class, function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertTrue($assertionPasses);
}

function test_assert_exception_thrown_passes_if_parent_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertExceptionThrown(\Throwable::class, function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertTrue($assertionPasses);
}
}
43 changes: 43 additions & 0 deletions tests/AssertNoExceptionsThrownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Devlop\PHPUnit\Tests;

use Devlop\PHPUnit\ExceptionAssertions;
use PHPUnit\Framework\TestCase;

final class AssertNoExceptionsThrownTest extends TestCase
{
use ExceptionAssertions;

function test_assert_no_exceptions_thrown_does_not_pass_if_any_exception_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertNoExceptionsThrown(function () : void {
throw new \RuntimeException('Oh?');
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertFalse($assertionPasses);
}

function test_assert_no_exceptions_thrown_passes_if_no_exceptions_was_thrown() : void
{
$assertionPasses = true;

try {
$this->assertNoExceptionsThrown(function () : void {
// do nothing
});
} catch (\PHPUnit\Framework\Exception $exception) {
$assertionPasses = false;
}

$this->assertTrue($assertionPasses);
}
}
Loading

0 comments on commit f1da0d7

Please sign in to comment.