engelsystem/tests/Unit/Renderer/Twig/Extensions/AssetsTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.4 KiB
PHP
Raw Normal View History

2018-09-10 14:40:33 +02:00
<?php
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use Engelsystem\Helpers\Assets as AssetsProvider;
2018-09-10 14:40:33 +02:00
use Engelsystem\Http\UrlGenerator;
use Engelsystem\Renderer\Twig\Extensions\Assets;
use PHPUnit\Framework\MockObject\MockObject;
class AssetsTest extends ExtensionTest
{
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Assets::__construct
* @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getFunctions
*/
2021-08-17 15:10:31 +02:00
public function testGetFunctions(): void
2018-09-10 14:40:33 +02:00
{
/** @var UrlGenerator&MockObject $urlGenerator */
2018-09-10 14:40:33 +02:00
$urlGenerator = $this->createMock(UrlGenerator::class);
/** @var AssetsProvider&MockObject $assets */
$assets = $this->createMock(AssetsProvider::class);
2018-09-10 14:40:33 +02:00
$extension = new Assets($assets, $urlGenerator);
2018-09-10 14:40:33 +02:00
$functions = $extension->getFunctions();
$this->assertExtensionExists('asset', [$extension, 'getAsset'], $functions);
}
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getAsset
*/
public function testGetAsset(): void
{
/** @var UrlGenerator&MockObject $urlGenerator */
2018-09-10 14:40:33 +02:00
$urlGenerator = $this->createMock(UrlGenerator::class);
/** @var AssetsProvider&MockObject $assets */
$assets = $this->createMock(AssetsProvider::class);
2018-09-10 14:40:33 +02:00
$urlGenerator->expects($this->exactly(4))
2018-09-10 14:40:33 +02:00
->method('to')
->withConsecutive(['/test.png'], ['/assets/foo.css'], ['/assets/bar.css'], ['/assets/lorem-hashed.js'])
->willReturnCallback(function ($path) {
return 'https://foo.bar/project' . $path;
});
2018-09-10 14:40:33 +02:00
$assets->expects($this->exactly(3))
->method('getAssetPath')
->withConsecutive(['foo.css'], ['bar.css'], ['lorem.js'])
->willReturnOnConsecutiveCalls('foo.css', 'bar.css', 'lorem-hashed.js');
$extension = new Assets($assets, $urlGenerator);
$return = $extension->getAsset('test.png');
$this->assertEquals('https://foo.bar/project/test.png', $return);
2018-09-10 14:40:33 +02:00
$return = $extension->getAsset('assets/foo.css');
$this->assertEquals('https://foo.bar/project/assets/foo.css', $return);
$return = $extension->getAsset('/assets/bar.css');
$this->assertEquals('https://foo.bar/project/assets/bar.css', $return);
$return = $extension->getAsset('assets/lorem.js');
$this->assertEquals('https://foo.bar/project/assets/lorem-hashed.js', $return);
2018-09-10 14:40:33 +02:00
}
}