forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachePluginTest.php
More file actions
94 lines (80 loc) · 2.76 KB
/
Copy pathCachePluginTest.php
File metadata and controls
94 lines (80 loc) · 2.76 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
<?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\Plugin\Tests\Plugin;
use Cache\Adapter\Void\VoidCachePool;
use Generator;
use Geocoder\Model\Coordinates;
use Geocoder\Plugin\Plugin\CachePlugin;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;
use PHPUnit\Framework\TestCase;
class CachePluginTest extends TestCase
{
public function testPluginMiss()
{
$ttl = 4711;
$query = GeocodeQuery::create('foo');
$queryString = sha1($query->__toString());
$cache = $this->getMockBuilder(VoidCachePool::class)
->disableOriginalConstructor()
->setMethods(['get', 'set'])
->getMock();
$cache->expects($this->once())
->method('get')
->with('v4'.$queryString)
->willReturn(null);
$cache->expects($this->once())
->method('set')
->with('v4'.$queryString, 'result', $ttl)
->willReturn(true);
$first = function (Query $query) {
$this->fail('Plugin should not restart the chain');
};
$next = function (Query $query) {
return 'result';
};
$plugin = new CachePlugin($cache, $ttl);
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first));
}
public function getQueryProvider(): Generator
{
$query = GeocodeQuery::create('foo');
$key = sha1($query->__toString());
yield [$query, $key];
$query = ReverseQuery::create(new Coordinates(12.123456, 12.123456));
$lessPreciseQuery = $query->withCoordinates(new Coordinates(12.1235, 12.1235));
$key = sha1((string) $lessPreciseQuery);
yield [$query, $key];
}
/**
* @dataProvider getQueryProvider
*/
public function testPluginHit(Query $query, string $key)
{
$cache = $this->getMockBuilder(VoidCachePool::class)
->disableOriginalConstructor()
->setMethods(['get', 'set'])
->getMock();
$cache->expects($this->once())
->method('get')
->with('v4'.$key)
->willReturn('result');
$cache->expects($this->never())->method('set');
$first = function (Query $query) {
$this->fail('Plugin should not restart the chain');
};
$next = function (Query $query) {
$this->fail('Plugin not call $next on cache hit');
};
$plugin = new CachePlugin($cache, 0, 4);
$this->assertEquals('result', $plugin->handleQuery($query, $next, $first));
}
}