forked from geocoder-php/BazingaGeocoderBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginInteractionTest.php
More file actions
132 lines (108 loc) · 5.43 KB
/
PluginInteractionTest.php
File metadata and controls
132 lines (108 loc) · 5.43 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
128
129
130
131
132
<?php
declare(strict_types=1);
/*
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Bazinga\GeocoderBundle\Tests\Functional;
use Bazinga\GeocoderBundle\BazingaGeocoderBundle;
use Geocoder\Query\GeocodeQuery;
use Http\Message\RequestMatcher\RequestMatcher;
use Http\Mock\Client;
use Nyholm\BundleTest\TestKernel;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\KernelInterface;
final class PluginInteractionTest extends KernelTestCase
{
use ExpectDeprecationTrait;
protected static function getKernelClass(): string
{
return CustomTestKernel::class;
}
protected static function createKernel(array $options = []): KernelInterface
{
/**
* @var TestKernel $kernel
*/
$kernel = parent::createKernel($options);
$kernel->addTestBundle(BazingaGeocoderBundle::class);
$kernel->handleOptions($options);
return $kernel;
}
public function testCachePluginUsesIpFromFakeIpPlugin(): void
{
$kernel = self::bootKernel(['config' => static function (TestKernel $kernel) {
$kernel->setClearCacheAfterShutdown(false);
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
if ($kernel::VERSION_ID >= 60000) {
$kernel->addTestConfig(__DIR__.'/config/framework_sf6.yml');
}
$kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml');
$kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_cn.yml');
}]);
$kernel->setClearCacheAfterShutdown(false);
$container = self::getContainer();
$httpClient = $container->get(Client::class);
$httpClient->on(new RequestMatcher(), function (RequestInterface $request) {
if ('https://freegeoip.app/json/123.123.123.128' === (string) $request->getUri()) {
$stream = $this->createMock(StreamInterface::class);
$stream->expects(self::once())
->method('__toString')
->willReturn('{"ip":"123.123.123.128","country_code":"CN","country_name":"China","region_code":"CN-BJ","region_name":"Beijing","city":"Beijing","zip_code":"100006","time_zone":"Asia\/Shanghai","latitude":39.907501220703125,"longitude":116.39710235595703,"metro_code":0}');
$response = $this->createMock(ResponseInterface::class);
$response->expects(self::once())
->method('getStatusCode')
->willReturn(200);
$response->expects(self::once())
->method('getBody')
->willReturn($stream);
return $response;
}
self::fail(sprintf('Unexpected http call "%s %s".', $request->getMethod(), (string) $request->getUri()));
});
$geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin');
$result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1'));
$country = $result->first()->getCountry()->getCode();
self::assertEquals('CN', $country);
$kernel = self::bootKernel(['config' => static function (TestKernel $kernel) {
$kernel->setClearCacheAfterShutdown(false);
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
if ($kernel::VERSION_ID >= 60000) {
$kernel->addTestConfig(__DIR__.'/config/framework_sf6.yml');
}
$kernel->addTestConfig(__DIR__.'/config/cache_symfony.yml');
$kernel->addTestConfig(__DIR__.'/config/fakeip_with_cache_fr.yml');
}]);
$kernel->setClearCacheAfterShutdown(false);
$container = self::getContainer();
$httpClient = $container->get(Client::class);
$httpClient->on(new RequestMatcher(), function (RequestInterface $request) {
if ('https://freegeoip.app/json/87.98.128.10' === (string) $request->getUri()) {
$stream = $this->createMock(StreamInterface::class);
$stream->expects(self::once())
->method('__toString')
->willReturn('{"ip":"87.98.128.10","country_code":"FR","country_name":"France","region_code":null,"region_name":"Nord","city":"Roubaix","zip_code":"59100","time_zone":"Europe\/Paris","latitude":50.69371032714844,"longitude":3.174438953399658,"metro_code":0}');
$response = $this->createMock(ResponseInterface::class);
$response->expects(self::once())
->method('getStatusCode')
->willReturn(200);
$response->expects(self::once())
->method('getBody')
->willReturn($stream);
return $response;
}
self::fail(sprintf('Unexpected http call "%s %s".', $request->getMethod(), (string) $request->getUri()));
});
$geoPluginGeocoder = $container->get('bazinga_geocoder.provider.geoPlugin');
$result = $geoPluginGeocoder->geocodeQuery(GeocodeQuery::create('::1'));
$country = $result->first()->getCountry()->getCode();
self::assertEquals('FR', $country);
}
}