forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProviderCacheTest.php
More file actions
175 lines (137 loc) · 5.13 KB
/
ProviderCacheTest.php
File metadata and controls
175 lines (137 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\Cache\Tests;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Cache\ProviderCache;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
/**
* @author Tobias Nyholm <[email protected]>
*/
class ProviderCacheTest extends TestCase
{
/**
* @var Provider&MockObject
*/
private $providerMock;
/**
* @var CacheInterface&MockObject
*/
private $cacheMock;
protected function setUp(): void
{
parent::setUp();
$this->cacheMock = $this->createPartialMock(CacheInterface::class, ['get', 'set', 'delete', 'clear', 'setMultiple', 'getMultiple', 'deleteMultiple', 'has']);
$this->providerMock = $this->createPartialMock(Provider::class, ['getName', 'geocodeQuery', 'reverseQuery']);
}
public function testName(): void
{
$this->providerMock->expects($this->once())
->method('getName')
->willReturn('foo');
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock);
$this->assertEquals('foo (cache)', $providerCache->getName());
}
public function testGeocodeMiss(): void
{
$query = GeocodeQuery::create('foo');
$result = new AddressCollection([Address::createFromArray([])]);
$ttl = 4711;
$this->cacheMock->expects($this->once())
->method('get')
->willReturn(null);
$this->cacheMock->expects($this->once())
->method('set')
->with($this->anything(), $result, $ttl)
->willReturn(true);
$this->providerMock->expects($this->once())
->method('geocodeQuery')
->with($query)
->willReturn($result);
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
$providerCache->geocodeQuery($query);
}
public function testGeocodeHit(): void
{
$query = GeocodeQuery::create('foo');
$result = new AddressCollection([Address::createFromArray([])]);
$ttl = 4711;
$this->cacheMock->expects($this->once())
->method('get')
->willReturn($result);
$this->cacheMock->expects($this->never())
->method('set');
$this->providerMock->expects($this->never())
->method('geocodeQuery');
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
$providerCache->geocodeQuery($query);
}
public function testReverseMiss(): void
{
$query = ReverseQuery::fromCoordinates(1, 2);
$result = new AddressCollection([Address::createFromArray([])]);
$ttl = 4711;
$this->cacheMock->expects($this->once())
->method('get')
->willReturn(null);
$this->cacheMock->expects($this->once())
->method('set')
->with($this->anything(), $result, $ttl)
->willReturn(true);
$this->providerMock->expects($this->once())
->method('reverseQuery')
->with($query)
->willReturn($result);
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
$providerCache->reverseQuery($query);
}
public function testReverseHit(): void
{
$query = ReverseQuery::fromCoordinates(1, 2);
$result = new AddressCollection([Address::createFromArray([])]);
$ttl = 4711;
$this->cacheMock->expects($this->once())
->method('get')
->willReturn($result);
$this->cacheMock->expects($this->never())
->method('set');
$this->providerMock->expects($this->never())
->method('reverseQuery');
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
$providerCache->reverseQuery($query);
}
public function testCacheSeparation(): void
{
$query = GeocodeQuery::create('foo');
$ttl = 4711;
$this->providerMock->expects($this->any())
->method('getName')
->willReturn('foo');
$getCacheKey = self::getMethod('getCacheKey');
$providerCache = new ProviderCache($this->providerMock, $this->cacheMock, $ttl);
$providerCacheWithSeparation = new ProviderCache($this->providerMock, $this->cacheMock, $ttl, true);
$this->assertNotEquals(
$getCacheKey->invokeArgs($providerCache, [$query]),
$getCacheKey->invokeArgs($providerCacheWithSeparation, [$query])
);
}
protected static function getMethod(string $name): \ReflectionMethod
{
$class = new \ReflectionClass(ProviderCache::class);
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
}