You are here

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

Expanded class hierarchy of FormassemblyHtmlResponseAttachmentsProcessorTest

File

tests/src/Unit/FormassemblyHtmlResponseAttachmentsProcessorTest.php, line 31

Namespace

Drupal\Tests\formassembly\Unit
View 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

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