class BigPipeResponseAttachmentsProcessorTest

Same name in other branches
  1. 8.9.x core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php \Drupal\Tests\big_pipe\Unit\Render\BigPipeResponseAttachmentsProcessorTest
  2. 10 core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php \Drupal\Tests\big_pipe\Unit\Render\BigPipeResponseAttachmentsProcessorTest
  3. 11.x core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php \Drupal\Tests\big_pipe\Unit\Render\BigPipeResponseAttachmentsProcessorTest

@coversDefaultClass \Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor @group big_pipe

Hierarchy

Expanded class hierarchy of BigPipeResponseAttachmentsProcessorTest

File

core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php, line 25

Namespace

Drupal\Tests\big_pipe\Unit\Render
View source
class BigPipeResponseAttachmentsProcessorTest extends UnitTestCase {
    
    /**
     * @covers ::processAttachments
     *
     * @dataProvider nonHtmlResponseProvider
     */
    public function testNonHtmlResponse($response_class) {
        $big_pipe_response_attachments_processor = $this->createBigPipeResponseAttachmentsProcessor($this->prophesize(AttachmentsResponseProcessorInterface::class));
        $non_html_response = new $response_class();
        $this->expectException(\AssertionError::class);
        $big_pipe_response_attachments_processor->processAttachments($non_html_response);
    }
    public function nonHtmlResponseProvider() {
        return [
            'AjaxResponse, which implements AttachmentsInterface' => [
                AjaxResponse::class,
            ],
            'A dummy that implements AttachmentsInterface' => [
                get_class($this->prophesize(AttachmentsInterface::class)
                    ->reveal()),
            ],
        ];
    }
    
    /**
     * @covers ::processAttachments
     *
     * @dataProvider attachmentsProvider
     */
    public function testHtmlResponse(array $attachments) {
        $big_pipe_response = new BigPipeResponse(new HtmlResponse('original'));
        $big_pipe_response->setAttachments($attachments);
        // This mock is the main expectation of this test: verify that the decorated
        // service (that is this mock) never receives BigPipe placeholder
        // attachments, because it doesn't know (nor should it) how to handle them.
        $html_response_attachments_processor = $this->prophesize(AttachmentsResponseProcessorInterface::class);
        $html_response_attachments_processor->processAttachments(Argument::that(function ($response) {
            return $response instanceof HtmlResponse && empty(array_intersect([
                'big_pipe_placeholders',
                'big_pipe_nojs_placeholders',
            ], array_keys($response->getAttachments())));
        }))
            ->will(function ($args) {
            
            /** @var \Symfony\Component\HttpFoundation\Response|\Drupal\Core\Render\AttachmentsInterface $response */
            $response = $args[0];
            // Simulate its actual behavior.
            $attachments = array_diff_key($response->getAttachments(), [
                'html_response_attachment_placeholders' => TRUE,
            ]);
            $response->setContent('processed');
            $response->setAttachments($attachments);
            return $response;
        })
            ->shouldBeCalled();
        $big_pipe_response_attachments_processor = $this->createBigPipeResponseAttachmentsProcessor($html_response_attachments_processor);
        $processed_big_pipe_response = $big_pipe_response_attachments_processor->processAttachments($big_pipe_response);
        // The secondary expectation of this test: the original (passed in) response
        // object remains unchanged, the processed (returned) response object has
        // the expected values.
        $this->assertSame($attachments, $big_pipe_response->getAttachments(), 'Attachments of original response object MUST NOT be changed.');
        $this->assertEquals('original', $big_pipe_response->getContent(), 'Content of original response object MUST NOT be changed.');
        $this->assertEquals(array_diff_key($attachments, [
            'html_response_attachment_placeholders' => TRUE,
        ]), $processed_big_pipe_response->getAttachments(), 'Attachments of returned (processed) response object MUST be changed.');
        $this->assertEquals('processed', $processed_big_pipe_response->getContent(), 'Content of returned (processed) response object MUST be changed.');
    }
    public function attachmentsProvider() {
        $typical_cases = [
            'no attachments' => [
                [],
            ],
            'libraries' => [
                [
                    'library' => [
                        'core/drupal',
                    ],
                ],
            ],
            'libraries + drupalSettings' => [
                [
                    'library' => [
                        'core/drupal',
                    ],
                    'drupalSettings' => [
                        'foo' => 'bar',
                    ],
                ],
            ],
        ];
        $official_attachment_types = [
            'html_head',
            'feed',
            'html_head_link',
            'http_header',
            'library',
            'placeholders',
            'drupalSettings',
            'html_response_attachment_placeholders',
        ];
        $official_attachments_with_random_values = [];
        foreach ($official_attachment_types as $type) {
            $official_attachments_with_random_values[$type] = $this->randomMachineName();
        }
        $random_attachments = [
            'random' . $this->randomMachineName() => $this->randomMachineName(),
        ];
        $edge_cases = [
            'all official attachment types, with random assigned values, even if technically not valid, to prove BigPipeResponseAttachmentsProcessor is a perfect decorator' => [
                $official_attachments_with_random_values,
            ],
            'random attachment type (unofficial), with random assigned value, to prove BigPipeResponseAttachmentsProcessor is a perfect decorator' => [
                $random_attachments,
            ],
        ];
        $big_pipe_placeholder_attachments = [
            'big_pipe_placeholders' => [
                $this->randomMachineName(),
            ],
        ];
        $big_pipe_nojs_placeholder_attachments = [
            'big_pipe_nojs_placeholders' => [
                $this->randomMachineName(),
            ],
        ];
        $big_pipe_cases = [
            'only big_pipe_placeholders' => [
                $big_pipe_placeholder_attachments,
            ],
            'only big_pipe_nojs_placeholders' => [
                $big_pipe_nojs_placeholder_attachments,
            ],
            'big_pipe_placeholders + big_pipe_nojs_placeholders' => [
                $big_pipe_placeholder_attachments + $big_pipe_nojs_placeholder_attachments,
            ],
        ];
        $combined_cases = [
            'all official attachment types + big_pipe_placeholders + big_pipe_nojs_placeholders' => [
                $official_attachments_with_random_values + $big_pipe_placeholder_attachments + $big_pipe_nojs_placeholder_attachments,
            ],
            'random attachment types + big_pipe_placeholders + big_pipe_nojs_placeholders' => [
                $random_attachments + $big_pipe_placeholder_attachments + $big_pipe_nojs_placeholder_attachments,
            ],
        ];
        return $typical_cases + $edge_cases + $big_pipe_cases + $combined_cases;
    }
    
