Completed
Push — master ( 1b0014...e530af )
by Tobias
03:11
created

TestableGeocoder   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\Geocoder;
17
use Geocoder\Query\GeocodeQuery;
18
use Geocoder\Query\ReverseQuery;
19
use Geocoder\ProviderAggregator;
20
use Geocoder\Provider\LocaleAwareGeocoder;
21
use Geocoder\Provider\Provider;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @author William Durand <[email protected]>
26
 */
27
class ProviderAggregatorTest extends TestCase
28
{
29
    /**
30
     * @var TestableGeocoder
31
     */
32
    protected $geocoder;
33
34
    protected function setUp()
35
    {
36
        $this->geocoder = new TestableGeocoder();
37
    }
38
39
    public function testRegisterProvider()
40
    {
41
        $provider = new MockProvider('test');
42
        $this->geocoder->registerProvider($provider);
43
44
        $this->assertSame($provider, $this->geocoder->getProvider());
45
    }
46
47
    public function testRegisterProviders()
48
    {
49
        $provider = new MockProvider('test');
50
        $this->geocoder->registerProviders([$provider]);
51
52
        $this->assertSame($provider, $this->geocoder->getProvider());
53
    }
54
55
    public function testUsing()
56
    {
57
        $provider1 = new MockProvider('test1');
58
        $provider2 = new MockProvider('test2');
59
        $this->geocoder->registerProviders([$provider1, $provider2]);
60
61
        $this->assertSame($provider1, $this->geocoder->getProvider());
62
63
        $this->geocoder->using('test1');
64
        $this->assertSame($provider1, $this->geocoder->getProvider());
65
66
        $this->geocoder->using('test2');
67
        $this->assertSame($provider2, $this->geocoder->getProvider());
68
69
        $this->geocoder->using('test1');
70
        $this->assertSame($provider1, $this->geocoder->getProvider());
71
    }
72
73
    /**
74
     * @expectedException \Geocoder\Exception\ProviderNotRegistered
75
     */
76
    public function testUsingNonExistantProviderShouldThrowAnException()
77
    {
78
        $this->geocoder->using('non_existant');
79
    }
80
81
    /**
82
     * @expectedException \Geocoder\Exception\ProviderNotRegistered
83
     */
84
    public function testUsingAnEmptyProviderNameShouldThrowAnException()
85
    {
86
        $this->geocoder->using('');
87
    }
88
89
    public function testGetProviders()
90
    {
91
        $provider1 = new MockProvider('test1');
92
        $provider2 = new MockProvider('test2');
93
94
        $this->geocoder->registerProviders([$provider1, $provider2]);
95
        $result = $this->geocoder->getProviders();
96
97
        $expected = [
98
            'test1' => $provider1,
99
            'test2' => $provider2,
100
        ];
101
102
        $this->assertSame($expected, $result);
103
        $this->assertArrayHasKey('test1', $result);
104
        $this->assertArrayHasKey('test2', $result);
105
    }
106
107
    /**
108
     * @expectedException \RuntimeException
109
     */
110
    public function testGetProvider()
111
    {
112
        $this->geocoder->getProvider();
113
        $this->fail('getProvider() should throw an exception');
114
    }
115
116
    public function testGetProviderWithMultipleProvidersReturnsTheFirstOne()
117
    {
118
        $this->geocoder->registerProviders([
119
            $provider1 = new MockProvider('test1'),
120
            $provider2 = new MockProvider('test2'),
121
            $provider3 = new MockProvider('test3'),
122
        ]);
123
124
        $this->assertSame($provider1, $this->geocoder->getProvider());
125
    }
126
127
    public function testDefaultMaxResults()
128
    {
129
        $this->assertSame(Geocoder::DEFAULT_RESULT_LIMIT, $this->geocoder->getLimit());
130
    }
131
}
132
133
class MockProvider implements Provider
134
{
135
    protected $name;
136
137
    public function __construct($name)
138
    {
139
        $this->name = $name;
140
    }
141
142
    public function geocodeQuery(GeocodeQuery $query): Collection
143
    {
144
        return $this->returnResult([]);
145
    }
146
147
    public function reverseQuery(ReverseQuery $query): Collection
148
    {
149
        return $this->returnResult([]);
150
    }
151
152
    public function getName(): string
153
    {
154
        return $this->name;
155
    }
156
157
    public function getLimit()
158
    {
159
    }
160
161
    public function limit($limit)
162
    {
163
        return $this;
164
    }
165
166
    public function returnResult(array $data = [])
167
    {
168
    }
169
}
170
171
class MockLocaleAwareProvider extends MockProvider implements LocaleAwareGeocoder
172
{
173
}
174
175
class MockProviderWithData extends MockProvider
176
{
177
    public function geocode($address)
178
    {
179
        return $this->returnResult([
180
            'latitude' => 123,
181
            'longitude' => 456,
182
        ]);
183
    }
184
}
185
186
class MockProviderWithRequestCount extends MockProvider
187
{
188
    public $geocodeCount = 0;
189
190
    public function geocode($address)
191
    {
192
        ++$this->geocodeCount;
193
194
        return parent::geocode($address);
195
    }
196
}
197
198
class TestableGeocoder extends ProviderAggregator
199
{
200
    public $countCallGetProvider = 0;
201
202
    public function getProvider(): Provider
203
    {
204
        ++$this->countCallGetProvider;
205
206
        return parent::getProvider();
207
    }
208
}
209