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 Geocoder\Exception\QuotaExceeded; |
16
|
|
|
use Geocoder\Model\AddressCollection; |
17
|
|
|
use Geocoder\Plugin\Plugin\LoggerPlugin; |
18
|
|
|
use Geocoder\Plugin\PluginProvider; |
19
|
|
|
use Geocoder\Provider\Provider; |
20
|
|
|
use Geocoder\Query\GeocodeQuery; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
use Psr\Log\AbstractLogger; |
23
|
|
|
|
24
|
|
|
class LoggerPluginTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
public function testPlugin() |
27
|
|
|
{ |
28
|
|
|
$logger = $this->getMockBuilder(AbstractLogger::class) |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->setMethods(['log']) |
31
|
|
|
->getMock(); |
32
|
|
|
$logger->expects($this->once()) |
33
|
|
|
->method('log') |
34
|
|
|
->with('info', $this->callback(function ($message) { |
35
|
|
|
return false !== strstr($message, 'Got 0 results'); |
36
|
|
|
})); |
37
|
|
|
|
38
|
|
|
$geocodeQuery = GeocodeQuery::create('foo'); |
39
|
|
|
$collection = new AddressCollection([]); |
40
|
|
|
|
41
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
42
|
|
|
->disableOriginalConstructor() |
43
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
44
|
|
|
->getMock(); |
45
|
|
|
$provider->expects($this->once()) |
46
|
|
|
->method('geocodeQuery') |
47
|
|
|
->with($geocodeQuery) |
48
|
|
|
->willReturn($collection); |
49
|
|
|
$provider->expects($this->never())->method('reverseQuery'); |
50
|
|
|
$provider->expects($this->never())->method('getName'); |
51
|
|
|
|
52
|
|
|
$pluginProvider = new PluginProvider($provider, [new LoggerPlugin($logger)]); |
53
|
|
|
$this->assertSame($collection, $pluginProvider->geocodeQuery($geocodeQuery)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testPluginException() |
57
|
|
|
{ |
58
|
|
|
$this->expectException(QuotaExceeded::class); |
59
|
|
|
$logger = $this->getMockBuilder(AbstractLogger::class) |
60
|
|
|
->disableOriginalConstructor() |
61
|
|
|
->setMethods(['log']) |
62
|
|
|
->getMock(); |
63
|
|
|
$logger->expects($this->once()) |
64
|
|
|
->method('log') |
65
|
|
|
->with('error', $this->callback(function ($message) { |
66
|
|
|
return false !== strstr($message, 'QuotaExceeded'); |
67
|
|
|
})); |
68
|
|
|
|
69
|
|
|
$geocodeQuery = GeocodeQuery::create('foo'); |
70
|
|
|
$provider = $this->getMockBuilder(Provider::class) |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->setMethods(['geocodeQuery', 'reverseQuery', 'getName']) |
73
|
|
|
->getMock(); |
74
|
|
|
$provider->expects($this->once()) |
75
|
|
|
->method('geocodeQuery') |
76
|
|
|
->willThrowException(new QuotaExceeded()); |
77
|
|
|
$provider->expects($this->never())->method('reverseQuery'); |
78
|
|
|
$provider->expects($this->never())->method('getName'); |
79
|
|
|
|
80
|
|
|
$pluginProvider = new PluginProvider($provider, [new LoggerPlugin($logger)]); |
81
|
|
|
$pluginProvider->geocodeQuery($geocodeQuery); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|