-
Notifications
You must be signed in to change notification settings - Fork 13
/
LoggingMiddlewareTest.php
110 lines (93 loc) · 2.96 KB
/
LoggingMiddlewareTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Shoot\Shoot\Tests\Middleware;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shoot\Shoot\Middleware\LoggingMiddleware;
use Shoot\Shoot\Tests\Fixtures\ViewFactory;
use Shoot\Shoot\View;
final class LoggingMiddlewareTest extends TestCase
{
/** @var callable */
private $next;
/** @var ServerRequestInterface|MockObject */
private $request;
/**
* @return void
*/
protected function setUp()
{
$this->request = $this->createMock(ServerRequestInterface::class);
$this->next = function (View $view): View {
return $view;
};
}
/**
* @return void
*/
public function testShouldLogBasicDebugInformation()
{
$view = ViewFactory::create();
/** @var LoggerInterface|MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('debug')
->with(
$this->equalTo('item.twig'),
$this->callback(function (array $context): bool {
$diff = array_diff(
array_keys($context),
['presentation_model', 'presenter_name', 'time_taken', 'variables']
);
return count($diff) === 0;
})
);
$middleware = new LoggingMiddleware($logger);
$middleware->process($view, $this->request, $this->next);
}
/**
* @return void
*/
public function testShouldLogSuppressedExceptions()
{
$view = ViewFactory::create()->withSuppressedException(new RuntimeException());
/** @var LoggerInterface|MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('warning')
->with(
$this->equalTo('item.twig'),
$this->arrayHasKey('exception')
);
$middleware = new LoggingMiddleware($logger);
$middleware->process($view, $this->request, $this->next);
}
/**
* @return void
*/
public function testShouldLogUncaughtExceptions()
{
$view = ViewFactory::create();
$next = function () {
throw new Exception();
};
/** @var LoggerInterface|MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('error')
->with(
$this->equalTo('item.twig'),
$this->arrayHasKey('exception')
);
$this->expectException(Exception::class);
$middleware = new LoggingMiddleware($logger);
$middleware->process($view, $this->request, $next);
}
}