Fixed assets rendering
parent
222c9fed7d
commit
4bf3a68f43
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Renderer\Twig\Extensions;
|
||||
|
||||
use Engelsystem\Http\UrlGenerator;
|
||||
use Twig_Extension as TwigExtension;
|
||||
use Twig_Function as TwigFunction;
|
||||
|
||||
class Assets extends TwigExtension
|
||||
{
|
||||
/** @var UrlGenerator */
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* @param UrlGenerator $urlGenerator
|
||||
*/
|
||||
public function __construct(UrlGenerator $urlGenerator)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFunction[]
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('asset', [$this, 'getAsset']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return UrlGenerator|string
|
||||
*/
|
||||
public function getAsset($path)
|
||||
{
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
return $this->urlGenerator->to('/' . $path);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
||||
|
||||
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
|
||||
*/
|
||||
public function testGetGlobals()
|
||||
{
|
||||
/** @var UrlGenerator|MockObject $urlGenerator */
|
||||
$urlGenerator = $this->createMock(UrlGenerator::class);
|
||||
|
||||
$extension = new Assets($urlGenerator);
|
||||
$functions = $extension->getFunctions();
|
||||
|
||||
$this->assertExtensionExists('asset', [$extension, 'getAsset'], $functions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Assets::getAsset
|
||||
*/
|
||||
public function testGetAsset()
|
||||
{
|
||||
/** @var UrlGenerator|MockObject $urlGenerator */
|
||||
$urlGenerator = $this->createMock(UrlGenerator::class);
|
||||
|
||||
$urlGenerator->expects($this->exactly(2))
|
||||
->method('to')
|
||||
->with('/assets/foo.css')
|
||||
->willReturn('https://foo.bar/project/assets/foo.css');
|
||||
|
||||
$extension = new Assets($urlGenerator);
|
||||
|
||||
$return = $extension->getAsset('assets/foo.css');
|
||||
$this->assertEquals('https://foo.bar/project/assets/foo.css', $return);
|
||||
|
||||
$return = $extension->getAsset('/assets/foo.css');
|
||||
$this->assertEquals('https://foo.bar/project/assets/foo.css', $return);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue