-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 350a193
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.md diff=markdown | ||
*.php diff=php | ||
|
||
/.editorconfig export-ignore | ||
/.gitignore export-ignore | ||
/.gitattributes export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.lock | ||
.phpunit.result.cache | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Johan Rosenson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<p align="center"> | ||
<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> | ||
<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> | ||
</p> | ||
|
||
# PHPUnit Exception Assertions | ||
|
||
... | ||
|
||
# Installation | ||
|
||
... | ||
|
||
# Usage | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "devlop/phpunit-exception-assertions", | ||
"description": "PHPUnit assertions for exceptions.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Johan Rosenson", | ||
"email": "johan@devlop.se" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4|^8.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Devlop\\PHPUnit\\": "src/" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devlop\PHPUnit; | ||
|
||
use Throwable; | ||
|
||
trait ExceptionAssertions | ||
{ | ||
/** | ||
* Assert that a specific exception was thrown during executing of the callback | ||
* Returns the thrown exception if additional assertions needs to be done | ||
* | ||
* @param class-string $expectedException | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : Throwable | ||
{ | ||
$thrownException = null; | ||
|
||
try { | ||
$callback(); | ||
} catch (Throwable $e) { | ||
$thrownException = $e; | ||
} | ||
|
||
$this->assertInstanceOf( | ||
$expectedException, | ||
$thrownException, | ||
$thrownException === null | ||
? \sprintf('Failed asserting that an exception of type "%1$s" was thrown, no exception was thrown.', $expectedException) | ||
: \sprintf( | ||
'Failed asserting that an exception of type "%1$s" was thrown, the thrown exception was of type "%2$s".', | ||
$expectedException, | ||
\get_class($thrownException), | ||
), | ||
); | ||
|
||
if ($validator !== null) { | ||
$validator($thrownException); | ||
} | ||
|
||
return $thrownException; | ||
} | ||
|
||
/** | ||
* Assert that a specific exception was not thrown during execution of the callback | ||
* Assertion will pass if no exceptions was thrown or an exception | ||
* was thrown but not instanceof $expectedException | ||
* | ||
* @param class-string $expectedException | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function assertExceptionNotThrown(string $expectedException, callable $callback) : void | ||
{ | ||
$thrownException = null; | ||
|
||
try { | ||
$callback(); | ||
} catch (Throwable $e) { | ||
$thrownException = $e; | ||
} | ||
|
||
$this->assertNotInstanceOf( | ||
$expectedException, | ||
$thrownException, | ||
\sprintf( | ||
'Failed asserting that an exception of type "%1$s" was not thrown.', | ||
$expectedException, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Assert that no exceptions was thrown during execution of the callback | ||
* | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function assertNoExceptionsThrown(callable $callback) : void | ||
{ | ||
$thrownException = null; | ||
|
||
try { | ||
$callback(); | ||
} catch (Throwable $e) { | ||
$thrownException = $e; | ||
} | ||
|
||
$this->assertNull( | ||
$thrownException, | ||
\sprintf( | ||
'Failed asserting that no exceptions was thrown, exception of type "%1$s was thrown.', | ||
\get_class($thrownException), | ||
), | ||
); | ||
} | ||
} |