Skip to content

Commit f1da0d7

Browse files
committed
add tests and update readme
1 parent f638247 commit f1da0d7

8 files changed

+358
-19
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,28 @@ $this->assertExceptionThrown(\InvalidArgumentException::class, function () : voi
4141
});
4242
```
4343

44-
## assertNoExceptionsThrown
45-
46-
Asserts that no exceptions was thrown during the execution of the callback,
47-
if any exceptions was thrown during the execution the assertion fails.
48-
49-
```php
50-
$this->assertNoExceptionsThrown(function () : void {
51-
// code that should not throw any exceptions
52-
});
53-
```
54-
5544
## assertExceptionNotThrown
5645

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

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

6353
```php
6454
$this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void {
6555
// code that should not throw the exception
6656
});
6757
```
58+
59+
## assertNoExceptionsThrown
60+
61+
Asserts that no exceptions was thrown during the execution of the callback,
62+
if any exceptions was thrown during the execution the assertion fails.
63+
64+
```php
65+
$this->assertNoExceptionsThrown(function () : void {
66+
// code that should not throw any exceptions
67+
});
68+
```

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313
"php": "^7.4|^8.0",
1414
"phpunit/phpunit": "^9.5"
1515
},
16+
"require-dev": {
17+
"symfony/var-dumper": "^5.2"
18+
},
1619
"autoload": {
1720
"psr-4": {
1821
"Devlop\\PHPUnit\\": "src/"
1922
}
2023
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Devlop\\Buffer\\Tests\\": "tests/"
27+
}
28+
},
2129
"minimum-stability": "dev",
2230
"prefer-stable": true,
2331
"extra": {

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
verbose="true">
7+
<testsuites>
8+
<testsuite name="devlop/phpunit-exception-assertions Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</coverage>
17+
</phpunit>

src/ExceptionAssertions.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Devlop\PHPUnit;
66

7+
use PHPUnit\Framework\Assert;
78
use Throwable;
89

910
trait ExceptionAssertions
@@ -16,7 +17,7 @@ trait ExceptionAssertions
1617
* @param callable $callback
1718
* @return void
1819
*/
19-
public function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : Throwable
20+
public static function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : void
2021
{
2122
$thrownException = null;
2223

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

29-
$this->assertInstanceOf(
30+
Assert::assertInstanceOf(
3031
$expectedException,
3132
$thrownException,
3233
$thrownException === null
@@ -41,8 +42,6 @@ public function assertExceptionThrown(string $expectedException, callable $callb
4142
if ($validator !== null) {
4243
$validator($thrownException);
4344
}
44-
45-
return $thrownException;
4645
}
4746

4847
/**
@@ -54,7 +53,7 @@ public function assertExceptionThrown(string $expectedException, callable $callb
5453
* @param callable $callback
5554
* @return void
5655
*/
57-
public function assertExceptionNotThrown(string $expectedException, callable $callback) : void
56+
public static function assertExceptionNotThrown(string $expectedException, callable $callback) : void
5857
{
5958
$thrownException = null;
6059

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

67-
$this->assertNotInstanceOf(
66+
Assert::assertNotInstanceOf(
6867
$expectedException,
6968
$thrownException,
7069
\sprintf(
@@ -80,7 +79,7 @@ public function assertExceptionNotThrown(string $expectedException, callable $ca
8079
* @param callable $callback
8180
* @return void
8281
*/
83-
public function assertNoExceptionsThrown(callable $callback) : void
82+
public static function assertNoExceptionsThrown(callable $callback) : void
8483
{
8584
$thrownException = null;
8685

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

93-
$this->assertNull(
92+
Assert::assertNull(
9493
$thrownException,
9594
\sprintf(
9695
'Failed asserting that no exceptions was thrown, exception of type "%1$s was thrown.',
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\PHPUnit\Tests;
6+
7+
use Devlop\PHPUnit\ExceptionAssertions;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class AssertExceptionNotThrownTest extends TestCase
11+
{
12+
use ExceptionAssertions;
13+
14+
function test_assert_exception_not_thrown_does_not_pass_if_the_exception_was_thrown() : void
15+
{
16+
$assertionPasses = true;
17+
18+
try {
19+
$this->assertExceptionNotThrown(\RuntimeException::class, function () : void {
20+
throw new \RuntimeException('Oh?');
21+
});
22+
} catch (\PHPUnit\Framework\Exception $exception) {
23+
$assertionPasses = false;
24+
}
25+
26+
$this->assertFalse($assertionPasses);
27+
}
28+
29+
function test_assert_exception_not_thrown_does_not_pass_if_parent_exception_was_thrown() : void
30+
{
31+
$assertionPasses = true;
32+
33+
try {
34+
$this->assertExceptionNotThrown(\Throwable::class, function () : void {
35+
throw \RuntimeException('Oh?');
36+
});
37+
} catch (\PHPUnit\Framework\Exception $exception) {
38+
$assertionPasses = false;
39+
}
40+
41+
$this->assertFalse($assertionPasses);
42+
}
43+
44+
function test_assert_exception_not_thrown_passes_if_no_exception_was_thrown() : void
45+
{
46+
$assertionPasses = true;
47+
48+
try {
49+
$this->assertExceptionNotThrown(\RuntimeException::class, function () : void {
50+
// do nothing
51+
});
52+
} catch (\PHPUnit\Framework\Exception $exception) {
53+
$assertionPasses = false;
54+
}
55+
56+
$this->assertTrue($assertionPasses);
57+
}
58+
59+
function test_assert_exception_not_thrown_passes_if_another_exception_was_thrown() : void
60+
{
61+
$assertionPasses = true;
62+
63+
try {
64+
$this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void {
65+
throw new \RuntimeException('Oh?');
66+
});
67+
} catch (\PHPUnit\Framework\Exception $exception) {
68+
$assertionPasses = false;
69+
}
70+
71+
$this->assertTrue($assertionPasses);
72+
}
73+
}

tests/AssertExceptionThrownTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\PHPUnit\Tests;
6+
7+
use Devlop\PHPUnit\ExceptionAssertions;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class AssertExceptionThrownTest extends TestCase
11+
{
12+
use ExceptionAssertions;
13+
14+
function test_assert_exception_thrown_does_not_pass_if_no_exception_was_thrown() : void
15+
{
16+
$assertionPasses = true;
17+
18+
try {
19+
$this->assertExceptionThrown(\Throwable::class, function () : void {
20+
// do nothing
21+
});
22+
} catch (\PHPUnit\Framework\Exception $exception) {
23+
$assertionPasses = false;
24+
}
25+
26+
$this->assertFalse($assertionPasses);
27+
}
28+
29+
function test_assert_exception_thrown_does_not_pass_if_another_exception_was_thrown() : void
30+
{
31+
$assertionPasses = true;
32+
33+
try {
34+
$this->assertExceptionThrown(\InvalidArgumentException::class, function () : void {
35+
throw new \RuntimeException('Oh?');
36+
});
37+
} catch (\PHPUnit\Framework\Exception $exception) {
38+
$assertionPasses = false;
39+
}
40+
41+
$this->assertFalse($assertionPasses);
42+
}
43+
44+
function test_assert_exception_thrown_passes_if_the_correct_exception_was_thrown() : void
45+
{
46+
$assertionPasses = true;
47+
48+
try {
49+
$this->assertExceptionThrown(\RuntimeException::class, function () : void {
50+
throw new \RuntimeException('Oh?');
51+
});
52+
} catch (\PHPUnit\Framework\Exception $exception) {
53+
$assertionPasses = false;
54+
}
55+
56+
$this->assertTrue($assertionPasses);
57+
}
58+
59+
function test_assert_exception_thrown_passes_if_parent_exception_was_thrown() : void
60+
{
61+
$assertionPasses = true;
62+
63+
try {
64+
$this->assertExceptionThrown(\Throwable::class, function () : void {
65+
throw new \RuntimeException('Oh?');
66+
});
67+
} catch (\PHPUnit\Framework\Exception $exception) {
68+
$assertionPasses = false;
69+
}
70+
71+
$this->assertTrue($assertionPasses);
72+
}
73+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\PHPUnit\Tests;
6+
7+
use Devlop\PHPUnit\ExceptionAssertions;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class AssertNoExceptionsThrownTest extends TestCase
11+
{
12+
use ExceptionAssertions;
13+
14+
function test_assert_no_exceptions_thrown_does_not_pass_if_any_exception_was_thrown() : void
15+
{
16+
$assertionPasses = true;
17+
18+
try {
19+
$this->assertNoExceptionsThrown(function () : void {
20+
throw new \RuntimeException('Oh?');
21+
});
22+
} catch (\PHPUnit\Framework\Exception $exception) {
23+
$assertionPasses = false;
24+
}
25+
26+
$this->assertFalse($assertionPasses);
27+
}
28+
29+
function test_assert_no_exceptions_thrown_passes_if_no_exceptions_was_thrown() : void
30+
{
31+
$assertionPasses = true;
32+
33+
try {
34+
$this->assertNoExceptionsThrown(function () : void {
35+
// do nothing
36+
});
37+
} catch (\PHPUnit\Framework\Exception $exception) {
38+
$assertionPasses = false;
39+
}
40+
41+
$this->assertTrue($assertionPasses);
42+
}
43+
}

0 commit comments

Comments
 (0)