2018-09-10 14:40:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
|
|
|
|
2021-08-10 11:19:58 +02:00
|
|
|
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
|
|
|
{
|
2021-08-10 11:19:58 +02:00
|
|
|
/** @var UrlGenerator&MockObject $urlGenerator */
|
2018-09-10 14:40:33 +02:00
|
|
|
$urlGenerator = $this->createMock(UrlGenerator::class);
|
2021-08-10 11:19:58 +02:00
|
|
|
/** @var AssetsProvider&MockObject $assets */
|
|
|
|
$assets = $this->createMock(AssetsProvider::class);
|
2018-09-10 14:40:33 +02:00
|
|
|
|
2021-08-10 11:19:58 +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
|
|
|
|
{
|
2021-08-10 11:19:58 +02:00
|
|
|
/** @var UrlGenerator&MockObject $urlGenerator */
|
2018-09-10 14:40:33 +02:00
|
|
|
$urlGenerator = $this->createMock(UrlGenerator::class);
|
2021-08-10 11:19:58 +02:00
|
|
|
/** @var AssetsProvider&MockObject $assets */
|
|
|
|
$assets = $this->createMock(AssetsProvider::class);
|
2018-09-10 14:40:33 +02:00
|
|
|
|
2021-08-10 11:19:58 +02:00
|
|
|
$urlGenerator->expects($this->exactly(4))
|
2018-09-10 14:40:33 +02:00
|
|
|
->method('to')
|
2021-08-10 11:19:58 +02:00
|
|
|
->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
|
|
|
|
2021-08-10 11:19:58 +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);
|
|
|
|
|
2021-08-10 11:19:58 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|