You are here

class DfpHtmlResponseAttachmentsProcessorTest in Doubleclick for Publishers (DFP) 8

@coversDefaultClass \Drupal\dfp\DfpHtmlResponseAttachmentsProcessor @group dfp

Hierarchy

Expanded class hierarchy of DfpHtmlResponseAttachmentsProcessorTest

File

tests/src/Unit/DfpHtmlResponseAttachmentsProcessorTest.php, line 27
Contains \Drupal\Tests\dfp\Unit\DfpHtmlResponseAttachmentsProcessorTest.

Namespace

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

  /**
   * A mock core html attachment processor.
   *
   * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $attachmentProcessor;

  /**
   * A mock DFP token service.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $token;

  /**
   * A mock asset resolver service.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $assetResolver;

  /**
   * A mock CSS collection renderer.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $cssCollectionRenderer;

  /**
   * A mock JS collection renderer.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $jsCollectionRenderer;

  /**
   * A mock RequestStack.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $requestStack;

  /**
   * A mock renderer.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $renderer;

  /**
   * A mock module handler.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Mock core attachment processor.
    $this->attachmentProcessor = $this
      ->getMockBuilder(AttachmentsResponseProcessorInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->attachmentProcessor
      ->method('processAttachments')
      ->willReturnArgument(0);
    $this->token = $this
      ->prophesize(TokenInterface::class);
    $this->assetResolver = $this
      ->prophesize(AssetResolverInterface::class);
    $this->cssCollectionRenderer = $this
      ->prophesize(AssetCollectionRendererInterface::class);
    $this->jsCollectionRenderer = $this
      ->prophesize(AssetCollectionRendererInterface::class);
    $this->requestStack = $this
      ->prophesize(RequestStack::class);
    $this->renderer = $this
      ->prophesize(RendererInterface::class);
    $this->moduleHandler = $this
      ->prophesize(ModuleHandlerInterface::class);
  }

  /**
   * @covers ::processAttachments
   */
  public function testProcessAttachments() {

    // Create a response with two dfp_slot attachments and ensure that the
    // they are converted to html_head attachments. Also ensure that the other
    // html_head elements are added to create the necessary javascript in the
    // right order.
    $response = new HtmlResponse();
    for ($i = 1; $i < 3; $i++) {
      $tag = $this
        ->prophesize(TagView::class);
      $tag
        ->id()
        ->willReturn($i);
      $attachments['dfp_slot'][] = $tag
        ->reveal();
    }
    $response
      ->setAttachments($attachments);
    $config_factory = $this
      ->getConfigFactoryStub([
      'dfp.settings' => [
        'targeting' => [],
      ],
    ]);
    $response = $this
      ->getDfpAttachmentProcessor($config_factory)
      ->processAttachments($response);
    $this
      ->assertEquals('dfp-js-head-top', $response
      ->getAttachments()['html_head'][0][1]);
    $this
      ->assertEquals('dfp-slot-definition-1', $response
      ->getAttachments()['html_head'][1][1]);
    $this
      ->assertEquals('dfp-slot-definition-2', $response
      ->getAttachments()['html_head'][2][1]);
    $this
      ->assertEquals('dfp-js-head-bottom', $response
      ->getAttachments()['html_head'][3][1]);
    $this
      ->assertArrayNotHasKey('dfp_slot', $response
      ->getAttachments(), 'The dfp_slot attachments are converted to html_head attachments.');
  }

  /**
   * @covers ::processAttachments
   */
  public function testProcessAttachmentsNoSlots() {

    // Ensure that if there are no slots nothing is added to the attachments.
    $response = new HtmlResponse();
    $config_factory = $this
      ->getConfigFactoryStub();
    $response = $this
      ->getDfpAttachmentProcessor($config_factory)
      ->processAttachments($response);
    $this
      ->assertEmpty($response
      ->getAttachments());
  }

  /**
   * Creates a DfpHtmlResponseAttachmentsProcessor object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A mock config factory that can contain 'dfp.settings' configuration.
   *
   * @return \Drupal\dfp\DfpHtmlResponseAttachmentsProcessor
   *   The DfpHtmlResponseAttachmentsProcessor object.
   */
  protected function getDfpAttachmentProcessor(ConfigFactoryInterface $config_factory) {
    return new DfpHtmlResponseAttachmentsProcessor($this->attachmentProcessor, $this->token
      ->reveal(), $this->assetResolver
      ->reveal(), $config_factory, $this->cssCollectionRenderer
      ->reveal(), $this->jsCollectionRenderer
      ->reveal(), $this->requestStack
      ->reveal(), $this->renderer
      ->reveal(), $this->moduleHandler
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DfpHtmlResponseAttachmentsProcessorTest::$assetResolver protected property A mock asset resolver service.
DfpHtmlResponseAttachmentsProcessorTest::$attachmentProcessor protected property A mock core html attachment processor.
DfpHtmlResponseAttachmentsProcessorTest::$cssCollectionRenderer protected property A mock CSS collection renderer.
DfpHtmlResponseAttachmentsProcessorTest::$jsCollectionRenderer protected property A mock JS collection renderer.
DfpHtmlResponseAttachmentsProcessorTest::$moduleHandler protected property A mock module handler.
DfpHtmlResponseAttachmentsProcessorTest::$renderer protected property A mock renderer.
DfpHtmlResponseAttachmentsProcessorTest::$requestStack protected property A mock RequestStack.
DfpHtmlResponseAttachmentsProcessorTest::$token protected property A mock DFP token service.
DfpHtmlResponseAttachmentsProcessorTest::getDfpAttachmentProcessor protected function Creates a DfpHtmlResponseAttachmentsProcessor object.
DfpHtmlResponseAttachmentsProcessorTest::setUp protected function Overrides UnitTestCase::setUp
DfpHtmlResponseAttachmentsProcessorTest::testProcessAttachments public function @covers ::processAttachments
DfpHtmlResponseAttachmentsProcessorTest::testProcessAttachmentsNoSlots public function @covers ::processAttachments
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.