You are here

class GeoLocationTest in GeoIP API 8.2

Tests the geolocator plugin manager.

@coversDefaultClass \Drupal\geoip\GeoLocation

@group GeoIP

Hierarchy

Expanded class hierarchy of GeoLocationTest

File

tests/src/Unit/GeoLocationTest.php, line 20

Namespace

Drupal\Tests\geoip\Unit
View 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

Namesort descending Modifiers Type Description Overrides
GeoLocationTest::testGeolocate public function Test getGeoLocator.
GeoLocationTest::testGetGeoLocator public function Test getGeoLocator.
GeoLocationTest::testGetGeoLocatorId public function Test getGeoLocatorId.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340