PageRenderTime 79ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Provider/IpInfoDb/Tests/IpInfoDbTest.php

http://github.com/willdurand/Geocoder
PHP | 181 lines | 131 code | 37 blank | 13 comment | 3 complexity | 613efe1fae2551230ed8a4b196d78d6b MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder\Provider\IpInfoDb\Tests;
  11. use Geocoder\IntegrationTest\BaseTestCase;
  12. use Geocoder\Location;
  13. use Geocoder\Query\GeocodeQuery;
  14. use Geocoder\Query\ReverseQuery;
  15. use Geocoder\Provider\IpInfoDb\IpInfoDb;
  16. class IpInfoDbTest extends BaseTestCase
  17. {
  18. protected function getCacheDir()
  19. {
  20. return __DIR__.'/.cached_responses';
  21. }
  22. public function testConstructWithInvalidPrecision()
  23. {
  24. $this->expectException(\Geocoder\Exception\InvalidArgument::class);
  25. $this->expectExceptionMessage('Invalid precision value "foo" (allowed values: "city", "country").');
  26. new IpInfoDb($this->getMockedHttpClient(), 'api_key', 'foo');
  27. }
  28. public function testGetName()
  29. {
  30. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  31. $this->assertEquals('ip_info_db', $provider->getName());
  32. }
  33. public function testGeocodeWithRandomString()
  34. {
  35. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  36. $this->expectExceptionMessage('The IpInfoDb provider does not support street addresses, only IPv4 addresses.');
  37. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  38. $provider->geocodeQuery(GeocodeQuery::create('foobar'));
  39. }
  40. public function testGeocodeWithAddress()
  41. {
  42. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  43. $this->expectExceptionMessage('The IpInfoDb provider does not support street addresses, only IPv4 addresses.');
  44. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  45. $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
  46. }
  47. public function testGeocodeWithLocalhostIPv4()
  48. {
  49. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  50. $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
  51. $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
  52. $this->assertCount(1, $results);
  53. /** @var Location $result */
  54. $result = $results->first();
  55. $this->assertInstanceOf('\Geocoder\Model\Address', $result);
  56. $this->assertNull($result->getCoordinates());
  57. $this->assertNull($result->getPostalCode());
  58. $this->assertNull($result->getTimezone());
  59. $this->assertEmpty($result->getAdminLevels());
  60. $this->assertEquals('localhost', $result->getLocality());
  61. $this->assertEquals('localhost', $result->getCountry()->getName());
  62. }
  63. public function testGeocodeWithLocalhostIPv6()
  64. {
  65. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  66. $this->expectExceptionMessage('The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses.');
  67. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  68. $provider->geocodeQuery(GeocodeQuery::create('::1'));
  69. }
  70. public function testGeocodeWithRealIPv4GetsNullContent()
  71. {
  72. $this->expectException(\Geocoder\Exception\InvalidServerResponse::class);
  73. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  74. $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100'));
  75. }
  76. public function testGeocodeWithRealIPv4GetsEmptyContent()
  77. {
  78. $this->expectException(\Geocoder\Exception\InvalidServerResponse::class);
  79. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  80. $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100'));
  81. }
  82. public function testGeocodeWithRealIPv4()
  83. {
  84. if (!isset($_SERVER['IPINFODB_API_KEY'])) {
  85. $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml');
  86. }
  87. $provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY']);
  88. $results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100'));
  89. $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
  90. $this->assertCount(1, $results);
  91. /** @var Location $result */
  92. $result = $results->first();
  93. $this->assertInstanceOf('\Geocoder\Model\Address', $result);
  94. $this->assertEquals(36.154, $result->getCoordinates()->getLatitude(), '', 0.001);
  95. $this->assertEquals(-95.9928, $result->getCoordinates()->getLongitude(), '', 0.001);
  96. $this->assertEquals(74101, $result->getPostalCode());
  97. $this->assertEquals('Tulsa', $result->getLocality());
  98. $this->assertCount(1, $result->getAdminLevels());
  99. $this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName());
  100. $this->assertEquals('United States', $result->getCountry()->getName());
  101. $this->assertEquals('US', $result->getCountry()->getCode());
  102. $this->assertEquals('America/New_York', $result->getTimezone());
  103. }
  104. public function testGeocodeWithRealIPv6()
  105. {
  106. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  107. $this->expectExceptionMessage('The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses.');
  108. if (!isset($_SERVER['IPINFODB_API_KEY'])) {
  109. $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml');
  110. }
  111. $provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY']);
  112. $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.125.45.100'));
  113. }
  114. /**
  115. * @group temp
  116. */
  117. public function testGetGeocodedDataWithCountryPrecision()
  118. {
  119. if (!isset($_SERVER['IPINFODB_API_KEY'])) {
  120. $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml');
  121. }
  122. $provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY'], 'country');
  123. $results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100'));
  124. $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
  125. $this->assertCount(1, $results);
  126. /** @var Location $result */
  127. $result = $results->first();
  128. $this->assertInstanceOf('\Geocoder\Model\Address', $result);
  129. $this->assertNull($result->getCoordinates());
  130. $this->assertNull($result->getPostalCode());
  131. $this->assertNull($result->getLocality());
  132. $this->assertEmpty($result->getAdminLevels());
  133. $this->assertEquals('United States', $result->getCountry()->getName());
  134. $this->assertEquals('US', $result->getCountry()->getCode());
  135. $this->assertNull($result->getTimezone());
  136. }
  137. public function testReverse()
  138. {
  139. $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
  140. $this->expectExceptionMessage('The IpInfoDb provider is not able to do reverse geocoding.');
  141. $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key');
  142. $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
  143. }
  144. }