Passed
Pull Request — master (#2)
by Victor
03:19
created

LoggingMiddlewareTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Tests\Middleware;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Log\LogLevel;
9
use RuntimeException;
10
use Shoot\Shoot\Middleware\LoggingMiddleware;
11
use Shoot\Shoot\Tests\Fixtures\Logger;
12
use Shoot\Shoot\Tests\Fixtures\MiddlewareCallback;
13
use Shoot\Shoot\Tests\Fixtures\ViewFactory;
14
15
final class LoggingMiddlewareTest extends TestCase
16
{
17
    /** @var callable */
18
    private $next;
19
20
    /** @var ServerRequestInterface */
21
    private $request;
22
23
    /**
24
     * @return void
25
     */
26
    protected function setUp()
27
    {
28
        $this->next = new MiddlewareCallback();
29
        $this->request = $this->prophesize(ServerRequestInterface::class)->reveal();
30
    }
31
32
    /**
33
     * @return void
34
     */
35
    public function testProcessShouldLogBasicDebugInformation()
36
    {
37
        $view = ViewFactory::create();
38
        $wasCalled = false;
39
40
        $middleware = new LoggingMiddleware(new Logger(function ($level, $message, $context) use (&$wasCalled) {
41
            $this->assertSame(LogLevel::DEBUG, $level);
42
            $this->assertSame('item.twig', $message);
43
            $this->assertArrayHasKey('presentation_model', $context);
44
            $this->assertArrayHasKey('presenter_name', $context);
45
            $this->assertArrayHasKey('time_taken', $context);
46
            $this->assertArrayHasKey('variables', $context);
47
48
            $wasCalled = true;
49
        }));
50
51
        $middleware->process($view, $this->request, $this->next);
52
53
        $this->assertTrue($wasCalled);
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    public function testProcessShouldLogSuppressedExceptions()
60
    {
61
        $view = ViewFactory::create()->withSuppressedException(new RuntimeException());
62
        $wasCalled = false;
63
64
        $middleware = new LoggingMiddleware(new Logger(function ($level, $message, $context) use (&$wasCalled) {
65
            $this->assertSame(LogLevel::WARNING, $level);
66
            $this->assertArrayHasKey('exception', $context);
67
68
            $wasCalled = true;
69
        }));
70
71
        $middleware->process($view, $this->request, $this->next);
72
73
        $this->assertTrue($wasCalled);
74
    }
75
}
76