class GeoLocationTest in GeoIP API 8.2
Tests the geolocator plugin manager.
@coversDefaultClass \Drupal\geoip\GeoLocation
@group GeoIP
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\geoip\Unit\GeoLocationTest
Expanded class hierarchy of GeoLocationTest
File
- tests/
src/ Unit/ GeoLocationTest.php, line 20
Namespace
Drupal\Tests\geoip\UnitView source
class GeoLocationTest extends UnitTestCase {
/**
* Test getGeoLocatorId.
*
* @covers ::getGeoLocatorId
*/
public function testGetGeoLocatorId() {
$geolocators_manager = $this
->prophesize(GeoLocatorManager::class);
$config = $this
->prophesize(ImmutableConfig::class);
$config
->get('debug')
->willReturn(FALSE);
$config
->get('plugin_id')
->willReturn('local');
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('geoip.geolocation')
->willReturn($config
->reveal());
$cache_backend = $this
->prophesize(CacheBackendInterface::class);
$geolocation = new GeoLocation($geolocators_manager
->reveal(), $config_factory
->reveal(), $cache_backend
->reveal());
$this
->assertEquals('local', $geolocation
->getGeoLocatorId());
}
/**
* Test getGeoLocator.
*
* @covers ::getGeoLocator
*/
public function testGetGeoLocator() {
$geolocators_manager = $this
->prophesize(GeoLocatorManager::class);
$config = $this
->prophesize(ImmutableConfig::class);
$config
->get('debug')
->willReturn(FALSE);
$config
->get('plugin_id')
->willReturn('local');
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('geoip.geolocation')
->willReturn($config
->reveal());
$cache_backend = $this
->prophesize(CacheBackendInterface::class);
$geolocators_manager
->createInstance('local')
->willReturn($this
->prophesize(GeoLocatorInterface::class)
->reveal());
$geolocation = new GeoLocation($geolocators_manager
->reveal(), $config_factory
->reveal(), $cache_backend
->reveal());
$locator = $geolocation
->getGeoLocator();
$this
->assertTrue($locator instanceof GeoLocatorInterface);
}
/**
* Test getGeoLocator.
*
* @covers ::geolocate
*/
public function testGeolocate() {
$geolocators_manager = $this
->prophesize(GeoLocatorManager::class);
$config = $this
->prophesize(ImmutableConfig::class);
$config
->get('debug')
->willReturn(FALSE);
$config
->get('plugin_id')
->willReturn('local');
$config_factory = $this
->prophesize(ConfigFactoryInterface::class);
$config_factory
->get('geoip.geolocation')
->willReturn($config
->reveal());
$cache_backend = $this
->prophesize(CacheBackendInterface::class);
$locator = $this
->prophesize(GeoLocatorInterface::class);
$locator
->geolocate('127.0.0.1')
->willReturn(NULL);
$locator
->geolocate('2605:a000:140d:c18f:5995:dfe1:7914:4b4f')
->willReturn('US');
$locator
->geolocate('23.86.161.12')
->willReturn('CA');
$geolocators_manager
->createInstance('local')
->willReturn($locator
->reveal());
$geolocation = new GeoLocation($geolocators_manager
->reveal(), $config_factory
->reveal(), $cache_backend
->reveal());
$this
->assertNull($geolocation
->geolocate('127.0.0.1'));
$this
->assertEquals($geolocation
->geolocate('2605:a000:140d:c18f:5995:dfe1:7914:4b4f'), 'US');
$this
->assertEquals($geolocation
->geolocate('23.86.161.12'), 'CA');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GeoLocationTest:: |
public | function | Test getGeoLocator. | |
GeoLocationTest:: |
public | function | Test getGeoLocator. | |
GeoLocationTest:: |
public | function | Test getGeoLocatorId. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |