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; |
14
|
|
|
|
15
|
|
|
use Geocoder\Model\AddressCollection; |
16
|
|
|
use Geocoder\Plugin\Exception\LoopException; |
17
|
|
|
use Geocoder\Plugin\Plugin; |
18
|
|
|
use Geocoder\Plugin\PluginProvider; |
19
|
|
|
use Geocoder\Provider\Provider; |
20
|
|
|
use Geocoder\Query\GeocodeQuery; |
21
|
|
|
use Geocoder\Query\Query; |
22
|
|
|
use Geocoder\Query\ReverseQuery; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
class PluginProviderTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testDispatchQueries() |
28
|
|
|
{ |
29
|
|
|
$geocodeQuery = GeocodeQuery::create('foo'); |
30
|
|
|
$reverseQuery = ReverseQuery::fromCoordinates(47, 11); |
31
|
|
|
$collection = new AddressCollection([]); |
32
|
|
|
|
33
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
34
|
|
|
->disableOriginalConstructor() |
35
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
36
|
|
|
->getMock(); |
37
|
|
|
$provider->expects($this->once()) |
38
|
|
|
->method('geocodeQuery') |
39
|
|
|
->with($geocodeQuery) |
40
|
|
|
->willReturn($collection); |
41
|
|
|
$provider->expects($this->once()) |
42
|
|
|
->method('reverseQuery') |
43
|
|
|
->with($reverseQuery) |
44
|
|
|
->willReturn($collection); |
45
|
|
|
$provider->expects($this->never())->method('getName'); |
46
|
|
|
|
47
|
|
|
$pluginProvider = new PluginProvider($provider); |
48
|
|
|
$this->assertSame($collection, $pluginProvider->geocodeQuery($geocodeQuery)); |
49
|
|
|
$this->assertSame($collection, $pluginProvider->reverseQuery($reverseQuery)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testPluginsIsBeingUsedWhenGeocoding() |
53
|
|
|
{ |
54
|
|
|
$geocodeQuery = GeocodeQuery::create('foo'); |
55
|
|
|
$collection = new AddressCollection([]); |
56
|
|
|
|
57
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
58
|
|
|
->disableOriginalConstructor() |
59
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
60
|
|
|
->getMock(); |
61
|
|
|
$provider->expects($this->once()) |
62
|
|
|
->method('geocodeQuery') |
63
|
|
|
->with($geocodeQuery) |
64
|
|
|
->willReturn($collection); |
65
|
|
|
$provider->expects($this->never())->method('reverseQuery'); |
66
|
|
|
$provider->expects($this->never())->method('getName'); |
67
|
|
|
|
68
|
|
|
$pluginA = $this->getMockBuilder(Plugin::class) |
69
|
|
|
->disableOriginalConstructor() |
70
|
|
|
->setMethods(['handleQuery']) |
71
|
|
|
->getMock(); |
72
|
|
|
$pluginA->expects($this->once()) |
73
|
|
|
->method('handleQuery') |
74
|
|
|
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable')) |
75
|
|
|
->willReturnCallback(function (Query $query, callable $next, callable $first) { |
76
|
|
|
return $next($query); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$pluginProvider = new PluginProvider($provider, [$pluginA]); |
80
|
|
|
$this->assertSame($collection, $pluginProvider->geocodeQuery($geocodeQuery)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testPluginsIsBeingUsedWhenReverse() |
84
|
|
|
{ |
85
|
|
|
$reverseQuery = ReverseQuery::fromCoordinates(47, 11); |
86
|
|
|
$collection = new AddressCollection([]); |
87
|
|
|
|
88
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
89
|
|
|
->disableOriginalConstructor() |
90
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
91
|
|
|
->getMock(); |
92
|
|
|
$provider->expects($this->never())->method('geocodeQuery'); |
93
|
|
|
$provider->expects($this->never())->method('getName'); |
94
|
|
|
$provider->expects($this->once()) |
95
|
|
|
->method('reverseQuery') |
96
|
|
|
->with($reverseQuery) |
97
|
|
|
->willReturn($collection); |
98
|
|
|
|
99
|
|
|
$pluginA = $this->getMockBuilder(Plugin::class) |
100
|
|
|
->disableOriginalConstructor() |
101
|
|
|
->setMethods(['handleQuery']) |
102
|
|
|
->getMock(); |
103
|
|
|
$pluginA->expects($this->once()) |
104
|
|
|
->method('handleQuery') |
105
|
|
|
->with($reverseQuery, $this->isType('callable'), $this->isType('callable')) |
106
|
|
|
->willReturnCallback(function (Query $query, callable $next, callable $first) { |
107
|
|
|
return $next($query); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$pluginProvider = new PluginProvider($provider, [$pluginA]); |
111
|
|
|
$this->assertSame($collection, $pluginProvider->reverseQuery($reverseQuery)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testLoopException() |
115
|
|
|
{ |
116
|
|
|
$this->expectException(LoopException::class); |
117
|
|
|
$geocodeQuery = GeocodeQuery::create('foo'); |
118
|
|
|
|
119
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
122
|
|
|
->getMock(); |
123
|
|
|
$provider->expects($this->never())->method('geocodeQuery'); |
124
|
|
|
$provider->expects($this->never())->method('reverseQuery'); |
125
|
|
|
$provider->expects($this->never())->method('getName'); |
126
|
|
|
|
127
|
|
|
$pluginA = $this->getMockBuilder(Plugin::class) |
128
|
|
|
->disableOriginalConstructor() |
129
|
|
|
->setMethods(['handleQuery']) |
130
|
|
|
->getMock(); |
131
|
|
|
$pluginA->expects($this->any()) |
132
|
|
|
->method('handleQuery') |
133
|
|
|
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable')) |
134
|
|
|
->willReturnCallback(function (Query $query, callable $next, callable $first) { |
135
|
|
|
return $next($query); |
136
|
|
|
}); |
137
|
|
|
$pluginB = $this->getMockBuilder(Plugin::class) |
138
|
|
|
->disableOriginalConstructor() |
139
|
|
|
->setMethods(['handleQuery']) |
140
|
|
|
->getMock(); |
141
|
|
|
$pluginB->expects($this->any()) |
142
|
|
|
->method('handleQuery') |
143
|
|
|
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable')) |
144
|
|
|
->willReturnCallback(function (Query $query, callable $next, callable $first) { |
145
|
|
|
return $first($query); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$pluginProvider = new PluginProvider($provider, [$pluginA, $pluginB]); |
149
|
|
|
$pluginProvider->geocodeQuery($geocodeQuery); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|