1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests\Middleware; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Shoot\Shoot\Middleware\LoggingMiddleware; |
12
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
13
|
|
|
use Shoot\Shoot\View; |
14
|
|
|
|
15
|
|
|
final class LoggingMiddlewareTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var callable */ |
18
|
|
|
private $next; |
19
|
|
|
|
20
|
|
|
/** @var ServerRequestInterface|MockObject */ |
21
|
|
|
private $request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
29
|
|
|
$this->next = function (View $view): View { |
30
|
|
|
return $view; |
31
|
|
|
}; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function testShouldLogBasicDebugInformation() |
38
|
|
|
{ |
39
|
|
|
$view = ViewFactory::create(); |
40
|
|
|
|
41
|
|
|
/** @var LoggerInterface|MockObject $logger */ |
42
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
43
|
|
|
$logger |
44
|
|
|
->expects($this->once()) |
45
|
|
|
->method('debug') |
46
|
|
|
->with( |
47
|
|
|
$this->equalTo('item.twig'), |
48
|
|
|
$this->callback(function (array $context): bool { |
49
|
|
|
$diff = array_diff( |
50
|
|
|
array_keys($context), |
51
|
|
|
['presentation_model', 'presenter_name', 'time_taken', 'variables'] |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
return count($diff) === 0; |
55
|
|
|
}) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$middleware = new LoggingMiddleware($logger); |
59
|
|
|
$middleware->process($view, $this->request, $this->next); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function testShouldLogSuppressedExceptions() |
66
|
|
|
{ |
67
|
|
|
$view = ViewFactory::create()->withSuppressedException(new RuntimeException()); |
68
|
|
|
|
69
|
|
|
/** @var LoggerInterface|MockObject $logger */ |
70
|
|
|
$logger = $this->createMock(LoggerInterface::class); |
71
|
|
|
$logger |
72
|
|
|
->expects($this->once()) |
73
|
|
|
->method('warning') |
74
|
|
|
->with( |
75
|
|
|
$this->equalTo('item.twig'), |
76
|
|
|
$this->arrayHasKey('exception') |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$middleware = new LoggingMiddleware($logger); |
80
|
|
|
$middleware->process($view, $this->request, $this->next); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|