Skip to content
Snippets Groups Projects
Commit ee6f2738 authored by Simon Praetorius's avatar Simon Praetorius
Browse files

[TASK] Refactor AssetCollector tests

In preparation for changes on `AssetRenderer`, most of the
unit tests are converted to functional tests. The test cases
are preserved, but duplicated to cover both the new
functional and the old unit tests.

Resolves: #104854
Releases: main
Change-Id: I3ab5bad81d7554061ca4d2ef61946e8282dc8f85
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/85921


Reviewed-by: default avatarSimon Praetorius <simon@praetorius.me>
Tested-by: default avatarcore-ci <typo3@b13.com>
Reviewed-by: default avatarBenni Mack <benni@typo3.org>
Tested-by: default avatarBenni Mack <benni@typo3.org>
Reviewed-by: default avatarSimon Schaufelberger <simonschaufi+typo3@gmail.com>
Tested-by: default avatarSimon Praetorius <simon@praetorius.me>
parent 9d555aaa
Branches
Tags
No related merge requests found
This diff is collapsed.
......@@ -17,82 +17,36 @@ declare(strict_types=1);
namespace TYPO3\CMS\Core\Tests\Unit\Page;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Page\AssetRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Page\Event\BeforeJavaScriptsRenderingEvent;
use TYPO3\CMS\Core\Page\Event\BeforeStylesheetsRenderingEvent;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
final class AssetRendererTest extends UnitTestCase
{
protected bool $resetSingletonInstances = true;
protected AssetRenderer $subject;
protected MockObject&EventDispatcherInterface $eventDispatcher;
public function setUp(): void
{
parent::setUp();
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->subject = new AssetRenderer(null, $eventDispatcher);
$this->eventDispatcher = $eventDispatcher;
}
#[DataProviderExternal(AssetDataProvider::class, 'filesDataProvider')]
#[Test]
public function styleSheets(array $files, array $expectedResult, array $expectedMarkup): void
{
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
foreach ($files as $file) {
[$identifier, $source, $attributes, $options] = $file;
$assetCollector->addStyleSheet($identifier, $source, $attributes, $options);
}
self::assertSame($expectedMarkup['css_no_prio'], $this->subject->renderStyleSheets());
self::assertSame($expectedMarkup['css_prio'], $this->subject->renderStyleSheets(true));
}
#[DataProviderExternal(AssetDataProvider::class, 'filesDataProvider')]
#[Test]
public function javaScript(array $files, array $expectedResult, array $expectedMarkup): void
/**
* cross-product of all combinations of AssetRenderer::render*() methods and priorities
* @return array[] [render method name, isInline, isPriority, event class]
*/
public static function renderMethodsAndEventsDataProvider(): array
{
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
foreach ($files as $file) {
[$identifier, $source, $attributes, $options] = $file;
$assetCollector->addJavaScript($identifier, $source, $attributes, $options);
}
self::assertSame($expectedMarkup['js_no_prio'], $this->subject->renderJavaScript());
self::assertSame($expectedMarkup['js_prio'], $this->subject->renderJavaScript(true));
return [
['renderInlineJavaScript', true, true, BeforeJavaScriptsRenderingEvent::class],
['renderInlineJavaScript', true, false, BeforeJavaScriptsRenderingEvent::class],
['renderJavaScript', false, true, BeforeJavaScriptsRenderingEvent::class],
['renderJavaScript', false, false, BeforeJavaScriptsRenderingEvent::class],
['renderInlineStylesheets', true, true, BeforeStylesheetsRenderingEvent::class],
['renderInlineStylesheets', true, false, BeforeStylesheetsRenderingEvent::class],
['renderStylesheets', false, true, BeforeStylesheetsRenderingEvent::class],
['renderStylesheets', false, false, BeforeStylesheetsRenderingEvent::class],
];
}
#[DataProviderExternal(AssetDataProvider::class, 'inlineDataProvider')]
#[Test]
public function inlineJavaScript(array $sources, array $expectedResult, array $expectedMarkup): void
{
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
foreach ($sources as $source) {
[$identifier, $source, $attributes, $options] = $source;
$assetCollector->addInlineJavaScript($identifier, $source, $attributes, $options);
}
self::assertSame($expectedMarkup['js_no_prio'], $this->subject->renderInlineJavaScript());
self::assertSame($expectedMarkup['js_prio'], $this->subject->renderInlineJavaScript(true));
}
#[DataProviderExternal(AssetDataProvider::class, 'inlineDataProvider')]
#[Test]
public function inlineStyleSheets(array $sources, array $expectedResult, array $expectedMarkup): void
{
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
foreach ($sources as $source) {
[$identifier, $source, $attributes, $options] = $source;
$assetCollector->addInlineStyleSheet($identifier, $source, $attributes, $options);
}
self::assertSame($expectedMarkup['css_no_prio'], $this->subject->renderInlineStyleSheets());
self::assertSame($expectedMarkup['css_prio'], $this->subject->renderInlineStyleSheets(true));
}
#[DataProviderExternal(AssetDataProvider::class, 'renderMethodsAndEventsDataProvider')]
#[DataProvider('renderMethodsAndEventsDataProvider')]
#[Test]
public function beforeRenderingEvent(
string $renderMethodName,
......@@ -100,18 +54,22 @@ final class AssetRendererTest extends UnitTestCase
bool $priority,
string $eventClassName
): void {
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->method('dispatch')->willReturnArgument(0);
$assetCollector = new AssetCollector();
$assetRenderer = new AssetRenderer($assetCollector, $eventDispatcher);
$event = new $eventClassName(
$assetCollector,
$isInline,
$priority
);
$this->eventDispatcher
$eventDispatcher
->expects(self::once())
->method('dispatch')
->with($event);
$this->subject->$renderMethodName($priority);
$assetRenderer->$renderMethodName($priority);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment