isSubscribedToKernelRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\Tests\Unit\SubscribedProcessor;
6
7
use Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending;
8
use Dziki\MonologSentryBundle\UserAgent\ParserInterface;
9
use Dziki\MonologSentryBundle\UserAgent\UserAgent;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\HeaderBag;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Event\RequestEvent;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Symfony\Component\HttpKernel\KernelEvents;
17
18
/**
19
 * @covers \Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending
20
 *
21
 * @uses   \Dziki\MonologSentryBundle\UserAgent\UserAgent
22
 */
23
class BrowserDataAppendingTest extends TestCase
24
{
25
    /**
26
     * @var BrowserDataAppending
27
     */
28
    protected $browserDataAppendingProcessor;
29
    /**
30
     * @var ParserInterface|MockObject
31
     */
32
    protected $parser;
33
34
    /**
35
     * @test
36
     */
37
    public function isSubscribedToKernelRequest(): void
38
    {
39
        $subscribedEvents = BrowserDataAppending::getSubscribedEvents();
40
        $this->assertArrayHasKey(KernelEvents::REQUEST, $subscribedEvents);
41
        $this->assertSame(['onKernelRequest', 1024], $subscribedEvents[KernelEvents::REQUEST]);
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function parseUserAgentOnRequest(): BrowserDataAppending
48
    {
49
        /** @var ParserInterface|MockObject $parser */
50
        $parser = $this->createMock(ParserInterface::class);
51
        $this->parser = $parser;
52
        $browserDataAppendingProcessor = new BrowserDataAppending($parser);
53
54
        $this->parser
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Dziki\MonologSent...rAgent\ParserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            ->expects($this->once())
56
            ->method('parse')
57
            ->willReturn(UserAgent::create('Firefox', '62.0', 'Linux'));
58
59
        $headersBag = new HeaderBag(
60
            ['User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0']
61
        );
62
        $request = $this->createMock(Request::class);
63
        $request->headers = $headersBag;
64
        $event = new RequestEvent($this->createMock(Kernel::class), $request, null);
65
66
        $browserDataAppendingProcessor->onKernelRequest($event);
67
68
        return $browserDataAppendingProcessor;
69
    }
70
71
    /**
72
     * @test
73
     * @depends parseUserAgentOnRequest
74
     *
75
     * @param BrowserDataAppending $browserDataAppendingProcessor
76
     */
77
    public function addParsedUserAgentDataToLogRecord(BrowserDataAppending $browserDataAppendingProcessor): void
78
    {
79
        $record = $browserDataAppendingProcessor([]);
80
81
        $this->assertArrayHasKey('contexts', $record);
82
        $this->assertArrayHasKey('browser', $record['contexts']);
83
        $this->assertArrayHasKey('name', $record['contexts']['browser']);
84
        $this->assertArrayHasKey('version', $record['contexts']['browser']);
85
86
        $this->assertArrayHasKey('os', $record['contexts']);
87
        $this->assertArrayHasKey('name', $record['contexts']['os']);
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function doNotAddAnythingIfUserAgentNotParsed(): void
94
    {
95
        $browserDataAppendingProcessor = new BrowserDataAppending($this->createMock(ParserInterface::class));
96
97
        $record = $browserDataAppendingProcessor([]);
98
        $this->assertSame([], $record);
99
    }
100
}
101