|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\UX\StimulusBundle\Tests\Dto; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; |
| 17 | +use Twig\Environment; |
| 18 | +use Twig\Loader\ArrayLoader; |
| 19 | + |
| 20 | +final class StimulusActionAttributeTest extends TestCase |
| 21 | +{ |
| 22 | + private StimulusAttributes $stimulusAttributes; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->stimulusAttributes = new StimulusAttributes(new Environment(new ArrayLoader())); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider provideAddActionData |
| 31 | + */ |
| 32 | + #[DataProvider('provideAddActionData')] |
| 33 | + public function testAddAction(string $controllerName, string $actionName, ?string $eventName, string $expectedAction): void |
| 34 | + { |
| 35 | + $this->stimulusAttributes->addAction($controllerName, $actionName, $eventName); |
| 36 | + $attributesHtml = (string) $this->stimulusAttributes; |
| 37 | + self::assertSame(\sprintf('data-action="%s"', $expectedAction), $attributesHtml); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return iterable<array{ |
| 42 | + * controllerName: string, |
| 43 | + * actionName: string, |
| 44 | + * eventName: ?string, |
| 45 | + * expectedAction: string, |
| 46 | + * }> |
| 47 | + */ |
| 48 | + public static function provideAddActionData(): iterable |
| 49 | + { |
| 50 | + // basic datasets |
| 51 | + yield 'foo#bar' => [ |
| 52 | + 'controllerName' => 'foo', |
| 53 | + 'actionName' => 'bar', |
| 54 | + 'eventName' => null, |
| 55 | + 'expectedAction' => 'foo#bar', |
| 56 | + ]; |
| 57 | + yield 'baz->foo#bar' => [ |
| 58 | + 'controllerName' => 'foo', |
| 59 | + 'actionName' => 'bar', |
| 60 | + 'eventName' => 'baz', |
| 61 | + 'expectedAction' => 'baz->foo#bar', |
| 62 | + ]; |
| 63 | + |
| 64 | + // datasets from https://github.com/hotwired/stimulus |
| 65 | + yield 'keydown.esc@document->a#log' => [ |
| 66 | + 'controllerName' => 'a', |
| 67 | + 'actionName' => 'log', |
| 68 | + 'eventName' => 'keydown.esc@document', |
| 69 | + 'expectedAction' => 'keydown.esc@document->a#log', |
| 70 | + ]; |
| 71 | + yield 'keydown.enter->a#log' => [ |
| 72 | + 'controllerName' => 'a', |
| 73 | + 'actionName' => 'log', |
| 74 | + 'eventName' => 'keydown.enter', |
| 75 | + 'expectedAction' => 'keydown.enter->a#log', |
| 76 | + ]; |
| 77 | + yield 'keydown.shift+a->a#log' => [ |
| 78 | + 'controllerName' => 'a', |
| 79 | + 'actionName' => 'log', |
| 80 | + 'eventName' => 'keydown.shift+a', |
| 81 | + 'expectedAction' => 'keydown.shift+a->a#log', |
| 82 | + ]; |
| 83 | + yield 'keydown@window->c#log' => [ |
| 84 | + 'controllerName' => 'c', |
| 85 | + 'actionName' => 'log', |
| 86 | + 'eventName' => 'keydown@window', |
| 87 | + 'expectedAction' => 'keydown@window->c#log', |
| 88 | + ]; |
| 89 | + yield 'click->c#log:once' => [ |
| 90 | + 'controllerName' => 'c', |
| 91 | + 'actionName' => 'log:once', |
| 92 | + 'eventName' => 'click', |
| 93 | + 'expectedAction' => 'click->c#log:once', |
| 94 | + ]; |
| 95 | + |
| 96 | + // extended datasets |
| 97 | + yield 'vue:mount->foo#bar:passive' => [ |
| 98 | + 'controllerName' => 'foo', |
| 99 | + 'actionName' => 'bar:passive', |
| 100 | + 'eventName' => 'vue:mount', |
| 101 | + 'expectedAction' => 'vue:mount->foo#bar:passive', |
| 102 | + ]; |
| 103 | + yield 'foo--controller-1:baz->bar--controller-2#log' => [ |
| 104 | + 'controllerName' => '@bar/controller_2', |
| 105 | + 'actionName' => 'log', |
| 106 | + 'eventName' => '@foo/controller_1:baz', |
| 107 | + 'expectedAction' => 'foo--controller-1:baz->bar--controller-2#log', |
| 108 | + ]; |
| 109 | + yield 'foo--controller-1:baz@document->bar--controller-2#log:capture' => [ |
| 110 | + 'controllerName' => '@bar/controller_2', |
| 111 | + 'actionName' => 'log:capture', |
| 112 | + 'eventName' => '@foo/controller_1:baz@document', |
| 113 | + 'expectedAction' => 'foo--controller-1:baz@document->bar--controller-2#log:capture', |
| 114 | + ]; |
| 115 | + } |
| 116 | +} |
0 commit comments