8000 Add integration test · symfony/symfony@4fa5467 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fa5467

Browse files
committed
Add integration test
1 parent ffda0e8 commit 4fa5467

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function autoconfigureFromAttribute(ChildDefinition $definition, A
4444
public function process(ContainerBuilder $container): void
4545
{
4646
foreach ($container->findTaggedServiceIds(self::TAG, true) as $id => $tags) {
47-
$container->register('twig.extension.'.$id, AttributeExtension::class)
47+
$container->register('.twig.extension.'.$id, AttributeExtension::class)
4848
->setArguments([$container->getDefinition($id)->getClass()])
4949
->addTag('twig.extension');
5050
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 AttributeExtensionKernel('test', true);
33+
$kernel->boot();
34+
35+
/** @var Environment $twig */
36+
$twig = $kernel->getContainer()->get('twig_test');
37+
38+
self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(StaticExtensionWithAttributes::class));
39+
self::assertInstanceOf(AttributeExtension::class, $twig->getExtension(RuntimeExtensionWithAttributes::class));
40+
self::assertInstanceOf(RuntimeExtensionWithAttributes::class, $twig->getRuntime(RuntimeExtensionWithAttributes::class));
41+
42+
self::expectException(RuntimeError::class);
43+
$twig->getRuntime(StaticExtensionWithAttributes::class);
44+
}
45+
46+
protected function setUp(): void
47+
{
48+
$this->deleteTempDir();
49+
}
50+
51+
protected function tearDown(): void
52+
{
53+
$this->deleteTempDir();
54+
}
55+
56+
private function deleteTempDir()
57+
{
58+
if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel')) {
59+
return;
60+
}
61+
62+
$fs = new Filesystem();
63+
$fs->remove($dir);
64+
}
65+
}
66+
67+
class AttributeExtensionKernel extends Kernel
68+
{
69+
public function registerBundles(): iterable
70+
{
71+
return [new FrameworkBundle(), new TwigBundle()];
72+
}
73+
74+
public function registerContainerConfiguration(LoaderInterface $loader): void
75+
{
76+
$loader->load(static function (ContainerBuilder $container) {
77+
$container->register(StaticExtensionWithAttributes::class, StaticExtensionWithAttributes::class)
78+
->setAutoconfigured(true);
79+
$container->register(RuntimeExtensionWithAttributes::class, RuntimeExtensionWithAttributes::class)
80+
->setArguments(['prefix_'])
81+
->setAutoconfigured(true);
82+
83+
$container->setAlias('twig_test', 'twig')->setPublic(true);
84+
});
85+
}
86+
87+
public function getCacheDir(): string
88+
{
89+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtensionKernel/cache/'.$this->environment;
90+
}
91+
92+
public function getLogDir(): string
93+
{
94+
return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtensionKernel/logs';
95+
}
96+
}
97+
98+
class StaticExtensionWithAttributes
99+
{
100+
#[AsTwigFilter]
101+
public static function fooFilter(string $value): string
102+
{
103+
return $value;
104+
}
105+
106+
#[AsTwigFunction]
107+
public static function fooFunction(string $value): string
108+
{
109+
return $value;
110+
}
111+
112+
#[AsTwigTest]
113+
public static function fooTest(bool $value): bool
114+
{
115+
return $value;
116+
}
117+
}
118+
119+
class RuntimeExtensionWithAttributes
120+
{
121+
public function __construct(private bool $prefix)
122+
{
123+
}
124+
125+
#[AsTwigFilter]
126+
#[AsTwigFunction]
127+
public function prefix(string $value): string
128+
{
129+
return $this->prefix.$value;
130+
}
131+
132+
}

0 commit comments

Comments
 (0)
0