Completed
Push — master ( 419f51...37bf86 )
by Michał
08:39
created

foundEntriesReturnsItImmediately()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Parser;
9
use Dziki\MonologSentryBundle\UserAgent\UserAgent;
10
use PHPUnit\Framework\TestCase;
11
use Psr\SimpleCache\CacheInterface;
12
13
class CachedParserTest extends TestCase
14
{
15
    /**
16
     * @test
17
     * @dataProvider userAgentsDataProvider
18
     * @param string $requestedUserAgent
19
     */
20
    public function notFoundEntriesCallsNextParserAndAddToCache(string $requestedUserAgent): void
21
    {
22
        $cacheInterface = $this->createMock(CacheInterface::class);
23
        $cacheInterface->method('get')
24
                       ->willReturn(null)
25
        ;
26
27
        $parsedUserAgent = UserAgent::create('', '', '');
28
29
        $cacheInterface
30
            ->expects($this->once())
31
            ->method('set')
32
            ->with(md5($requestedUserAgent), serialize($parsedUserAgent))
33
                       ->willReturn(null)
34
        ;
35
36
        $nextParser = $this->createMock(Parser::class);
37
        $nextParser->expects($this->once())
38
                   ->method('parse')
39
                   ->with($requestedUserAgent)
40
                   ->willReturn($parsedUserAgent)
41
        ;
42
43
        $cachedParser = new CachedParser($cacheInterface, $nextParser);
44
45
        $cachedParser->parse($requestedUserAgent);
46
    }
47
48
    public function userAgentsDataProvider(): array
49
    {
50
        return [
51
            ['a'],
52
            ['b'],
53
            ['c'],
54
            ['d'],
55
            ['e'],
56
        ];
57
    }
58
59
    /**
60
     * @test
61
     * @dataProvider userAgentsDataProvider
62
     * @param string $requestedUserAgent
63
     */
64
    public function foundEntriesReturnsItImmediately(string $requestedUserAgent): void
65
    {
66
        $parsedSerializedAgent = serialize(UserAgent::create('', '', ''));
67
68
        $cacheInterface = $this->createMock(CacheInterface::class);
69
        $cacheInterface->expects($this->once())
70
                       ->method('get')
71
                       ->with(md5($requestedUserAgent))
72
                       ->willReturn($parsedSerializedAgent)
73
        ;
74
75
        $nextParser = $this->createMock(Parser::class);
76
77
        $cachedParser = new CachedParser($cacheInterface, $nextParser);
78
79
        $cachedParser->parse($requestedUserAgent);
80
    }
81
}
82