‪TYPO3CMS  10.4
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 {
31  protected ‪$assetRenderer;
32 
37 
38  public function ‪setUp(): void
39  {
40  parent::setUp();
41  $this->resetSingletonInstances = true;
42  $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
43  $this->assetRenderer = GeneralUtility::makeInstance(
44  AssetRenderer::class,
45  null,
46  $this->eventDispatcher
47  );
48  }
49 
56  public function ‪testStyleSheets(array $files, array $expectedResult, array $expectedMarkup): void
57  {
58  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
59  foreach ($files as $file) {
60  [$identifier, $source, $attributes, $options] = $file;
61  $assetCollector->addStyleSheet($identifier, $source, $attributes, $options);
62  }
63  self::assertSame($expectedMarkup['css_no_prio'], $this->assetRenderer->renderStyleSheets());
64  self::assertSame($expectedMarkup['css_prio'], $this->assetRenderer->renderStyleSheets(true));
65  }
66 
73  public function ‪testJavaScript(array $files, array $expectedResult, array $expectedMarkup): void
74  {
75  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
76  foreach ($files as $file) {
77  [$identifier, $source, $attributes, $options] = $file;
78  $assetCollector->addJavaScript($identifier, $source, $attributes, $options);
79  }
80  self::assertSame($expectedMarkup['js_no_prio'], $this->assetRenderer->renderJavaScript());
81  self::assertSame($expectedMarkup['js_prio'], $this->assetRenderer->renderJavaScript(true));
82  }
83 
90  public function ‪testInlineJavaScript(array $sources, array $expectedResult, array $expectedMarkup): void
91  {
92  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
93  foreach ($sources as $source) {
94  [$identifier, $source, $attributes, $options] = $source;
95  $assetCollector->addInlineJavaScript($identifier, $source, $attributes, $options);
96  }
97  self::assertSame($expectedMarkup['js_no_prio'], $this->assetRenderer->renderInlineJavaScript());
98  self::assertSame($expectedMarkup['js_prio'], $this->assetRenderer->renderInlineJavaScript(true));
99  }
100 
107  public function ‪testInlineStyleSheets(array $sources, array $expectedResult, array $expectedMarkup): void
108  {
109  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
110  foreach ($sources as $source) {
111  [$identifier, $source, $attributes, $options] = $source;
112  $assetCollector->addInlineStyleSheet($identifier, $source, $attributes, $options);
113  }
114  self::assertSame($expectedMarkup['css_no_prio'], $this->assetRenderer->renderInlineStyleSheets());
115  self::assertSame($expectedMarkup['css_prio'], $this->assetRenderer->renderInlineStyleSheets(true));
116  }
117 
125  public function ‪testBeforeRenderingEvent(
126  string $renderMethodName,
127  bool $isInline,
128  bool $priority,
129  string $eventClassName
130  ): void {
131  $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
132  $event = new $eventClassName(
133  $assetCollector,
134  $isInline,
135  $priority
136  );
137 
138  $this->eventDispatcher
139  ->expects(self::once())
140  ->method('dispatch')
141  ->with($event);
142 
143  $this->assetRenderer->$renderMethodName($priority);
144  }
145 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Core\Tests\Unit\Page
Definition: AssetCollectorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\testInlineStyleSheets
‪testInlineStyleSheets(array $sources, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: AssetRendererTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\setUp
‪setUp()
Definition: AssetRendererTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\testJavaScript
‪testJavaScript(array $files, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\testStyleSheets
‪testStyleSheets(array $files, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:54
‪TYPO3\CMS\Core\Page\AssetRenderer
Definition: AssetRenderer.php:31
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\testBeforeRenderingEvent
‪testBeforeRenderingEvent(string $renderMethodName, bool $isInline, bool $priority, string $eventClassName)
Definition: AssetRendererTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\$assetRenderer
‪AssetRenderer $assetRenderer
Definition: AssetRendererTest.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest
Definition: AssetRendererTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Page\AssetRendererTest\testInlineJavaScript
‪testInlineJavaScript(array $sources, array $expectedResult, array $expectedMarkup)
Definition: AssetRendererTest.php:88