diff --git a/README.md b/README.md index 08f1ebd..c7fb628 100644 --- a/README.md +++ b/README.md @@ -41,17 +41,6 @@ $this->assertExceptionThrown(\InvalidArgumentException::class, function () : voi }); ``` -## assertNoExceptionsThrown - -Asserts that no exceptions was thrown during the execution of the callback, -if any exceptions was thrown during the execution the assertion fails. - -```php -$this->assertNoExceptionsThrown(function () : void { - // code that should not throw any exceptions -}); -``` - ## assertExceptionNotThrown Asserts that a specific exception was not thrown during the execution of the callback, @@ -59,9 +48,21 @@ if the specified exception was thrown during execution the assertion fails. **use with caution** - this will only assert that a specific exception was not thrown and the assertion will pass for any other exceptions thrown, intentional or accidental. +In most cases it is probably better to `assertNoExceptionsThrown` instead. ```php $this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void { // code that should not throw the exception }); ``` + +## assertNoExceptionsThrown + +Asserts that no exceptions was thrown during the execution of the callback, +if any exceptions was thrown during the execution the assertion fails. + +```php +$this->assertNoExceptionsThrown(function () : void { + // code that should not throw any exceptions +}); +``` diff --git a/composer.json b/composer.json index d24ef2c..23728af 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,19 @@ "php": "^7.4|^8.0", "phpunit/phpunit": "^9.5" }, + "require-dev": { + "symfony/var-dumper": "^5.2" + }, "autoload": { "psr-4": { "Devlop\\PHPUnit\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Devlop\\Buffer\\Tests\\": "tests/" + } + }, "minimum-stability": "dev", "prefer-stable": true, "extra": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..8c8b315 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/src/ExceptionAssertions.php b/src/ExceptionAssertions.php index cfa1589..7713159 100644 --- a/src/ExceptionAssertions.php +++ b/src/ExceptionAssertions.php @@ -4,6 +4,7 @@ namespace Devlop\PHPUnit; +use PHPUnit\Framework\Assert; use Throwable; trait ExceptionAssertions @@ -16,7 +17,7 @@ trait ExceptionAssertions * @param callable $callback * @return void */ - public function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : Throwable + public static function assertExceptionThrown(string $expectedException, callable $callback, ?callable $validator = null) : void { $thrownException = null; @@ -26,7 +27,7 @@ public function assertExceptionThrown(string $expectedException, callable $callb $thrownException = $e; } - $this->assertInstanceOf( + Assert::assertInstanceOf( $expectedException, $thrownException, $thrownException === null @@ -41,8 +42,6 @@ public function assertExceptionThrown(string $expectedException, callable $callb if ($validator !== null) { $validator($thrownException); } - - return $thrownException; } /** @@ -54,7 +53,7 @@ public function assertExceptionThrown(string $expectedException, callable $callb * @param callable $callback * @return void */ - public function assertExceptionNotThrown(string $expectedException, callable $callback) : void + public static function assertExceptionNotThrown(string $expectedException, callable $callback) : void { $thrownException = null; @@ -64,7 +63,7 @@ public function assertExceptionNotThrown(string $expectedException, callable $ca $thrownException = $e; } - $this->assertNotInstanceOf( + Assert::assertNotInstanceOf( $expectedException, $thrownException, \sprintf( @@ -80,7 +79,7 @@ public function assertExceptionNotThrown(string $expectedException, callable $ca * @param callable $callback * @return void */ - public function assertNoExceptionsThrown(callable $callback) : void + public static function assertNoExceptionsThrown(callable $callback) : void { $thrownException = null; @@ -90,7 +89,7 @@ public function assertNoExceptionsThrown(callable $callback) : void $thrownException = $e; } - $this->assertNull( + Assert::assertNull( $thrownException, \sprintf( 'Failed asserting that no exceptions was thrown, exception of type "%1$s was thrown.', diff --git a/tests/AssertExceptionNotThrownTest.php b/tests/AssertExceptionNotThrownTest.php new file mode 100644 index 0000000..bed5255 --- /dev/null +++ b/tests/AssertExceptionNotThrownTest.php @@ -0,0 +1,73 @@ +assertExceptionNotThrown(\RuntimeException::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertFalse($assertionPasses); + } + + function test_assert_exception_not_thrown_does_not_pass_if_parent_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionNotThrown(\Throwable::class, function () : void { + throw \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertFalse($assertionPasses); + } + + function test_assert_exception_not_thrown_passes_if_no_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionNotThrown(\RuntimeException::class, function () : void { + // do nothing + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertTrue($assertionPasses); + } + + function test_assert_exception_not_thrown_passes_if_another_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionNotThrown(\InvalidArgumentException::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertTrue($assertionPasses); + } +} diff --git a/tests/AssertExceptionThrownTest.php b/tests/AssertExceptionThrownTest.php new file mode 100644 index 0000000..f5a750e --- /dev/null +++ b/tests/AssertExceptionThrownTest.php @@ -0,0 +1,73 @@ +assertExceptionThrown(\Throwable::class, function () : void { + // do nothing + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertFalse($assertionPasses); + } + + function test_assert_exception_thrown_does_not_pass_if_another_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionThrown(\InvalidArgumentException::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertFalse($assertionPasses); + } + + function test_assert_exception_thrown_passes_if_the_correct_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionThrown(\RuntimeException::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertTrue($assertionPasses); + } + + function test_assert_exception_thrown_passes_if_parent_exception_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertExceptionThrown(\Throwable::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertTrue($assertionPasses); + } +} diff --git a/tests/AssertNoExceptionsThrownTest.php b/tests/AssertNoExceptionsThrownTest.php new file mode 100644 index 0000000..cec09cd --- /dev/null +++ b/tests/AssertNoExceptionsThrownTest.php @@ -0,0 +1,43 @@ +assertNoExceptionsThrown(function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertFalse($assertionPasses); + } + + function test_assert_no_exceptions_thrown_passes_if_no_exceptions_was_thrown() : void + { + $assertionPasses = true; + + try { + $this->assertNoExceptionsThrown(function () : void { + // do nothing + }); + } catch (\PHPUnit\Framework\Exception $exception) { + $assertionPasses = false; + } + + $this->assertTrue($assertionPasses); + } +} diff --git a/tests/ExceptionAssertionsTest.php b/tests/ExceptionAssertionsTest.php new file mode 100644 index 0000000..d4c5a4a --- /dev/null +++ b/tests/ExceptionAssertionsTest.php @@ -0,0 +1,125 @@ +assertTrue($staticallyAccessible, 'Method assertExceptionThrown is not static.'); + } + + function test_assert_exception_thrown_should_not_require_the_using_class_to_implement_dependant_phpunit_methods() : void + { + $class = new class { + use ExceptionAssertions; + }; + + $executionFailed = false; + + try { + $class::assertExceptionThrown(\Throwable::class, function () : void { + throw new \RuntimeException('Oh?'); + }); + } catch (\Throwable $e) { + $executionFailed = true; + } + + $this->assertFalse($executionFailed, 'Method assertExceptionThrown does not working without the using class implementing dependant methods.'); + } + + function test_assert_exception_not_thrown_should_be_statically_accessible() : void + { + $class = new class extends TestCase { + use ExceptionAssertions; + }; + + $staticallyAccessible = true; + + try { + $class::assertExceptionNotThrown(\Throwable::class, function () : void { + // + }); + } catch (\Throwable $e) { + $staticallyAccessible = false; + } + + $this->assertTrue($staticallyAccessible, 'Method assertExceptionNotThrown is not static.'); + } + + function test_assert_exception_not_thrown_should_not_require_the_using_class_to_implement_dependant_phpunit_methods() : void + { + $class = new class { + use ExceptionAssertions; + }; + + $executionFailed = false; + + try { + $class::assertExceptionNotThrown(\Throwable::class, function () : void { + // + }); + } catch (\Throwable $e) { + $executionFailed = true; + } + + $this->assertFalse($executionFailed, 'Method assertExceptionNotThrown does not working without the using class implementing dependant methods.'); + } + + function test_assert_no_exceptions_thrown_should_be_statically_accessible() : void + { + $class = new class extends TestCase { + use ExceptionAssertions; + }; + + $staticallyAccessible = true; + + try { + $class::assertNoExceptionsThrown(function () : void { + // do nothing + }); + } catch (\Throwable $e) { + $staticallyAccessible = false; + } + + $this->assertTrue($staticallyAccessible, 'Method assertNoExceptionsThrown is not static.'); + } + + function test_assert_no_exceptions_thrown_should_not_require_the_using_class_to_implement_dependant_phpunit_methods() : void + { + $class = new class { + use ExceptionAssertions; + }; + + $executionFailed = false; + + try { + $class::assertNoExceptionsThrown(function () : void { + // do nothing + }); + } catch (\Throwable $e) { + $executionFailed = true; + } + + $this->assertFalse($executionFailed, 'Method assertNoExceptionsThrown does not working without the using class implementing dependant methods.'); + } +}