|
| 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