Skip to content

Commit 350a193

Browse files
committed
Initial Commit
0 parents  commit 350a193

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.md diff=markdown
2+
*.php diff=php
3+
4+
/.editorconfig export-ignore
5+
/.gitignore export-ignore
6+
/.gitattributes export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/tests export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.phpunit.result.cache
4+
.DS_Store

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Johan Rosenson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<p align="center">
2+
<a href="https://packagist.org/packages/devlop/phpunit-exception-assertions"><img src="https://img.shields.io/packagist/v/devlop/phpunit-exception-assertions" alt="Latest Stable Version"></a>
3+
<a href="https://github.com/devlop-ab/phpunit-exception-assertions/blob/master/LICENSE.md"><img src="https://img.shields.io/packagist/l/devlop/phpunit-exception-assertions" alt="License"></a>
4+
</p>
5+
6+
# PHPUnit Exception Assertions
7+
8+
...
9+
10+
# Installation
11+
12+
...
13+
14+
# Usage
15+
16+
...

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "devlop/phpunit-exception-assertions",
3+
"description": "PHPUnit assertions for exceptions.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Johan Rosenson",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.4|^8.0"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^9.5"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Devlop\\PHPUnit\\": "src/"
21+
}
22+
},
23+
"minimum-stability": "dev",
24+
"prefer-stable": true,
25+
"extra": {
26+
"branch-alias": {
27+
"dev-master": "1.x-dev"
28+
}
29+
}
30+
}

src/ExceptionAssertions.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\PHPUnit;
6+
7+
use Throwable;
8+
9+
trait ExceptionAssertions
10+
{
11+
/**
12+
* Assert that a specific exception was thrown during executing of the callback
13+
* Returns the thrown exception if additional assertions needs to be done
14+
*
15+
* @param class-string $expectedException
16+
* @param callable $callback
17+
* @return void
18+
*/
19+
public function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : Throwable
20+
{
21+
$thrownException = null;
22+
23+
try {
24+
$callback();
25+
} catch (Throwable $e) {
26+
$thrownException = $e;
27+
}
28+
29+
$this->assertInstanceOf(
30+
$expectedException,
31+
$thrownException,
32+
$thrownException === null
33+
? \sprintf('Failed asserting that an exception of type "%1$s" was thrown, no exception was thrown.', $expectedException)
34+
: \sprintf(
35+
'Failed asserting that an exception of type "%1$s" was thrown, the thrown exception was of type "%2$s".',
36+
$expectedException,
37+
\get_class($thrownException),
38+
),
39+
);
40+
41+
if ($validator !== null) {
42+
$validator($thrownException);
43+
}
44+
45+
return $thrownException;
46+
}
47+
48+
/**
49+
* Assert that a specific exception was not thrown during execution of the callback
50+
* Assertion will pass if no exceptions was thrown or an exception
51+
* was thrown but not instanceof $expectedException
52+
*
53+
* @param class-string $expectedException
54+
* @param callable $callback
55+
* @return void
56+
*/
57+
public function assertExceptionNotThrown(string $expectedException, callable $callback) : void
58+
{
59+
$thrownException = null;
60+
61+
try {
62+
$callback();
63+
} catch (Throwable $e) {
64+
$thrownException = $e;
65+
}
66+
67+
$this->assertNotInstanceOf(
68+
$expectedException,
69+
$thrownException,
70+
\sprintf(
71+
'Failed asserting that an exception of type "%1$s" was not thrown.',
72+
$expectedException,
73+
),
74+
);
75+
}
76+
77+
/**
78+
* Assert that no exceptions was thrown during execution of the callback
79+
*
80+
* @param callable $callback
81+
* @return void
82+
*/
83+
public function assertNoExceptionsThrown(callable $callback) : void
84+
{
85+
$thrownException = null;
86+
87+
try {
88+
$callback();
89+
} catch (Throwable $e) {
90+
$thrownException = $e;
91+
}
92+
93+
$this->assertNull(
94+
$thrownException,
95+
\sprintf(
96+
'Failed asserting that no exceptions was thrown, exception of type "%1$s was thrown.',
97+
\get_class($thrownException),
98+
),
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)