2018-08-26 02:54:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Renderer;
|
|
|
|
|
2018-09-02 02:09:56 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2020-03-01 00:18:33 +01:00
|
|
|
use Engelsystem\Renderer\Twig\Extensions\Develop;
|
2018-08-26 02:54:52 +02:00
|
|
|
use Engelsystem\Renderer\TwigEngine;
|
|
|
|
use Engelsystem\Renderer\TwigLoader;
|
|
|
|
use Engelsystem\Renderer\TwigServiceProvider;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-26 12:23:47 +02:00
|
|
|
use ReflectionClass as Reflection;
|
2019-11-06 13:16:00 +01:00
|
|
|
use ReflectionException;
|
2018-08-26 12:23:47 +02:00
|
|
|
use stdClass;
|
2020-03-01 00:18:33 +01:00
|
|
|
use Symfony\Component\VarDumper\VarDumper;
|
2019-03-16 22:02:36 +01:00
|
|
|
use Twig\Environment as Twig;
|
2019-11-29 19:38:28 +01:00
|
|
|
use Twig\Extension\AbstractExtension;
|
2019-03-16 22:02:36 +01:00
|
|
|
use Twig\Extension\CoreExtension as TwigCore;
|
|
|
|
use Twig\Extension\ExtensionInterface as ExtensionInterface;
|
|
|
|
use Twig\Loader\LoaderInterface as TwigLoaderInterface;
|
2018-08-26 02:54:52 +02:00
|
|
|
|
|
|
|
class TwigServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Renderer\TwigServiceProvider::register
|
2018-08-26 12:23:47 +02:00
|
|
|
* @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigExtensions
|
2018-08-26 02:54:52 +02:00
|
|
|
*/
|
|
|
|
public function testRegister(): void
|
2018-08-26 12:23:47 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApp(['make', 'instance', 'tag']);
|
|
|
|
$class = $this->createMock(stdClass::class);
|
|
|
|
|
|
|
|
$className = 'Foo\Bar\Class';
|
|
|
|
$classAlias = 'twig.extension.foo';
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('make')
|
|
|
|
->with('Foo\Bar\Class')
|
|
|
|
->willReturn($class);
|
|
|
|
|
|
|
|
$app->expects($this->exactly(2))
|
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
[$className, $class],
|
|
|
|
[$classAlias, $class]
|
|
|
|
);
|
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('tag')
|
|
|
|
->with($classAlias, ['twig.extension']);
|
|
|
|
|
|
|
|
/** @var TwigServiceProvider|MockObject $serviceProvider */
|
|
|
|
$serviceProvider = $this->getMockBuilder(TwigServiceProvider::class)
|
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['registerTwigEngine'])
|
2018-08-26 12:23:47 +02:00
|
|
|
->getMock();
|
|
|
|
$serviceProvider->expects($this->once())
|
|
|
|
->method('registerTwigEngine');
|
|
|
|
$this->setExtensionsTo($serviceProvider, ['foo' => 'Foo\Bar\Class']);
|
|
|
|
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Renderer\TwigServiceProvider::boot
|
|
|
|
*/
|
|
|
|
public function testBoot(): void
|
|
|
|
{
|
|
|
|
/** @var Twig|MockObject $twig */
|
|
|
|
$twig = $this->createMock(Twig::class);
|
|
|
|
/** @var ExtensionInterface|MockObject $firsExtension */
|
|
|
|
$firsExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
|
|
|
|
/** @var ExtensionInterface|MockObject $secondExtension */
|
|
|
|
$secondExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
|
2020-03-01 00:18:33 +01:00
|
|
|
/** @var Develop|MockObject $devExtension */
|
|
|
|
$devExtension = $this->createMock(Develop::class);
|
|
|
|
/** @var VarDumper|MockObject $dumper */
|
|
|
|
$dumper = $this->createMock(VarDumper::class);
|
2018-08-26 12:23:47 +02:00
|
|
|
|
2020-03-01 00:18:33 +01:00
|
|
|
$app = $this->getApp(['get', 'tagged', 'make']);
|
2018-08-26 12:23:47 +02:00
|
|
|
|
2020-03-01 00:18:33 +01:00
|
|
|
$app->expects($this->exactly(2))
|
2018-08-26 12:23:47 +02:00
|
|
|
->method('get')
|
2020-03-01 00:18:33 +01:00
|
|
|
->withConsecutive(['twig.environment'], ['twig.extension.develop'])
|
|
|
|
->willReturnOnConsecutiveCalls($twig, $devExtension);
|
2018-08-26 12:23:47 +02:00
|
|
|
$app->expects($this->once())
|
|
|
|
->method('tagged')
|
|
|
|
->with('twig.extension')
|
|
|
|
->willReturn([$firsExtension, $secondExtension]);
|
2020-03-01 00:18:33 +01:00
|
|
|
$app->expects($this->once())
|
|
|
|
->method('make')
|
|
|
|
->with(VarDumper::class)
|
|
|
|
->willReturn($dumper);
|
2018-08-26 12:23:47 +02:00
|
|
|
|
|
|
|
$twig->expects($this->exactly(2))
|
|
|
|
->method('addExtension')
|
2019-09-17 22:16:53 +02:00
|
|
|
->withConsecutive([$firsExtension], [$secondExtension]);
|
2018-08-26 12:23:47 +02:00
|
|
|
|
2020-03-01 00:18:33 +01:00
|
|
|
$devExtension->expects($this->once())
|
|
|
|
->method('setDumper')
|
|
|
|
->with($dumper);
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
$serviceProvider = new TwigServiceProvider($app);
|
|
|
|
$serviceProvider->boot();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigEngine
|
|
|
|
*/
|
2018-10-28 12:59:49 +01:00
|
|
|
public function testRegisterTwigEngine(): void
|
2018-08-26 02:54:52 +02:00
|
|
|
{
|
|
|
|
/** @var TwigEngine|MockObject $htmlEngine */
|
|
|
|
$twigEngine = $this->createMock(TwigEngine::class);
|
|
|
|
/** @var TwigLoader|MockObject $twigLoader */
|
|
|
|
$twigLoader = $this->createMock(TwigLoader::class);
|
|
|
|
/** @var Twig|MockObject $twig */
|
|
|
|
$twig = $this->createMock(Twig::class);
|
2018-09-02 02:09:56 +02:00
|
|
|
/** @var Config|MockObject $config */
|
|
|
|
$config = $this->createMock(Config::class);
|
|
|
|
/** @var TwigCore|MockObject $twigCore */
|
2019-11-29 19:38:28 +01:00
|
|
|
$twigCore = $this->getMockForAbstractClass(
|
|
|
|
AbstractExtension::class,
|
|
|
|
[],
|
|
|
|
'',
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
['setTimezone']
|
|
|
|
);
|
2018-08-26 02:54:52 +02:00
|
|
|
|
|
|
|
$app = $this->getApp(['make', 'instance', 'tag', 'get']);
|
|
|
|
|
|
|
|
$viewsPath = __DIR__ . '/Stub';
|
|
|
|
|
|
|
|
$app->expects($this->exactly(3))
|
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
[TwigLoader::class, ['paths' => $viewsPath]],
|
2018-10-28 12:59:49 +01:00
|
|
|
[Twig::class, ['options' => ['cache' => false, 'auto_reload' => true, 'strict_variables' => true]]],
|
2018-08-26 02:54:52 +02:00
|
|
|
[TwigEngine::class]
|
|
|
|
)->willReturnOnConsecutiveCalls(
|
|
|
|
$twigLoader,
|
|
|
|
$twig,
|
|
|
|
$twigEngine
|
|
|
|
);
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
$app->expects($this->exactly(6))
|
2018-08-26 02:54:52 +02:00
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
[TwigLoader::class, $twigLoader],
|
|
|
|
[TwigLoaderInterface::class, $twigLoader],
|
2018-08-26 12:23:47 +02:00
|
|
|
['twig.loader', $twigLoader],
|
2018-08-26 02:54:52 +02:00
|
|
|
[Twig::class, $twig],
|
2018-08-26 12:23:47 +02:00
|
|
|
['twig.environment', $twig],
|
2018-08-26 02:54:52 +02:00
|
|
|
['renderer.twigEngine', $twigEngine]
|
|
|
|
);
|
|
|
|
|
2018-10-28 12:59:49 +01:00
|
|
|
$app->expects($this->exactly(3))
|
2018-08-26 02:54:52 +02:00
|
|
|
->method('get')
|
2018-10-28 12:59:49 +01:00
|
|
|
->withConsecutive(['path.views'], ['config'], ['path.cache.views'])
|
|
|
|
->willReturnOnConsecutiveCalls($viewsPath, $config, 'cache/views');
|
2018-08-26 02:54:52 +02:00
|
|
|
|
|
|
|
$this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]);
|
|
|
|
|
2018-10-28 12:59:49 +01:00
|
|
|
$config->expects($this->exactly(3))
|
2018-09-02 02:09:56 +02:00
|
|
|
->method('get')
|
2018-10-28 12:59:49 +01:00
|
|
|
->withConsecutive(['environment'], ['environment'], ['timezone'])
|
|
|
|
->willReturnOnConsecutiveCalls('development', 'development', 'The/World');
|
2018-09-02 02:09:56 +02:00
|
|
|
|
|
|
|
$twig->expects($this->once())
|
|
|
|
->method('getExtension')
|
|
|
|
->with(TwigCore::class)
|
|
|
|
->willReturn($twigCore);
|
|
|
|
|
|
|
|
$twigCore->expects($this->once())
|
|
|
|
->method('setTimezone')
|
|
|
|
->with('The/World');
|
|
|
|
|
2018-08-26 02:54:52 +02:00
|
|
|
$serviceProvider = new TwigServiceProvider($app);
|
2018-08-26 12:23:47 +02:00
|
|
|
$this->setExtensionsTo($serviceProvider, []);
|
|
|
|
|
2018-08-26 02:54:52 +02:00
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2018-08-26 12:23:47 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-06 13:16:00 +01:00
|
|
|
* @throws ReflectionException
|
2018-08-26 12:23:47 +02:00
|
|
|
*/
|
|
|
|
protected function setExtensionsTo(TwigServiceProvider $serviceProvider, array $extensions): void
|
|
|
|
{
|
|
|
|
$reflection = new Reflection(get_class($serviceProvider));
|
|
|
|
|
|
|
|
$property = $reflection->getProperty('extensions');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
$property->setValue($serviceProvider, $extensions);
|
|
|
|
}
|
2018-08-26 02:54:52 +02:00
|
|
|
}
|