class FormassemblyHtmlResponseAttachmentsProcessorTest in FormAssembly 8
Unit test calls for FormassemblyHtmlResponseAttachmentsProcessor.
@author Shawn P. Duncan <code@sd.shawnduncan.org>
Copyright 2019 by Shawn P. Duncan. This code is released under the GNU General Public License. Which means that it is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.html @coversDefaultClass \Drupal\formassembly\Component\Render\FormassemblyHtmlResponseAttachmentsProcessor @group formassembly
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\formassembly\Unit\FormassemblyHtmlResponseAttachmentsProcessorTest
Expanded class hierarchy of FormassemblyHtmlResponseAttachmentsProcessorTest
File
- tests/
src/ Unit/ FormassemblyHtmlResponseAttachmentsProcessorTest.php, line 31
Namespace
Drupal\Tests\formassembly\UnitView source
class FormassemblyHtmlResponseAttachmentsProcessorTest extends UnitTestCase {
/**
* A mock core html attachment processor.
*
* @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $attachmentProcessor;
/**
* 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->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 fa_form_attachments attachments and ensure that
// they are properly 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();
$attachments['fa_form_attachments'] = [
0 => [
'#type' => 'fa_form_inline_js',
'#value' => "console.log('test')",
'#weight' => 6,
],
1 => [
'#type' => 'fa_form_external_css',
'#rel' => 'stylesheet',
'#href' => 'http://example.com/example.css',
'#weight' => 1,
],
2 => [
'#type' => 'fa_form_external_js',
'#src' => 'http://example.com/example.js',
'#weight' => 0,
],
];
$response
->setAttachments($attachments);
$config_factory = $this
->getConfigFactoryStub([
'formassembly.api.oauth' => [
'instance' => NULL,
],
]);
$response = $this
->getFormassemblyAttachmentProcessor($config_factory)
->processAttachments($response);
$processedAttachments = $response
->getAttachments();
$this
->assertEquals('fa_form_external_js', $processedAttachments["html_head"][0][0]["#type"]);
$this
->assertEquals('fa_form_external_css', $processedAttachments["html_head"][1][0]["#type"]);
$this
->assertEquals('fa_form_inline_js', $processedAttachments["html_head"][2][0]["#type"]);
$this
->assertArrayNotHasKey('fa_form_attachments', $processedAttachments, 'The fa_form_attachments attachments are converted to html_head attachments.');
}
/**
* @covers ::processAttachments
*/
public function testProcessAttachmentsNoAdditions() {
// Ensure that if there are no fa_form_attachments
// nothing is added to the attachments.
$response = new HtmlResponse();
$config_factory = $this
->getConfigFactoryStub();
$response = $this
->getFormassemblyAttachmentProcessor($config_factory)
->processAttachments($response);
$processedAttachments = $response
->getAttachments();
$this
->assertEmpty($processedAttachments);
}
/**
* Creates a FormassemblyHtmlResponseAttachmentsProcessor object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A mock config factory that can contain 'dfp.settings' configuration.
*
* @return \Drupal\formassembly\Component\Render\FormassemblyHtmlResponseAttachmentsProcessor
* The DfpHtmlResponseAttachmentsProcessor object.
*/
protected function getFormassemblyAttachmentProcessor(ConfigFactoryInterface $config_factory) {
return new FormassemblyHtmlResponseAttachmentsProcessor($this->attachmentProcessor, $this->assetResolver
->reveal(), $config_factory, $this->cssCollectionRenderer
->reveal(), $this->jsCollectionRenderer
->reveal(), $this->requestStack
->reveal(), $this->renderer
->reveal(), $this->moduleHandler
->reveal());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock asset resolver service. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock core html attachment processor. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock CSS collection renderer. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock JS collection renderer. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock module handler. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock renderer. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | property | A mock RequestStack. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | function | Creates a FormassemblyHtmlResponseAttachmentsProcessor object. | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
public | function | @covers ::processAttachments | |
FormassemblyHtmlResponseAttachmentsProcessorTest:: |
public | function | @covers ::processAttachments | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |