forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProviderTest.php
More file actions
127 lines (108 loc) · 5.06 KB
/
PluginProviderTest.php
File metadata and controls
127 lines (108 loc) · 5.06 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
<?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;
use Geocoder\Model\AddressCollection;
use Geocoder\Plugin\Exception\LoopException;
use Geocoder\Plugin\Plugin;
use Geocoder\Plugin\PluginProvider;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;
use PHPUnit\Framework\TestCase;
class PluginProviderTest extends TestCase
{
public function testDispatchQueries(): void
{
$geocodeQuery = GeocodeQuery::create('foo');
$reverseQuery = ReverseQuery::fromCoordinates(47, 11);
$collection = new AddressCollection([]);
$provider = $this->createPartialMock(Provider::class, ['geocodeQuery', 'reverseQuery', 'getName']);
$provider->expects($this->once())
->method('geocodeQuery')
->with($geocodeQuery)
->willReturn($collection);
$provider->expects($this->once())
->method('reverseQuery')
->with($reverseQuery)
->willReturn($collection);
$provider->expects($this->never())->method('getName');
$pluginProvider = new PluginProvider($provider);
$this->assertSame($collection, $pluginProvider->geocodeQuery($geocodeQuery));
$this->assertSame($collection, $pluginProvider->reverseQuery($reverseQuery));
}
public function testPluginsIsBeingUsedWhenGeocoding(): void
{
$geocodeQuery = GeocodeQuery::create('foo');
$collection = new AddressCollection([]);
$provider = $this->createPartialMock(Provider::class, ['geocodeQuery', 'reverseQuery', 'getName']);
$provider->expects($this->once())
->method('geocodeQuery')
->with($geocodeQuery)
->willReturn($collection);
$provider->expects($this->never())->method('reverseQuery');
$provider->expects($this->never())->method('getName');
$pluginA = $this->createPartialMock(Plugin::class, ['handleQuery']);
$pluginA->expects($this->once())
->method('handleQuery')
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable'))
->willReturnCallback(function (Query $query, callable $next, callable $first) {
return $next($query);
});
$pluginProvider = new PluginProvider($provider, [$pluginA]);
$this->assertSame($collection, $pluginProvider->geocodeQuery($geocodeQuery));
}
public function testPluginsIsBeingUsedWhenReverse(): void
{
$reverseQuery = ReverseQuery::fromCoordinates(47, 11);
$collection = new AddressCollection([]);
$provider = $this->createPartialMock(Provider::class, ['geocodeQuery', 'reverseQuery', 'getName']);
$provider->expects($this->never())->method('geocodeQuery');
$provider->expects($this->never())->method('getName');
$provider->expects($this->once())
->method('reverseQuery')
->with($reverseQuery)
->willReturn($collection);
$pluginA = $this->createPartialMock(Plugin::class, ['handleQuery']);
$pluginA->expects($this->once())
->method('handleQuery')
->with($reverseQuery, $this->isType('callable'), $this->isType('callable'))
->willReturnCallback(function (Query $query, callable $next, callable $first) {
return $next($query);
});
$pluginProvider = new PluginProvider($provider, [$pluginA]);
$this->assertSame($collection, $pluginProvider->reverseQuery($reverseQuery));
}
public function testLoopException(): void
{
$this->expectException(LoopException::class);
$geocodeQuery = GeocodeQuery::create('foo');
$provider = $this->createPartialMock(Provider::class, ['geocodeQuery', 'reverseQuery', 'getName']);
$provider->expects($this->never())->method('geocodeQuery');
$provider->expects($this->never())->method('reverseQuery');
$provider->expects($this->never())->method('getName');
$pluginA = $this->createPartialMock(Plugin::class, ['handleQuery']);
$pluginA->expects($this->any())
->method('handleQuery')
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable'))
->willReturnCallback(function (Query $query, callable $next, callable $first) {
return $next($query);
});
$pluginB = $this->createPartialMock(Plugin::class, ['handleQuery']);
$pluginB->expects($this->any())
->method('handleQuery')
->with($geocodeQuery, $this->isType('callable'), $this->isType('callable'))
->willReturnCallback(function (Query $query, callable $next, callable $first) {
return $first($query);
});
$pluginProvider = new PluginProvider($provider, [$pluginA, $pluginB]);
$pluginProvider->geocodeQuery($geocodeQuery);
}
}