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