2018-08-26 12:23:47 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-26 12:23:47 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2021-06-29 00:27:57 +02:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2019-11-06 13:16:00 +01:00
|
|
|
use Exception;
|
2018-09-02 02:09:56 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-03-16 22:02:36 +01:00
|
|
|
use Twig\Node\Node as TwigNode;
|
2020-04-19 20:41:38 +02:00
|
|
|
use Twig\TwigFunction;
|
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 TwigFunction[] $functions
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function assertFilterExists(string $name, callable $callback, array $functions): void
|
2018-08-29 15:29:48 +02:00
|
|
|
{
|
|
|
|
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 TwigFunction[] $functions
|
2019-11-06 13:16:00 +01:00
|
|
|
* @throws Exception
|
2018-08-26 12:23:47 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function assertExtensionExists(
|
|
|
|
string $name,
|
2023-01-18 13:02:11 +01:00
|
|
|
mixed $callback,
|
2022-12-14 19:15:20 +01:00
|
|
|
array $functions,
|
|
|
|
array $options = []
|
|
|
|
): void {
|
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 mixed[] $globals
|
2019-11-06 13:16:00 +01:00
|
|
|
* @throws Exception
|
2018-08-26 12:23:47 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function assertGlobalsExists(string $name, mixed $value, array $globals): void
|
2018-08-26 12:23:47 +02:00
|
|
|
{
|
2022-06-06 12:32:07 +02:00
|
|
|
if (array_key_exists($name, $globals)) {
|
2018-08-26 12:23:47 +02:00
|
|
|
$this->assertArraySubset([$name => $value], $globals);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail(sprintf('Global %s not found', $name));
|
|
|
|
}
|
|
|
|
}
|