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\Parser; |
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\GetResponseEvent; |
15
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
16
|
|
|
|
17
|
|
|
class BrowserDataAppendingTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var BrowserDataAppending |
21
|
|
|
*/ |
22
|
|
|
protected $browserDataAppendingProcessor; |
23
|
|
|
/** |
24
|
|
|
* @var Parser|MockObject |
25
|
|
|
*/ |
26
|
|
|
protected $parser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
public function isSubscribedToKernelRequest(): void |
32
|
|
|
{ |
33
|
|
|
$subscribedEvents = BrowserDataAppending::getSubscribedEvents(); |
34
|
|
|
$this->assertArrayHasKey(KernelEvents::REQUEST, $subscribedEvents); |
35
|
|
|
$this->assertSame(['onKernelRequest', 1024], $subscribedEvents[KernelEvents::REQUEST]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function parseUserAgentOnRequest(): BrowserDataAppending |
42
|
|
|
{ |
43
|
|
|
/** @var Parser|MockObject $parser */ |
44
|
|
|
$parser = $this->createMock(Parser::class); |
45
|
|
|
$this->parser = $parser; |
46
|
|
|
$browserDataAppendingProcessor = new BrowserDataAppending($parser); |
47
|
|
|
|
48
|
|
|
$this->parser |
|
|
|
|
49
|
|
|
->expects($this->once()) |
50
|
|
|
->method('parse') |
51
|
|
|
->willReturn(UserAgent::create('Firefox', '62.0', 'Linux')) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
$headersBag = new HeaderBag( |
55
|
|
|
['User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0'] |
56
|
|
|
); |
57
|
|
|
$request = $this->createMock(Request::class); |
58
|
|
|
$request->headers = $headersBag; |
59
|
|
|
/** |
60
|
|
|
* @var GetResponseEvent|MockObject $event |
61
|
|
|
*/ |
62
|
|
|
$event = $this->createMock(GetResponseEvent::class); |
63
|
|
|
$event->method('getRequest') |
|
|
|
|
64
|
|
|
->willReturn($request) |
65
|
|
|
; |
66
|
|
|
|
67
|
|
|
$browserDataAppendingProcessor->onKernelRequest($event); |
68
|
|
|
|
69
|
|
|
return $browserDataAppendingProcessor; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
* @depends parseUserAgentOnRequest |
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(Parser::class)); |
96
|
|
|
|
97
|
|
|
$record = $browserDataAppendingProcessor([]); |
98
|
|
|
$this->assertSame([], $record); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.