2018-08-26 12:23:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2018-09-02 02:09:56 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-26 12:23:47 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Twig_Function as TwigFunction;
|
2018-09-02 02:09:56 +02:00
|
|
|
use Twig_Node as TwigNode;
|
2018-08-26 12:23:47 +02:00
|
|
|
|
|
|
|
abstract class ExtensionTest extends TestCase
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
2018-08-29 15:29:48 +02:00
|
|
|
/**
|
|
|
|
* Assert that a twig filter was registered
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param callable $callback
|
|
|
|
* @param TwigFunction[] $functions
|
|
|
|
*/
|
|
|
|
protected function assertFilterExists($name, $callback, $functions)
|
|
|
|
{
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
if ($function->getName() != $name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($callback, $function->getCallable());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail(sprintf('Filter %s not found', $name));
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
/**
|
|
|
|
* Assert that a twig function was registered
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param callable $callback
|
|
|
|
* @param TwigFunction[] $functions
|
|
|
|
*/
|
2018-09-02 02:09:56 +02:00
|
|
|
protected function assertExtensionExists($name, $callback, $functions, $options = [])
|
2018-08-26 12:23:47 +02:00
|
|
|
{
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
if ($function->getName() != $name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($callback, $function->getCallable());
|
2018-09-02 02:09:56 +02:00
|
|
|
|
|
|
|
if (isset($options['is_save'])) {
|
|
|
|
/** @var TwigNode|MockObject $twigNode */
|
|
|
|
$twigNode = $this->createMock(TwigNode::class);
|
|
|
|
|
|
|
|
$this->assertArraySubset($options['is_save'], $function->getSafe($twigNode));
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail(sprintf('Function %s not found', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a global exists
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
* @param mixed[] $globals
|
|
|
|
*/
|
|
|
|
protected function assertGlobalsExists($name, $value, $globals)
|
|
|
|
{
|
|
|
|
if (isset($globals[$name])) {
|
|
|
|
$this->assertArraySubset([$name => $value], $globals);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail(sprintf('Global %s not found', $name));
|
|
|
|
}
|
2018-08-29 15:29:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a token parser was set
|
|
|
|
*
|
|
|
|
* @param $tokenParser
|
|
|
|
* @param $tokenParsers
|
|
|
|
*/
|
|
|
|
protected function assertTokenParserExists($tokenParser, $tokenParsers)
|
|
|
|
{
|
|
|
|
$this->assertArraySubset(
|
|
|
|
[$tokenParser],
|
|
|
|
$tokenParsers,
|
|
|
|
sprintf('Token parser %s not found', get_class($tokenParser))
|
|
|
|
);
|
|
|
|
}
|
2018-08-26 12:23:47 +02:00
|
|
|
}
|