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\Plugin\Tests\Plugin; |
14
|
|
|
|
15
|
|
|
use Cache\Adapter\Void\VoidCachePool; |
16
|
|
|
use Geocoder\Plugin\Plugin\CachePlugin; |
17
|
|
|
use Geocoder\Query\GeocodeQuery; |
18
|
|
|
use Geocoder\Query\Query; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class CachePluginTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testPluginMiss() |
24
|
|
|
{ |
25
|
|
|
$ttl = 4711; |
26
|
|
|
$query = GeocodeQuery::create('foo'); |
27
|
|
|
$queryString = sha1($query->__toString()); |
28
|
|
|
$cache = $this->getMockBuilder(VoidCachePool::class) |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->setMethods(['get', 'set']) |
31
|
|
|
->getMock(); |
32
|
|
|
|
33
|
|
|
$cache->expects($this->once()) |
34
|
|
|
->method('get') |
35
|
|
|
->with('v4'.$queryString) |
36
|
|
|
->willReturn(null); |
37
|
|
|
$cache->expects($this->once()) |
38
|
|
|
->method('set') |
39
|
|
|
->with('v4'.$queryString, 'result', $ttl) |
40
|
|
|
->willReturn(true); |
41
|
|
|
|
42
|
|
|
$first = function (Query $query) { |
43
|
|
|
$this->fail('Plugin should not restart the chain'); |
44
|
|
|
}; |
45
|
|
|
$next = function (Query $query) { |
46
|
|
|
return 'result'; |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
$plugin = new CachePlugin($cache, $ttl); |
50
|
|
|
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testPluginHit() |
54
|
|
|
{ |
55
|
|
|
$query = GeocodeQuery::create('foo'); |
56
|
|
|
$queryString = sha1($query->__toString()); |
57
|
|
|
$cache = $this->getMockBuilder(VoidCachePool::class) |
58
|
|
|
->disableOriginalConstructor() |
59
|
|
|
->setMethods(['get', 'set']) |
60
|
|
|
->getMock(); |
61
|
|
|
|
62
|
|
|
$cache->expects($this->once()) |
63
|
|
|
->method('get') |
64
|
|
|
->with('v4'.$queryString) |
65
|
|
|
->willReturn('result'); |
66
|
|
|
$cache->expects($this->never())->method('set'); |
67
|
|
|
|
68
|
|
|
$first = function (Query $query) { |
69
|
|
|
$this->fail('Plugin should not restart the chain'); |
70
|
|
|
}; |
71
|
|
|
$next = function (Query $query) { |
72
|
|
|
$this->fail('Plugin not call $next on cache hit'); |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
$plugin = new CachePlugin($cache); |
76
|
|
|
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|