|  | 
|  | 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\Twig; | 
|  | 13 | + | 
|  | 14 | +use PHPUnit\Framework\TestCase; | 
|  | 15 | +use Symfony\Contracts\Service\ServiceLocatorTrait; | 
|  | 16 | +use Symfony\Contracts\Service\ServiceProviderInterface; | 
|  | 17 | +use Symfony\UX\Turbo\Twig\TurboRuntime; | 
|  | 18 | +use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface; | 
|  | 19 | +use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface; | 
|  | 20 | +use Twig\Environment; | 
|  | 21 | + | 
|  | 22 | +final class TurboRuntimeTest extends TestCase | 
|  | 23 | +{ | 
|  | 24 | +    public function testRenderTurboStreamListen() | 
|  | 25 | +    { | 
|  | 26 | +        $twig = $this->createMock(Environment::class); | 
|  | 27 | +        $renderer = $this->createMock(TurboStreamListenRendererInterface::class); | 
|  | 28 | +        $renderer->expects($this->once()) | 
|  | 29 | +            ->method('renderTurboStreamListen') | 
|  | 30 | +            ->willReturn('rendered-attributes') | 
|  | 31 | +        ; | 
|  | 32 | +        $container = new class(['default' => fn () => $renderer]) implements ServiceProviderInterface { | 
|  | 33 | +            use ServiceLocatorTrait; | 
|  | 34 | +        }; | 
|  | 35 | + | 
|  | 36 | +        $runtime = new TurboRuntime($container, 'default'); | 
|  | 37 | +        $runtime->renderTurboStreamListen($twig, 'a_topic'); | 
|  | 38 | +    } | 
|  | 39 | + | 
|  | 40 | +    public function testRenderTurboStreamListenWithMultipleHubs() | 
|  | 41 | +    { | 
|  | 42 | +        $twig = $this->createMock(Environment::class); | 
|  | 43 | +        $renderer1 = $this->createMock(TurboStreamListenRendererInterface::class); | 
|  | 44 | +        $renderer2 = $this->createMock(TurboStreamListenRendererWithOptionsInterface::class); | 
|  | 45 | +        $renderer2->expects($this->once()) | 
|  | 46 | +            ->method('renderTurboStreamListen') | 
|  | 47 | +            ->with($twig, 'a_topic', ['hub' => 'hub2']) | 
|  | 48 | +            ->willReturn('rendered-attributes') | 
|  | 49 | +        ; | 
|  | 50 | +        $container = new class(['hub1' => fn () => $renderer1, 'hub2' => fn () => $renderer2]) implements ServiceProviderInterface { | 
|  | 51 | +            use ServiceLocatorTrait; | 
|  | 52 | +        }; | 
|  | 53 | + | 
|  | 54 | +        $runtime = new TurboRuntime($container, 'hub1'); | 
|  | 55 | +        $runtime->renderTurboStreamListen($twig, 'a_topic', 'hub2'); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    public function testRenderTurboStreamListenWithDifferentsHub() | 
|  | 59 | +    { | 
|  | 60 | +        $this->expectException(\InvalidArgumentException::class); | 
|  | 61 | + | 
|  | 62 | +        $twig = $this->createMock(Environment::class); | 
|  | 63 | +        $renderer = $this->createMock(TurboStreamListenRendererInterface::class); | 
|  | 64 | +        $container = new class(['default' => fn () => $renderer]) implements ServiceProviderInterface { | 
|  | 65 | +            use ServiceLocatorTrait; | 
|  | 66 | +        }; | 
|  | 67 | + | 
|  | 68 | +        $runtime = new TurboRuntime($container, 'default'); | 
|  | 69 | +        $runtime->renderTurboStreamListen($twig, 'a_topic', 'default', ['hub' => 'hub3']); | 
|  | 70 | +    } | 
|  | 71 | +} | 
0 commit comments