Completed
Push — master ( 4dfb44...513dd2 )
by Tobias
03:00
created

ProviderCacheTest::testReverseHit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\Cache\Tests;
14
15
use Geocoder\Model\Address;
16
use Geocoder\Model\AddressCollection;
17
use Geocoder\Provider\Cache\ProviderCache;
18
use Geocoder\Provider\Provider;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
use PHPUnit\Framework\TestCase;
22
use Psr\SimpleCache\CacheInterface;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class ProviderCacheTest extends TestCase
28
{
29
    /**
30
     * @var \PHPUnit_Framework_MockObject_MockObject|Provider
31
     */
32
    private $providerMock;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|CacheInterface
36
     */
37
    private $cacheMock;
38
39
    protected function setUp()
40
    {
41
        parent::setUp();
42
43
        $this->cacheMock = $this->getMockBuilder(CacheInterface::class)
44
            ->setMethods(['get', 'set', 'delete', 'clear', 'setMultiple', 'getMultiple', 'deleteMultiple', 'has'])
45
46
            ->getMock();
47
48
        $this->providerMock = $this->getMockBuilder(Provider::class)
49
            ->setMethods(['getFoo', 'getName', 'geocodeQuery', 'reverseQuery'])
50
            ->getMock();
51
    }
52
53
    public function testName()
54
    {
55
        $this->providerMock->expects($this->once())
56
            ->method('getName')
57
            ->willReturn('foo');
58
59
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock);
60
        $this->assertEquals('foo (cache)', $providerCache->getName());
61
    }
62
63
    public function testMagicFunction()
64
    {
65
        $this->providerMock->expects($this->once())
66
            ->method('getFoo')
67
            ->willReturn('foo');
68
69
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock);
70
        $this->assertEquals('foo', $providerCache->getFoo());
71
    }
72
73
    public function testGeocodeMiss()
74
    {
75
        $query = GeocodeQuery::create('foo');
76
        $result = new AddressCollection([Address::createFromArray([])]);
77
        $ttl = 4711;
78
79
        $this->cacheMock->expects($this->once())
80
            ->method('get')
81
            ->willReturn(null);
82
83
        $this->cacheMock->expects($this->once())
84
            ->method('set')
85
            ->with($this->anything(), $result, $ttl)
86
            ->willReturn(null);
87
88
        $this->providerMock->expects($this->once())
89
            ->method('geocodeQuery')
90
            ->with($query)
91
            ->willReturn($result);
92
93
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
94
        $providerCache->geocodeQuery($query);
95
    }
96
97
    public function testGeocodeHit()
98
    {
99
        $query = GeocodeQuery::create('foo');
100
        $result = new AddressCollection([Address::createFromArray([])]);
101
        $ttl = 4711;
102
103
        $this->cacheMock->expects($this->once())
104
            ->method('get')
105
            ->willReturn($result);
106
107
        $this->cacheMock->expects($this->never())
108
            ->method('set');
109
110
        $this->providerMock->expects($this->never())
111
            ->method('geocodeQuery');
112
113
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
114
        $providerCache->geocodeQuery($query);
115
    }
116
117
    public function testReverseMiss()
118
    {
119
        $query = ReverseQuery::fromCoordinates(1, 2);
120
        $result = new AddressCollection([Address::createFromArray([])]);
121
        $ttl = 4711;
122
123
        $this->cacheMock->expects($this->once())
124
            ->method('get')
125
            ->willReturn(null);
126
127
        $this->cacheMock->expects($this->once())
128
            ->method('set')
129
            ->with($this->anything(), $result, $ttl)
130
            ->willReturn(null);
131
132
        $this->providerMock->expects($this->once())
133
            ->method('reverseQuery')
134
            ->with($query)
135
            ->willReturn($result);
136
137
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
138
        $providerCache->reverseQuery($query);
139
    }
140
141
    public function testReverseHit()
142
    {
143
        $query = ReverseQuery::fromCoordinates(1, 2);
144
        $result = new AddressCollection([Address::createFromArray([])]);
145
        $ttl = 4711;
146
147
        $this->cacheMock->expects($this->once())
148
            ->method('get')
149
            ->willReturn($result);
150
151
        $this->cacheMock->expects($this->never())
152
            ->method('set');
153
154
        $this->providerMock->expects($this->never())
155
            ->method('reverseQuery');
156
157
        $providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
158
        $providerCache->reverseQuery($query);
159
    }
160
}
161