1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dziki\MonologSentryBundle\Tests\Unit\UserAgent; |
6
|
|
|
|
7
|
|
|
use Dziki\MonologSentryBundle\UserAgent\CachedParser; |
8
|
|
|
use Dziki\MonologSentryBundle\UserAgent\ParserInterface; |
9
|
|
|
use Dziki\MonologSentryBundle\UserAgent\UserAgent; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Psr\SimpleCache\CacheInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Dziki\MonologSentryBundle\UserAgent\CachedParser |
15
|
|
|
* |
16
|
|
|
* @uses \Dziki\MonologSentryBundle\UserAgent\UserAgent |
17
|
|
|
*/ |
18
|
|
|
class CachedParserTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
* @dataProvider userAgentsDataProvider |
23
|
|
|
* |
24
|
|
|
* @param string $requestedUserAgent |
25
|
|
|
*/ |
26
|
|
|
public function notFoundEntriesCallsNextParserAndAddToCache(string $requestedUserAgent): void |
27
|
|
|
{ |
28
|
|
|
$cacheInterface = $this->createMock(CacheInterface::class); |
29
|
|
|
$cacheInterface->method('get') |
30
|
|
|
->willReturn(null) |
31
|
|
|
; |
32
|
|
|
|
33
|
|
|
$parsedUserAgent = UserAgent::create('', '', ''); |
34
|
|
|
|
35
|
|
|
$cacheInterface |
36
|
|
|
->expects($this->once()) |
37
|
|
|
->method('set') |
38
|
|
|
->with(md5($requestedUserAgent), serialize($parsedUserAgent)) |
39
|
|
|
->willReturn(null) |
40
|
|
|
; |
41
|
|
|
|
42
|
|
|
$nextParser = $this->createMock(ParserInterface::class); |
43
|
|
|
$nextParser->expects($this->once()) |
44
|
|
|
->method('parse') |
45
|
|
|
->with($requestedUserAgent) |
46
|
|
|
->willReturn($parsedUserAgent) |
47
|
|
|
; |
48
|
|
|
|
49
|
|
|
$cachedParser = new CachedParser($cacheInterface, $nextParser); |
50
|
|
|
|
51
|
|
|
$cachedParser->parse($requestedUserAgent); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function userAgentsDataProvider(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
['a'], |
58
|
|
|
['b'], |
59
|
|
|
['c'], |
60
|
|
|
['d'], |
61
|
|
|
['e'], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
* @dataProvider userAgentsDataProvider |
68
|
|
|
* |
69
|
|
|
* @param string $requestedUserAgent |
70
|
|
|
*/ |
71
|
|
|
public function foundEntriesReturnsItImmediately(string $requestedUserAgent): void |
72
|
|
|
{ |
73
|
|
|
$parsedSerializedAgent = serialize(UserAgent::create('', '', '')); |
74
|
|
|
|
75
|
|
|
$cacheInterface = $this->createMock(CacheInterface::class); |
76
|
|
|
$cacheInterface->expects($this->once()) |
77
|
|
|
->method('get') |
78
|
|
|
->with(md5($requestedUserAgent)) |
79
|
|
|
->willReturn($parsedSerializedAgent) |
80
|
|
|
; |
81
|
|
|
|
82
|
|
|
$nextParser = $this->createMock(ParserInterface::class); |
83
|
|
|
|
84
|
|
|
$cachedParser = new CachedParser($cacheInterface, $nextParser); |
85
|
|
|
|
86
|
|
|
$cachedParser->parse($requestedUserAgent); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|