From 350a193ca3704180cad469bf4b8990956077e813 Mon Sep 17 00:00:00 2001 From: Johan Date: Mon, 22 Feb 2021 17:09:52 +0700 Subject: [PATCH] Initial Commit --- .editorconfig | 9 ++++ .gitattributes | 8 +++ .gitignore | 4 ++ LICENSE.md | 21 ++++++++ README.md | 16 ++++++ composer.json | 30 +++++++++++ src/ExceptionAssertions.php | 101 ++++++++++++++++++++++++++++++++++++ 7 files changed, 189 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/ExceptionAssertions.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..cf80b4e --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..88c3871 --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..67dd1b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.lock +.phpunit.result.cache +.DS_Store diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..bed3adf --- /dev/null +++ b/LICENSE.md @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..78f7bdf --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +

+ Latest Stable Version + License +

+ +# PHPUnit Exception Assertions + +... + +# Installation + +... + +# Usage + +... diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ac3227a --- /dev/null +++ b/composer.json @@ -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" + } + } +} diff --git a/src/ExceptionAssertions.php b/src/ExceptionAssertions.php new file mode 100644 index 0000000..5086390 --- /dev/null +++ b/src/ExceptionAssertions.php @@ -0,0 +1,101 @@ +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), + ), + ); + } +}