EnvelopeTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 58
dl 0
loc 106
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddlogAddsALogToTheLogger() 0 76 1
A createLoggerMock() 0 5 1
A testIsInstantiable() 0 9 1
A testImplementsPsrLoggerawareinterface() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DanBettles\Defence\Tests;
6
7
use DanBettles\Defence\Envelope;
8
use DanBettles\Defence\Logger\NullLogger;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\LogLevel;
14
use ReflectionClass;
15
use Symfony\Component\HttpFoundation\Request;
16
17
class EnvelopeTest extends TestCase
18
{
19
    //###> Factory Methods ###
20
    private function createLoggerMock(): MockObject
21
    {
22
        return $this
23
            ->getMockBuilder(LoggerInterface::class)
24
            ->getMock()
25
        ;
26
    }
27
    //###< Factory Methods ###
28
29
    public function testIsInstantiable(): void
30
    {
31
        $request = Request::createFromGlobals();
32
        $logger = new NullLogger();
33
34
        $envelope = new Envelope($request, $logger);
35
36
        $this->assertSame($request, $envelope->getRequest());
37
        $this->assertSame($logger, $envelope->getLogger());
38
    }
39
40
    public function testImplementsPsrLoggerawareinterface(): void
41
    {
42
        $reflectionClass = new ReflectionClass(Envelope::class);
43
44
        $this->assertTrue($reflectionClass->implementsInterface(LoggerAwareInterface::class));
45
    }
46
47
    public function testAddlogAddsALogToTheLogger(): void
48
    {
49
        ($fullyLoadedRequestLoggerMock = $this->createLoggerMock())
50
            ->expects($this->once())
51
            ->method('log')
52
            ->with(LogLevel::WARNING, 'The request looks suspicious.', [
53
                'host_name' => gethostname(),
54
                'request_method' => 'GET',
55
                'uri' => 'http://foo.com/?bar=baz&qux=quux',
56
                'user_agent' => 'garply',
57
                'referer' => 'grault',
58
            ])
59
        ;
60
61
        /** @var LoggerInterface $fullyLoadedRequestLoggerMock */
62
63
        $fullyLoadedRequest = new Request([], [], [], [], [], [
64
            'HTTP_HOST' => 'foo.com',
65
            'REQUEST_METHOD' => 'GET',
66
            'QUERY_STRING' => 'bar=baz&qux=quux',
67
            'HTTP_USER_AGENT' => 'garply',
68
            'HTTP_REFERER' => 'grault',
69
        ]);
70
71
        (new Envelope($fullyLoadedRequest, $fullyLoadedRequestLoggerMock))
72
            ->addLogEntry(LogLevel::WARNING, 'The request looks suspicious.')
73
        ;
74
75
        ($minimalRequestLoggerMock = $this->createLoggerMock())
76
            ->expects($this->once())
77
            ->method('log')
78
            ->with(LogLevel::EMERGENCY, 'The request looks suspicious.', [
79
                'host_name' => gethostname(),
80
                'request_method' => 'GET',
81
                'uri' => 'http://foo.com/?bar=baz&qux=quux',
82
            ])
83
        ;
84
85
        /** @var LoggerInterface $minimalRequestLoggerMock */
86
87
        $minimalRequest = new Request([], [], [], [], [], [
88
            'HTTP_HOST' => 'foo.com',
89
            'REQUEST_METHOD' => 'GET',
90
            'QUERY_STRING' => 'bar=baz&qux=quux',
91
        ]);
92
93
        (new Envelope($minimalRequest, $minimalRequestLoggerMock))
94
            ->addLogEntry(LogLevel::EMERGENCY, 'The request looks suspicious.')
95
        ;
96
97
        ($postRequestLoggerMock = $this->createLoggerMock())
98
            ->expects($this->once())
99
            ->method('log')
100
            ->with(LogLevel::EMERGENCY, 'The request looks suspicious.', [
101
                'host_name' => gethostname(),
102
                'request_method' => 'POST',
103
                'uri' => 'http://foo.com/',
104
                'parameters' => [
105
                    'foo' => 'bar',
106
                    'baz' => 'qux',
107
                ],
108
            ])
109
        ;
110
111
        /** @var LoggerInterface $postRequestLoggerMock */
112
113
        $postRequest = new Request([], [
114
            'foo' => 'bar',
115
            'baz' => 'qux',
116
        ], [], [], [], [
117
            'HTTP_HOST' => 'foo.com',
118
            'REQUEST_METHOD' => 'POST',
119
        ]);
120
121
        (new Envelope($postRequest, $postRequestLoggerMock))
122
            ->addLogEntry(LogLevel::EMERGENCY, 'The request looks suspicious.')
123
        ;
124
    }
125
}
126