Skip to content

Commit eca78b6

Browse files
committed
bug #3147 [Turbo] Fix Mercure custom transport with turbo_steam_listen (Fan2Shrek)
This PR was merged into the 2.x branch. Discussion ---------- [Turbo] Fix Mercure custom transport with `turbo_steam_listen` Fix #3128 | Q | A | -------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | no <!-- required for new features, or documentation updates --> | Issues | Fix #3128 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This PR passes `$transport` to `$options`, allowing Mercure to successfully create authentication for the correct hub. Commits ------- 603bab8 [Turbo] Fix mercure auth the wrong hub with turbo_steam_listen
2 parents 375415b + 603bab8 commit eca78b6

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/Turbo/src/Twig/TurboRuntime.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public function __construct(
3636
*/
3737
public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string
3838
{
39-
$options['transport'] = $transport ??= $this->defaultTransport;
39+
if (\array_key_exists('hub', $options) && $transport !== $options['hub']) {
40+
throw new \InvalidArgumentException('When passing the "transport" option, the "hub" key in options is not allowed.');
41+
}
42+
43+
$options['hub'] = $transport ??= $this->defaultTransport;
4044

4145
if (!$this->turboStreamListenRenderers->has($transport)) {
4246
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport));
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)