Passed
Push — master ( 20ba69...1ea7e6 )
by Victor
47s queued 11s
created

testShouldLogUncaughtExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    /**
25
     * @return void
26
     */
27
    protected function setUp()
28
    {
29
        $this->request = $this->createMock(ServerRequestInterface::class);
30
        $this->next = function (View $view): View {
31
            return $view;
32
        };
33
34
        parent::setUp();
35
    }
36
37
    /**
38
     * @return void
39
     */
40
    public function testShouldLogBasicDebugInformation()
41
    {
42
        $view = ViewFactory::create();
43
44
        /** @var LoggerInterface|MockObject $logger */
45
        $logger = $this->createMock(LoggerInterface::class);
46
        $logger
47
            ->expects($this->once())
48
            ->method('debug')
49
            ->with(
50
                $this->equalTo('template.twig'),
51
                $this->callback(function (array $context): bool {
52
                    $diff = array_diff(
53
                        array_keys($context),
54
                        ['presentation_model', 'presenter_name', 'time_taken', 'variables']
55
                    );
56
57
                    return count($diff) === 0;
58
                })
59
            );
60
61
        $middleware = new LoggingMiddleware($logger);
62
        $middleware->process($view, $this->request, $this->next);
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    public function testShouldLogSuppressedExceptions()
69
    {
70
        $view = ViewFactory::create()->withSuppressedException(new RuntimeException());
71
72
        /** @var LoggerInterface|MockObject $logger */
73
        $logger = $this->createMock(LoggerInterface::class);
74
        $logger
75
            ->expects($this->once())
76
            ->method('warning')
77
            ->with(
78
                $this->equalTo('template.twig'),
79
                $this->arrayHasKey('exception')
80
            );
81
82
        $middleware = new LoggingMiddleware($logger);
83
        $middleware->process($view, $this->request, $this->next);
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    public function testShouldLogUncaughtExceptions()
90
    {
91
        $view = ViewFactory::create();
92
93
        $next = function () {
94
            throw new Exception();
95
        };
96
97
        /** @var LoggerInterface|MockObject $logger */
98
        $logger = $this->createMock(LoggerInterface::class);
99
        $logger
100
            ->expects($this->once())
101
            ->method('error')
102
            ->with(
103
                $this->equalTo('template.twig'),
104
                $this->arrayHasKey('exception')
105
            );
106
107
        $this->expectException(Exception::class);
108
109
        $middleware = new LoggingMiddleware($logger);
110
        $middleware->process($view, $this->request, $next);
111
    }
112
}
113