diff --git a/src/StimulusBundle/tests/Dto/StimulusActionAttributeTest.php b/src/StimulusBundle/tests/Dto/StimulusActionAttributeTest.php deleted file mode 100644 index bb152f671b8..00000000000 --- a/src/StimulusBundle/tests/Dto/StimulusActionAttributeTest.php +++ /dev/null @@ -1,115 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\UX\StimulusBundle\Tests\Dto; - -use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; -use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; -use Twig\Environment; -use Twig\Loader\ArrayLoader; - -final class StimulusActionAttributeTest extends TestCase -{ - private StimulusAttributes $stimulusAttributes; - - protected function setUp(): void - { - $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); - } - - /** - * @dataProvider provideAddActionData - */ - public function testAddAction(string $controllerName, string $actionName, ?string $eventName, string $expectedAction): void - { - $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); - $attributesHtml = (string) $this->stimulusAttributes; - self::assertSame(\sprintf('data-action="%s"', $expectedAction), $attributesHtml); - } - - /** - * @return iterable - */ - public static function provideAddActionData(): iterable - { - // basic datasets - yield 'foo#bar' => [ - 'controllerName' => 'foo', - 'actionName' => 'bar', - 'eventName' => null, - 'expectedAction' => 'foo#bar', - ]; - yield 'baz->foo#bar' => [ - 'controllerName' => 'foo', - 'actionName' => 'bar', - 'eventName' => 'baz', - 'expectedAction' => 'baz->foo#bar', - ]; - - // datasets from https://github.com/hotwired/stimulus - yield 'keydown.esc@document->a#log' => [ - 'controllerName' => 'a', - 'actionName' => 'log', - 'eventName' => 'keydown.esc@document', - 'expectedAction' => 'keydown.esc@document->a#log', - ]; - yield 'keydown.enter->a#log' => [ - 'controllerName' => 'a', - 'actionName' => 'log', - 'eventName' => 'keydown.enter', - 'expectedAction' => 'keydown.enter->a#log', - ]; - yield 'keydown.shift+a->a#log' => [ - 'controllerName' => 'a', - 'actionName' => 'log', - 'eventName' => 'keydown.shift+a', - 'expectedAction' => 'keydown.shift+a->a#log', - ]; - yield 'keydown@window->c#log' => [ - 'controllerName' => 'c', - 'actionName' => 'log', - 'eventName' => 'keydown@window', - 'expectedAction' => 'keydown@window->c#log', - ]; - yield 'click->c#log:once' => [ - 'controllerName' => 'c', - 'actionName' => 'log:once', - 'eventName' => 'click', - 'expectedAction' => 'click->c#log:once', - ]; - - // extended datasets - yield 'vue:mount->foo#bar:passive' => [ - 'controllerName' => 'foo', - 'actionName' => 'bar:passive', - 'eventName' => 'vue:mount', - 'expectedAction' => 'vue:mount->foo#bar:passive', - ]; - yield 'foo--controller-1:baz->bar--controller-2#log' => [ - 'controllerName' => '@bar/controller_2', - 'actionName' => 'log', - 'eventName' => '@foo/controller_1:baz', - 'expectedAction' => 'foo--controller-1:baz->bar--controller-2#log', - ]; - yield 'foo--controller-1:baz@document->bar--controller-2#log:capture' => [ - 'controllerName' => '@bar/controller_2', - 'actionName' => 'log:capture', - 'eventName' => '@foo/controller_1:baz@document', - 'expectedAction' => 'foo--controller-1:baz@document->bar--controller-2#log:capture', - ]; - } -} diff --git a/src/StimulusBundle/tests/Dto/StimulusAttributesTest.php b/src/StimulusBundle/tests/Dto/StimulusAttributesTest.php index 17527a3491c..be252c2863f 100644 --- a/src/StimulusBundle/tests/Dto/StimulusAttributesTest.php +++ b/src/StimulusBundle/tests/Dto/StimulusAttributesTest.php @@ -148,4 +148,91 @@ public function testAddAttribute() $this->assertSame('foo="bar baz"', (string) $this->stimulusAttributes); $this->assertSame(['foo' => 'bar baz'], $this->stimulusAttributes->toArray()); } + + /** + * @dataProvider provideAddComplexActionData + */ + public function testAddComplexAction(string $controllerName, string $actionName, ?string $eventName, string $expectedAction): void + { + $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); + $attributesHtml = (string) $this->stimulusAttributes; + self::assertSame(\sprintf('data-action="%s"', $expectedAction), $attributesHtml); + } + + /** + * @return iterable + */ + public static function provideAddComplexActionData(): iterable + { + // basic datasets + yield 'foo#bar' => [ + 'controllerName' => 'foo', + 'actionName' => 'bar', + 'eventName' => null, + 'expectedAction' => 'foo#bar', + ]; + yield 'baz->foo#bar' => [ + 'controllerName' => 'foo', + 'actionName' => 'bar', + 'eventName' => 'baz', + 'expectedAction' => 'baz->foo#bar', + ]; + + // datasets from https://github.com/hotwired/stimulus + yield 'keydown.esc@document->a#log' => [ + 'controllerName' => 'a', + 'actionName' => 'log', + 'eventName' => 'keydown.esc@document', + 'expectedAction' => 'keydown.esc@document->a#log', + ]; + yield 'keydown.enter->a#log' => [ + 'controllerName' => 'a', + 'actionName' => 'log', + 'eventName' => 'keydown.enter', + 'expectedAction' => 'keydown.enter->a#log', + ]; + yield 'keydown.shift+a->a#log' => [ + 'controllerName' => 'a', + 'actionName' => 'log', + 'eventName' => 'keydown.shift+a', + 'expectedAction' => 'keydown.shift+a->a#log', + ]; + yield 'keydown@window->c#log' => [ + 'controllerName' => 'c', + 'actionName' => 'log', + 'eventName' => 'keydown@window', + 'expectedAction' => 'keydown@window->c#log', + ]; + yield 'click->c#log:once' => [ + 'controllerName' => 'c', + 'actionName' => 'log:once', + 'eventName' => 'click', + 'expectedAction' => 'click->c#log:once', + ]; + + // extended datasets + yield 'vue:mount->foo#bar:passive' => [ + 'controllerName' => 'foo', + 'actionName' => 'bar:passive', + 'eventName' => 'vue:mount', + 'expectedAction' => 'vue:mount->foo#bar:passive', + ]; + yield 'foo--controller-1:baz->bar--controller-2#log' => [ + 'controllerName' => '@bar/controller_2', + 'actionName' => 'log', + 'eventName' => '@foo/controller_1:baz', + 'expectedAction' => 'foo--controller-1:baz->bar--controller-2#log', + ]; + yield 'foo--controller-1:baz@document->bar--controller-2#log:capture' => [ + 'controllerName' => '@bar/controller_2', + 'actionName' => 'log:capture', + 'eventName' => '@foo/controller_1:baz@document', + 'expectedAction' => 'foo--controller-1:baz@document->bar--controller-2#log:capture', + ]; + } }