You are here

class EdgescapeTest in Akamai 8.3

Same name in this branch
  1. 8.3 tests/src/Functional/EdgescapeTest.php \Drupal\Tests\akamai\Functional\EdgescapeTest
  2. 8.3 tests/src/Unit/Helper/EdgescapeTest.php \Drupal\Tests\akamai\Unit\Helper\EdgescapeTest

Edgescape Helper Tests.

@group Akamai

Hierarchy

Expanded class hierarchy of EdgescapeTest

File

tests/src/Unit/Helper/EdgescapeTest.php, line 15

Namespace

Drupal\Tests\akamai\Unit\Helper
View source
class EdgescapeTest extends UnitTestCase {

  /**
   * Get a mock config factory.
   */
  public function getConfigFactory($edgescapeEnabled = FALSE) {
    $config = [
      'edgescape_support' => $edgescapeEnabled,
    ];
    return $this
      ->getConfigFactoryStub([
      'akamai.settings' => $config,
    ]);
  }

  /**
   * Get a mock request stack with request.
   */
  public function getRequestStack($headerValue = NULL) {
    $request = new Request();
    if ($headerValue) {
      $request->headers = new HeaderBag([
        Edgescape::EDGESCAPE_HEADER => (string) $headerValue,
      ]);
    }
    $requestStack = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\RequestStack')
      ->disableOriginalConstructor()
      ->setMethods([
      'getCurrentRequest',
    ])
      ->getMock();
    $requestStack
      ->method('getCurrentRequest')
      ->willReturn($request);
    return $requestStack;
  }

  /**
   * Get an Edgescape header value.
   */
  public function getEdgescapeHeaderValue($info = []) {
    $info = $info + [
      'georegion' => '263',
      'country_code' => 'US',
      'region_code' => 'MA',
      'city' => 'CAMBRIDGE',
      'dma' => '506',
      'pmsa' => '1120',
      'areacode' => '617',
      'county' => 'MIDDLESEX',
      'fips' => '25017',
      'lat' => '42.3933',
      'long' => '-71.1333',
      'timezone' => 'EST',
      'zip' => '02138-02142',
      'continent' => 'NA',
      'throughput' => 'vhigh',
      'asnum' => '21399',
    ];
    return http_build_query($info, '', ',');
  }

  /**
   * Tests getEdgescapeHeaderValue creating a valid header value.
   */
  public function testGetEdgescapeHeaderValue() {
    $expected = 'georegion=263,country_code=US,region_code=MA,city=CAMBRIDGE,dma=506,pmsa=1120,areacode=617,county=MIDDLESEX,fips=25017,lat=42.3933,long=-71.1333,timezone=EST,zip=02138-02142,continent=NA,throughput=vhigh,asnum=21399';
    $this
      ->assertSame($this
      ->getEdgescapeHeaderValue(), $expected);
  }

  /**
   * Tests getInformationByType returns empty.
   */
  public function testGetInformationByTypeReturnsEmpty() {

    // Config false and header not present.
    $config = $this
      ->getConfigFactory();
    $requestStack = $this
      ->getRequestStack();
    $helper = new Edgescape($config, $requestStack);
    $this
      ->assertSame($helper
      ->getInformationByType('country_code'), '');

    // Config false and header present.
    $config = $this
      ->getConfigFactory();
    $requestStack = $this
      ->getRequestStack($this
      ->getEdgescapeHeaderValue());
    $helper = new Edgescape($config, $requestStack);
    $this
      ->assertSame($helper
      ->getInformationByType('country_code'), '');

    // Config true and header not present.
    $config = $this
      ->getConfigFactory(TRUE);
    $requestStack = $this
      ->getRequestStack();
    $helper = new Edgescape($config, $requestStack);
    $this
      ->assertSame($helper
      ->getInformationByType('country_code'), '');
  }

  /**
   * Tests getInformationByType returns values.
   */
  public function testGetInformationByTypeReturnsValues() {

    // Config true and header present.
    $config = $this
      ->getConfigFactory(TRUE);
    $requestStack = $this
      ->getRequestStack($this
      ->getEdgescapeHeaderValue());
    $helper = new Edgescape($config, $requestStack);
    $this
      ->assertSame($helper
      ->getInformationByType('country_code'), 'US');
    $this
      ->assertSame($helper
      ->getInformationByType('continent'), 'NA');
    $this
      ->assertSame($helper
      ->getInformationByType('foo'), '');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EdgescapeTest::getConfigFactory public function Get a mock config factory.
EdgescapeTest::getEdgescapeHeaderValue public function Get an Edgescape header value.
EdgescapeTest::getRequestStack public function Get a mock request stack with request.
EdgescapeTest::testGetEdgescapeHeaderValue public function Tests getEdgescapeHeaderValue creating a valid header value.
EdgescapeTest::testGetInformationByTypeReturnsEmpty public function Tests getInformationByType returns empty.
EdgescapeTest::testGetInformationByTypeReturnsValues public function Tests getInformationByType returns values.
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