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 Psr\Log\LoggerInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Shoot\Shoot\Middleware\LoggingMiddleware; |
13
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
14
|
|
|
use Shoot\Shoot\View; |
15
|
|
|
|
16
|
|
|
final class LoggingMiddlewareTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var callable */ |
19
|
|
|
private $next; |
20
|
|
|
|
21
|
|
|
/** @var ServerRequestInterface|MockObject */ |
22
|
|
|
private $request; |
23
|
|
|
|
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
27
|
|
|
$this->next = function (View $view): View { |
28
|
|
|
return $view; |
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
parent::setUp(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testShouldLogBasicDebugInformation(): void |
35
|
|
|
{ |
36
|
|
|
$view = ViewFactory::create(); |
37
|
|
|
|
38
|
|
|
/** @var LoggerInterface|MockObject $logger */ |
39
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
40
|
|
|
$logger |
41
|
|
|
->expects($this->once()) |
42
|
|
|
->method('debug') |
43
|
|
|
->with( |
44
|
|
|
$this->equalTo('template.twig'), |
45
|
|
|
$this->callback(function (array $context): bool { |
46
|
|
|
$diff = array_diff( |
47
|
|
|
array_keys($context), |
48
|
|
|
['presentation_model', 'presenter_name', 'time_taken', 'variables'] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return count($diff) === 0; |
52
|
|
|
}) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$middleware = new LoggingMiddleware($logger); |
56
|
|
|
$middleware->process($view, $this->request, $this->next); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testShouldLogSuppressedExceptions(): void |
60
|
|
|
{ |
61
|
|
|
$view = ViewFactory::create()->withSuppressedException(new RuntimeException()); |
62
|
|
|
|
63
|
|
|
/** @var LoggerInterface|MockObject $logger */ |
64
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
65
|
|
|
$logger |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('warning') |
68
|
|
|
->with( |
69
|
|
|
$this->equalTo('template.twig'), |
70
|
|
|
$this->arrayHasKey('exception') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$middleware = new LoggingMiddleware($logger); |
74
|
|
|
$middleware->process($view, $this->request, $this->next); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testShouldLogUncaughtExceptions(): void |
78
|
|
|
{ |
79
|
|
|
$view = ViewFactory::create(); |
80
|
|
|
|
81
|
|
|
$next = function () { |
82
|
|
|
throw new Exception(); |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
/** @var LoggerInterface|MockObject $logger */ |
86
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
87
|
|
|
$logger |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('error') |
90
|
|
|
->with( |
91
|
|
|
$this->equalTo('template.twig'), |
92
|
|
|
$this->arrayHasKey('exception') |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->expectException(Exception::class); |
96
|
|
|
|
97
|
|
|
$middleware = new LoggingMiddleware($logger); |
98
|
|
|
$middleware->process($view, $this->request, $next); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|