|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\TwigBundle\Tests\Functional; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
| 15 | +use Symfony\Bundle\TwigBundle\Tests\TestCase; |
| 16 | +use Symfony\Bundle\TwigBundle\TwigBundle; |
| 17 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\Filesystem\Filesystem; |
| 20 | +use Symfony\Component\HttpKernel\Kernel; |
| 21 | +use Twig\Attribute\AsTwigFilter; |
| 22 | +use Twig\Attribute\AsTwigFunction; |
| 23 | +use Twig\Attribute\AsTwigTest; |
| 24 | +use Twig\Environment; |
| 25 | +use Twig\Error\RuntimeError; |
| 26 | +use Twig\Extension\AttributeExtension; |
| 27 | + |
| 28 | +class AttributeExtensionTest extends TestCase |
| 29 | +{ |
| 30 | + public function testExtensionWithAttributes() |
| 31 | + { |
| 32 | + $kernel = new class('test', true) extends Kernel |
| 33 | + { |
| 34 | + public function registerBundles(): iterable |
| 35 | + { |
| 36 | + return [new FrameworkBundle(), new TwigBundle()]; |
| 37 | + } |
| 38 | + |
| 39 | + public function registerContainerConfiguration(LoaderInterface $loader): void |
| 40 | + { |
| 41 | + $loader->load(static function (ContainerBuilder $container) { |
| 42 | + $container->register(StaticExtensionWithAttributes::class, StaticExtensionWithAttributes::class) |
| 43 | + ->setAutoconfigured(true); |
| 44 | + $container->register(RuntimeExtensionWithAttributes::class, RuntimeExtensionWithAttributes::class) |
| 45 | + ->setArguments(['prefix_']) |
| 46 | + ->setAutoconfigured(true); |
| 47 | + |
| 48 | + $container->setAlias('twig_test', 'twig')->setPublic(true); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + public function getProjectDir(): string |
| 53 | + { |
| 54 | + return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension'; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + $kernel->boot(); |
| 59 | + |
| 60 | + /** @var Environment $twig */ |
| 61 | + $twig = $kernel->getContainer()->get('twig_test'); |
| 62 | + |
| 63 | + self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(StaticExtensionWithAttributes::class)); |
| 64 | + self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(RuntimeExtensionWithAttributes::class)); |
| 65 | + self::assertInstanceOf(RuntimeExtensionWithAttributes::class, $twig->getRuntime(RuntimeExtensionWithAttributes::class)); |
| 66 | + |
| 67 | + self::expectException(RuntimeError::class); |
| 68 | + $twig->getRuntime(StaticExtensionWithAttributes::class); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @before |
| 73 | + * @after |
| 74 | + */ |
| 75 | + protected function deleteTempDir() |
| 76 | + { |
| 77 | + if (file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension')) { |
| 78 | + (new Filesystem())->remove($dir); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class StaticExtensionWithAttributes |
| 84 | +{ |
| 85 | + #[AsTwigFilter] |
| 86 | + public static function fooFilter(string $value): string |
| 87 | + { |
| 88 | + return $value; |
| 89 | + } |
| 90 | + |
| 91 | + #[AsTwigFunction] |
| 92 | + public static function fooFunction(string $value): string |
| 93 | + { |
| 94 | + return $value; |
| 95 | + } |
| 96 | + |
| 97 | + #[AsTwigTest] |
| 98 | + public static function fooTest(bool $value): bool |
| 99 | + { |
| 100 | + return $value; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +class RuntimeExtensionWithAttributes |
| 105 | +{ |
| 106 | + public function __construct(private bool $prefix) |
| 107 | + { |
| 108 | + } |
| 109 | + |
| 110 | + #[AsTwigFilter] |
| 111 | + #[AsTwigFunction] |
| 112 | + public function prefix(string $value): string |
| 113 | + { |
| 114 | + return $this->prefix.$value; |
| 115 | + } |
| 116 | +} |
0 commit comments