|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Symfony package. |
| 7 | + * |
| 8 | + * (c) Fabien Potencier <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Symfony\UX\LiveComponent\Tests\Integration\DependencyInjection\Compiler; |
| 15 | + |
| 16 | +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | +use Symfony\Component\DependencyInjection\Definition; |
| 19 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 20 | +use Symfony\UX\LiveComponent\DependencyInjection\Compiler\LiveComponentTagPass; |
| 21 | + |
| 22 | +final class LiveComponentTagPassTest extends AbstractCompilerPassTestCase |
| 23 | +{ |
| 24 | + public function testAddingTwigComponentTagToServicesTaggedWithLiveComponentTag(): void |
| 25 | + { |
| 26 | + $liveComponent = new Definition(); |
| 27 | + $liveComponent->addTag('live.component', ['key' => 'foo', 'template' => 'bar']); |
| 28 | + |
| 29 | + $this->setDefinition('my_live_component', $liveComponent); |
| 30 | + |
| 31 | + $this->compile(); |
| 32 | + |
| 33 | + $this->assertContainerBuilderHasServiceDefinitionWithTag( |
| 34 | + 'my_live_component', |
| 35 | + 'twig.component', |
| 36 | + [ |
| 37 | + 'key' => 'foo', |
| 38 | + 'template' => 'bar', |
| 39 | + 'expose_public_props' => true, |
| 40 | + 'attributes_var' => 'attributes', |
| 41 | + 'default_action' => null, |
| 42 | + 'live' => true, |
| 43 | + 'csrf' => true, |
| 44 | + 'route' => 'ux_live_component', |
| 45 | + 'method' => 'post', |
| 46 | + 'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_PATH, |
| 47 | + ] |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testOverridingTagAttributesWithLiveComponentTag(): void |
| 52 | + { |
| 53 | + $liveComponent = new Definition(); |
| 54 | + $liveComponent->addTag('live.component', [ |
| 55 | + 'key' => 'foo', |
| 56 | + 'template' => 'bar', |
| 57 | + 'expose_public_props' => false, |
| 58 | + 'attributes_var' => 'custom_attributes', |
| 59 | + 'default_action' => 'customAction', |
| 60 | + 'csrf' => false, |
| 61 | + 'route' => 'custom_route', |
| 62 | + 'method' => 'get', |
| 63 | + 'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_URL, |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->setDefinition('my_live_component', $liveComponent); |
| 67 | + |
| 68 | + $this->compile(); |
| 69 | + |
| 70 | + $this->assertContainerBuilderHasServiceDefinitionWithTag( |
| 71 | + 'my_live_component', |
| 72 | + 'twig.component', |
| 73 | + [ |
| 74 | + 'key' => 'foo', |
| 75 | + 'template' => 'bar', |
| 76 | + 'expose_public_props' => false, |
| 77 | + 'attributes_var' => 'custom_attributes', |
| 78 | + 'default_action' => 'customAction', |
| 79 | + 'live' => true, |
| 80 | + 'csrf' => false, |
| 81 | + 'route' => 'custom_route', |
| 82 | + 'method' => 'get', |
| 83 | + 'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_URL, |
| 84 | + ] |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function testThrowingExceptionWhenKeyIsNotPresentOnLiveComponentTag(): void |
| 89 | + { |
| 90 | + $liveComponent = new Definition(); |
| 91 | + $liveComponent->addTag('live.component', ['template' => 'bar']); |
| 92 | + |
| 93 | + $this->setDefinition('my_live_component', $liveComponent); |
| 94 | + |
| 95 | + $this->expectException(\InvalidArgumentException::class); |
| 96 | + $this->expectExceptionMessage('The "key" attribute is required for the "live.component" tag'); |
| 97 | + |
| 98 | + $this->compile(); |
| 99 | + } |
| 100 | + |
| 101 | + public function testThrowingExceptionWhenTemplateIsNotPresentOnLiveComponentTag(): void |
| 102 | + { |
| 103 | + $liveComponent = new Definition(); |
| 104 | + $liveComponent->addTag('live.component', ['key' => 'foo']); |
| 105 | + |
| 106 | + $this->setDefinition('my_live_component', $liveComponent); |
| 107 | + |
| 108 | + $this->expectException(\InvalidArgumentException::class); |
| 109 | + $this->expectExceptionMessage('The "template" attribute is required for the "live.component" tag'); |
| 110 | + |
| 111 | + $this->compile(); |
| 112 | + } |
| 113 | + |
| 114 | + protected function registerCompilerPass(ContainerBuilder $container): void |
| 115 | + { |
| 116 | + $container->addCompilerPass(new LiveComponentTagPass()); |
| 117 | + } |
| 118 | +} |
0 commit comments