1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests\Unit\Middleware; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Shoot\Shoot\Middleware\SuppressionMiddleware; |
11
|
|
|
use Shoot\Shoot\MiddlewareInterface; |
12
|
|
|
use Shoot\Shoot\SuppressedException; |
13
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
14
|
|
|
use Shoot\Shoot\View; |
15
|
|
|
|
16
|
|
|
final class SuppressionMiddlewareTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var MiddlewareInterface */ |
19
|
|
|
private $middleware; |
20
|
|
|
|
21
|
|
|
/** @var callable */ |
22
|
|
|
private $next; |
23
|
|
|
|
24
|
|
|
/** @var ServerRequestInterface|MockObject */ |
25
|
|
|
private $request; |
26
|
|
|
|
27
|
|
|
protected function setUp(): void |
28
|
|
|
{ |
29
|
|
|
$this->middleware = new SuppressionMiddleware(); |
30
|
|
|
|
31
|
|
|
$this->next = function (View $view): View { |
32
|
|
|
$view->render(); |
33
|
|
|
|
34
|
|
|
return $view; |
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
38
|
|
|
|
39
|
|
|
parent::setUp(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testShouldCatchSuppressedExceptionAndAssignToView(): void |
43
|
|
|
{ |
44
|
|
|
$view = ViewFactory::createWithCallback(function () { |
45
|
|
|
throw new SuppressedException(new Exception()); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
$this->assertFalse($view->hasSuppressedException()); |
49
|
|
|
|
50
|
|
|
$view = $this->middleware->process($view, $this->request, $this->next); |
51
|
|
|
|
52
|
|
|
$this->assertTrue($view->hasSuppressedException()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testShouldIgnoreAllOtherExceptions(): void |
56
|
|
|
{ |
57
|
|
|
$view = ViewFactory::createWithCallback(function () { |
58
|
|
|
throw new Exception(); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$this->expectException(Exception::class); |
62
|
|
|
|
63
|
|
|
$this->middleware->process($view, $this->request, $this->next); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|