1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests\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
|
|
|
/** |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->middleware = new SuppressionMiddleware(); |
33
|
|
|
|
34
|
|
|
$this->next = function (View $view): View { |
35
|
|
|
$view->render(); |
36
|
|
|
|
37
|
|
|
return $view; |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function testShouldCatchSuppressedExceptionAndAssignToView() |
47
|
|
|
{ |
48
|
|
|
$view = ViewFactory::createWithCallback(function () { |
49
|
|
|
throw new SuppressedException(new Exception()); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$this->assertFalse($view->hasSuppressedException()); |
53
|
|
|
|
54
|
|
|
$view = $this->middleware->process($view, $this->request, $this->next); |
55
|
|
|
|
56
|
|
|
$this->assertTrue($view->hasSuppressedException()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function testShouldIgnoreAllOtherExceptions() |
63
|
|
|
{ |
64
|
|
|
$view = ViewFactory::createWithCallback(function () { |
65
|
|
|
throw new Exception(); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->expectException(Exception::class); |
69
|
|
|
|
70
|
|
|
$this->middleware->process($view, $this->request, $this->next); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|