DfpHtmlResponseAttachmentsProcessorTest.php in Doubleclick for Publishers (DFP) 8
File
tests/src/Unit/DfpHtmlResponseAttachmentsProcessorTest.php
View source
<?php
namespace Drupal\Tests\dfp\Unit;
use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\RendererInterface;
use Drupal\dfp\DfpHtmlResponseAttachmentsProcessor;
use Drupal\dfp\TokenInterface;
use Drupal\dfp\View\TagView;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
class DfpHtmlResponseAttachmentsProcessorTest extends UnitTestCase {
protected $attachmentProcessor;
protected $token;
protected $assetResolver;
protected $cssCollectionRenderer;
protected $jsCollectionRenderer;
protected $requestStack;
protected $renderer;
protected $moduleHandler;
protected function setUp() {
parent::setUp();
$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);
}
public function testProcessAttachments() {
$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.');
}
public function testProcessAttachmentsNoSlots() {
$response = new HtmlResponse();
$config_factory = $this
->getConfigFactoryStub();
$response = $this
->getDfpAttachmentProcessor($config_factory)
->processAttachments($response);
$this
->assertEmpty($response
->getAttachments());
}
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());
}
}