You are here

class TagViewTest in Doubleclick for Publishers (DFP) 8

@coversDefaultClass \Drupal\dfp\View\TagView @group dfp

Hierarchy

Expanded class hierarchy of TagViewTest

File

tests/src/Unit/View/TagViewTest.php, line 20
Contains \Drupal\Tests\dfp\Unit\View\TagViewTest.

Namespace

Drupal\Tests\dfp\Unit\View
View source
class TagViewTest extends UnitTestCase {

  /**
   * @covers ::formatSize
   * @dataProvider formatSizeProvider
   */
  public function testFormatSize($size, $expected) {
    $this
      ->assertSame($expected, TagView::formatSize($size));
  }

  /**
   * Data provider for self::testFormatSize().
   */
  public function formatSizeProvider() {
    return [
      [
        '<none>',
        '[]',
      ],
      [
        'fluid',
        "'fluid'",
      ],
      [
        '300x250 ',
        '[300, 250]',
      ],
      [
        '300x250, 728x90 ',
        '[[300, 250], [728, 90]]',
      ],
      [
        '300x250, 728x90, fluid',
        "[[300, 250], [728, 90], 'fluid']",
      ],
    ];
  }

  /**
   * @covers ::getSlug
   * @dataProvider getSlugProvider
   */
  public function testGetSlug($tag_slug, $default_slug, $expected_slug) {
    $tag = $this
      ->prophesize(TagInterface::class);
    $tag
      ->slug()
      ->willReturn($tag_slug);
    $config_factory = $this
      ->getConfigFactoryStub([
      'dfp.settings' => [
        'default_slug' => $default_slug,
      ],
    ]);
    $token = $this
      ->prophesize(TokenInterface::class)
      ->reveal();
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $tag_view = new TagView($tag
      ->reveal(), $config_factory
      ->get('dfp.settings'), $token, $module_handler);
    $this
      ->assertSame($expected_slug, $tag_view
      ->getSlug());
  }

  /**
   * Data provider for self::testGetSlug().
   */
  public function getSlugProvider() {
    return [
      [
        'slug',
        'default_slug',
        'slug',
      ],
      [
        '',
        'default_slug',
        'default_slug',
      ],
      [
        '<none>',
        'default_slug',
        '',
      ],
    ];
  }

  /**
   * @covers ::getAdUnit
   * @dataProvider getAdUnitProvider
   */
  public function testGetAdUnit($tag_ad_unit, $default_ad_unit, $network_id, $expected_adunit) {
    $tag = $this
      ->prophesize(TagInterface::class);
    $tag
      ->adunit()
      ->willReturn($tag_ad_unit);
    $config_factory = $this
      ->getConfigFactoryStub([
      'dfp.settings' => [
        'adunit_pattern' => $default_ad_unit,
        'network_id' => $network_id,
      ],
    ]);
    $token = $this
      ->createMock(TokenInterface::class);
    $token
      ->method('replace')
      ->willReturnArgument(0);
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $tag_view = new TagView($tag
      ->reveal(), $config_factory
      ->get('dfp.settings'), $token, $module_handler);
    $this
      ->assertSame($expected_adunit, $tag_view
      ->getAdUnit());
  }

  /**
   * Data provider for self::testGetAdUnit().
   */
  public function getAdUnitProvider() {
    return [
      [
        'adunit',
        'default_adunit',
        '12345',
        '/12345/adunit',
      ],
      [
        '',
        'default_adunit',
        '67890',
        '/67890/default_adunit',
      ],
    ];
  }

  /**
   * @covers ::getShortTagQueryString
   * @dataProvider getShortTagQueryStringProvider
   */
  public function testGetShortTagQueryString($tag_ad_unit, $tag_sizes, $tag_targeting, $network_id, $regex) {
    $tag = $this
      ->prophesize(TagInterface::class);
    $tag
      ->adunit()
      ->willReturn($tag_ad_unit);
    $tag
      ->size()
      ->willReturn($tag_sizes);
    $tag
      ->targeting()
      ->willReturn($tag_targeting);
    $config_factory = $this
      ->getConfigFactoryStub([
      'dfp.settings' => [
        'adunit_pattern' => 'default_adunit',
        'network_id' => $network_id,
      ],
    ]);
    $token = $this
      ->createMock(TokenInterface::class);
    $token
      ->method('replace')
      ->willReturnArgument(0);
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $tag_view = new TagView($tag
      ->reveal(), $config_factory
      ->get('dfp.settings'), $token, $module_handler);
    $this
      ->assertRegExp($regex, $tag_view
      ->getShortTagQueryString());
  }

  /**
   * Data provider for self::testGetShortTagQueryString().
   */
  public function getShortTagQueryStringProvider() {
    return [
      [
        'adunit',
        '300x200',
        [],
        '12345',
        '|^iu=/12345/adunit&sz=300x200&c=[0-9]{5}$|',
      ],
      [
        'adunit',
        '300x200',
        [
          [
            'target' => 'target',
            'value' => 'value,value2',
          ],
        ],
        '12345',
        '|^iu=/12345/adunit&sz=300x200&c=[0-9]{5}&t=target%3Dvalue%2Cvalue2$|',
      ],
      [
        'adunit',
        '300x200',
        [
          [
            'target' => 'target',
            'value' => 'value,value2',
          ],
          [
            'target' => 'target2',
            'value' => 'value3',
          ],
        ],
        '12345',
        '|^iu=/12345/adunit&sz=300x200&c=[0-9]{5}&t=target%3Dvalue%2Cvalue2%26target2%3Dvalue3$|',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TagViewTest::formatSizeProvider public function Data provider for self::testFormatSize().
TagViewTest::getAdUnitProvider public function Data provider for self::testGetAdUnit().
TagViewTest::getShortTagQueryStringProvider public function Data provider for self::testGetShortTagQueryString().
TagViewTest::getSlugProvider public function Data provider for self::testGetSlug().
TagViewTest::testFormatSize public function @covers ::formatSize @dataProvider formatSizeProvider
TagViewTest::testGetAdUnit public function @covers ::getAdUnit @dataProvider getAdUnitProvider
TagViewTest::testGetShortTagQueryString public function @covers ::getShortTagQueryString @dataProvider getShortTagQueryStringProvider
TagViewTest::testGetSlug public function @covers ::getSlug @dataProvider getSlugProvider
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