Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johanrosenson committed Feb 22, 2021
0 parents commit 350a193
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
8 changes: 8 additions & 0 deletions .gitattributes
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.lock
.phpunit.result.cache
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
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
16 changes: 16 additions & 0 deletions README.md
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

...
30 changes: 30 additions & 0 deletions composer.json
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"
}
}
}
101 changes: 101 additions & 0 deletions src/ExceptionAssertions.php
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),
),
);
}
}

0 comments on commit 350a193

Please sign in to comment.