‪TYPO3CMS  11.5
AssetRendererTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 class ‪AssetRendererTest extends UnitTestCase
27 {
29  protected ?EventDispatcherInterface ‪$eventDispatcher;
30 
31  public function ‪setUp(): void
32  {
33  parent::setUp();
34  $this->resetSingletonInstances = true;
35  $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
36  $this->assetRenderer = GeneralUtility::makeInstance(
37  AssetRenderer::class,
38  null,
39  $this->eventDispatcher
40  );
41  }
42 
50  public function ‪styleSheets(array $files, array $expectedResult, array $expectedMarkup): void
51  {
52  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
53  foreach ($files as $file) {
54  [$identifier, $source, $attributes, $options] = $file;
55  $assetCollector->addStyleSheet($identifier, $source, $attributes, $options);
56  }
57  self::assertSame($expectedMarkup['css_no_prio'], $this->assetRenderer->renderStyleSheets());
58  self::assertSame($expectedMarkup['css_prio'], $this->assetRenderer->renderStyleSheets(true));
59  }
60 
68  public function ‪javaScript(array $files, array $expectedResult, array $expectedMarkup): void
69  {
70  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
71  foreach ($files as $file) {
72  [$identifier, $source, $attributes, $options] = $file;
73  $assetCollector->addJavaScript($identifier, $source, $attributes, $options);
74  }
75  self::assertSame($expectedMarkup['js_no_prio'], $this->assetRenderer->renderJavaScript());
76  self::assertSame($expectedMarkup['js_prio'], $this->assetRenderer->renderJavaScript(true));
77  }
78 
86  public function ‪inlineJavaScript(array $sources, array $expectedResult, array $expectedMarkup): void
87  {
88  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
89  foreach ($sources as $source) {
90  [$identifier, $source, $attributes, $options] = $source;
91  $assetCollector->addInlineJavaScript($identifier, $source, $attributes, $options);
92  }
93  self::assertSame($expectedMarkup['js_no_prio'], $this->assetRenderer->renderInlineJavaScript());
94  self::assertSame($expectedMarkup['js_prio'], $this->assetRenderer->renderInlineJavaScript(true));
95  }
96 
104  public function ‪inlineStyleSheets(array $sources, array $expectedResult, array $expectedMarkup): void
105  {
106  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
107  foreach ($sources as $source) {
108  [$identifier, $source, $attributes, $options] = $source;
109  $assetCollector->addInlineStyleSheet($identifier, $source, $attributes, $options);
110  }
111  self::assertSame($expectedMarkup['css_no_prio'], $this->assetRenderer->renderInlineStyleSheets());
112  self::assertSame($expectedMarkup['css_prio'], $this->assetRenderer->renderInlineStyleSheets(true));
113  }
114 
123  public function ‪beforeRenderingEvent(
124  string $renderMethodName,
125  bool $isInline,
126  bool $priority,
127  string $eventClassName
128  ): void {
129  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
130  $event = new $eventClassName(
131  $assetCollector,
132  $isInline,
133  $priority
134  );
135 
136  $this->eventDispatcher
137  ->expects(self::once())
138  ->method('dispatch')
139  ->with($event);
140 
141  $this->assetRenderer->$renderMethodName($priority);
142  }
143 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\beforeRenderingEvent
‪beforeRenderingEvent(string $renderMethodName, bool $isInline, bool $priority, string $eventClassName)
Definition: AssetRendererTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Page
Definition: AssetCollectorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: AssetRendererTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\inlineStyleSheets
‪inlineStyleSheets(array $sources, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\setUp
‪setUp()
Definition: AssetRendererTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\styleSheets
‪styleSheets(array $files, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:50
‪TYPO3\CMS\Core\Page\AssetRenderer
Definition: AssetRenderer.php:30
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\inlineJavaScript
‪inlineJavaScript(array $sources, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\$assetRenderer
‪AssetRenderer $assetRenderer
Definition: AssetRendererTest.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest
Definition: AssetRendererTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\javaScript
‪javaScript(array $files, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:68