|
10 | 10 | use Illuminate\Contracts\View\Factory as ViewFactory;
|
11 | 11 | use Illuminate\Database\RecordsNotFoundException;
|
12 | 12 | use Illuminate\Foundation\Exceptions\Handler;
|
| 13 | +use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling; |
13 | 14 | use Illuminate\Http\RedirectResponse;
|
14 | 15 | use Illuminate\Http\Request;
|
15 | 16 | use Illuminate\Routing\Redirector;
|
16 | 17 | use Illuminate\Routing\ResponseFactory;
|
17 | 18 | use Illuminate\Support\MessageBag;
|
| 19 | +use Illuminate\Testing\Assert; |
18 | 20 | use Illuminate\Validation\ValidationException;
|
19 | 21 | use Illuminate\Validation\Validator;
|
20 | 22 | use InvalidArgumentException;
|
21 | 23 | use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
22 | 24 | use Mockery as m;
|
23 | 25 | use OutOfRangeException;
|
| 26 | +use PHPUnit\Framework\AssertionFailedError; |
24 | 27 | use PHPUnit\Framework\TestCase;
|
25 | 28 | use Psr\Log\LoggerInterface;
|
26 | 29 | use Psr\Log\LogLevel;
|
|
34 | 37 | class FoundationExceptionsHandlerTest extends TestCase
|
35 | 38 | {
|
36 | 39 | use MockeryPHPUnitIntegration;
|
| 40 | + use InteractsWithExceptionHandling; |
37 | 41 |
|
38 | 42 | protected $config;
|
39 | 43 |
|
@@ -367,6 +371,54 @@ public function getErrorView($e)
|
367 | 371 |
|
368 | 372 | $this->assertNull($handler->getErrorView(new HttpException(404)));
|
369 | 373 | }
|
| 374 | + |
| 375 | + public function testAssertExceptionIsThrown() |
| 376 | + { |
| 377 | + $this->assertThrows(function () { |
| 378 | + throw new Exception; |
| 379 | + }); |
| 380 | + $this->assertThrows(function () { |
| 381 | + throw new CustomException; |
| 382 | + }); |
| 383 | + $this->assertThrows(function () { |
| 384 | + throw new CustomException; |
| 385 | + }, CustomException::class); |
| 386 | + $this->assertThrows(function () { |
| 387 | + throw new Exception('Some message.'); |
| 388 | + }, expectedMessage: 'Some message.'); |
| 389 | + $this->assertThrows(function () { |
| 390 | + throw new CustomException('Some message.'); |
| 391 | + }, expectedMessage: 'Some message.'); |
| 392 | + $this->assertThrows(function () { |
| 393 | + throw new CustomException('Some message.'); |
| 394 | + }, expectedClass: CustomException::class, expectedMessage: 'Some message.'); |
| 395 | + |
| 396 | + try { |
| 397 | + $this->assertThrows(function () { |
| 398 | + throw new Exception; |
| 399 | + }, CustomException::class); |
| 400 | + $testFailed = true; |
| 401 | + } catch (AssertionFailedError $exception) { |
| 402 | + $testFailed = false; |
| 403 | + } |
| 404 | + |
| 405 | + if ($testFailed) { |
| 406 | + Assert::fail('assertThrows failed: non matching exceptions are thrown.'); |
| 407 | + } |
| 408 | + |
| 409 | + try { |
| 410 | + $this->assertThrows(function () { |
| 411 | + throw new Exception('Some message.'); |
| 412 | + }, expectedClass: Exception::class, expectedMessage: 'Other message.'); |
| 413 | + $testFailed = true; |
| 414 | + } catch (AssertionFailedError $exception) { |
| 415 | + $testFailed = false; |
| 416 | + } |
| 417 | + |
| 418 | + if ($testFailed) { |
| 419 | + Assert::fail('assertThrows failed: non matching message are thrown.'); |
| 420 | + } |
| 421 | + } |
370 | 422 | }
|
371 | 423 |
|
372 | 424 | class CustomException extends Exception
|
|
0 commit comments