    /**
     * Creates a BigPipeResponseAttachmentsProcessor with mostly dummies.
     *
     * @param \Prophecy\Prophecy\ObjectProphecy $decorated_html_response_attachments_processor
     *   An object prophecy implementing AttachmentsResponseProcessorInterface.
     *
     * @return \Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor
     *   The BigPipeResponseAttachmentsProcessor to test.
     */
    protected function createBigPipeResponseAttachmentsProcessor(ObjectProphecy $decorated_html_response_attachments_processor) {
        return new BigPipeResponseAttachmentsProcessor($decorated_html_response_attachments_processor->reveal(), $this->prophesize(AssetResolverInterface::class)
            ->reveal(), $this->prophesize(ConfigFactoryInterface::class)
            ->reveal(), $this->prophesize(AssetCollectionRendererInterface::class)
            ->reveal(), $this->prophesize(AssetCollectionRendererInterface::class)
            ->reveal(), $this->prophesize(RequestStack::class)
            ->reveal(), $this->prophesize(RendererInterface::class)
            ->reveal(), $this->prophesize(ModuleHandlerInterface::class)
            ->reveal());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
BigPipeResponseAttachmentsProcessorTest::attachmentsProvider public function
BigPipeResponseAttachmentsProcessorTest::createBigPipeResponseAttachmentsProcessor protected function Creates a BigPipeResponseAttachmentsProcessor with mostly dummies.
BigPipeResponseAttachmentsProcessorTest::nonHtmlResponseProvider public function
BigPipeResponseAttachmentsProcessorTest::testHtmlResponse public function @covers ::processAttachments
BigPipeResponseAttachmentsProcessorTest::testNonHtmlResponse public function @covers ::processAttachments
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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 338
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.