|
| 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\Turbo\Tests\Bridge\Mercure; |
| 13 | + |
| 14 | +use App\Entity\Book; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 16 | +use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; |
| 17 | + |
| 18 | +final class TurboStreamListenRendererTest extends KernelTestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider provideTestCases |
| 22 | + * |
| 23 | + * @param array<mixed> $context |
| 24 | + */ |
| 25 | + public function testRenderTurboStreamListen(string $template, array $context, string $expectedResult): void |
| 26 | + { |
| 27 | + $twig = self::getContainer()->get('twig'); |
| 28 | + self::assertInstanceOf(\Twig\Environment::class, $twig); |
| 29 | + |
| 30 | + $this->assertSame($expectedResult, $twig->createTemplate($template)->render($context)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return iterable<array{0: string, 1: array<mixed>, 2: string}> |
| 35 | + */ |
| 36 | + public static function provideTestCases(): iterable |
| 37 | + { |
| 38 | + $newEscape = (new \ReflectionClass(StimulusAttributes::class))->hasMethod('escape'); |
| 39 | + |
| 40 | + $book = new Book(); |
| 41 | + $book->id = 123; |
| 42 | + |
| 43 | + yield [ |
| 44 | + "{{ turbo_stream_listen('a_topic') }}", |
| 45 | + [], |
| 46 | + $newEscape |
| 47 | + ? 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="a_topic"' |
| 48 | + : 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="a_topic"', |
| 49 | + ]; |
| 50 | + |
| 51 | + yield [ |
| 52 | + "{{ turbo_stream_listen('App\\Entity\\Book') }}", |
| 53 | + [], |
| 54 | + $newEscape |
| 55 | + ? 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="AppEntityBook"' |
| 56 | + : 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="AppEntityBook"', |
| 57 | + ]; |
| 58 | + |
| 59 | + yield [ |
| 60 | + '{{ turbo_stream_listen(book) }}', |
| 61 | + ['book' => $book], |
| 62 | + $newEscape |
| 63 | + ? 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="https://symfony.com/ux-turbo/App%5CEntity%5CBook/123"' |
| 64 | + : 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="https://symfony.com/ux-turbo/App%5CEntity%5CBook/123"', |
| 65 | + ]; |
| 66 | + |
| 67 | + yield [ |
| 68 | + "{{ turbo_stream_listen(['a_topic', 'App\\Entity\\Book', book]) }}", |
| 69 | + ['book' => $book], |
| 70 | + $newEscape |
| 71 | + ? 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topics-value="["a_topic","AppEntityBook","https:\/\/symfony.com\/ux-turbo\/App%5CEntity%5CBook\/123"]"' |
| 72 | + : 'data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:3000/.well-known/mercure" data-symfony--ux-turbo--mercure-turbo-stream-topics-value="["a_topic","AppEntityBook","https:\/\/symfony.com\/ux-turbo\/App%5CEntity%5CBook\/123"]"', |
| 73 | + ]; |
| 74 | + } |
| 75 | +} |
0 commit comments