-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2407 [Turbo] Add support for providing multiple mercure topi…
…cs to `turbo_stream_listen` (norkunas, Kocal) This PR was merged into the 2.x branch. Discussion ---------- [Turbo] Add support for providing multiple mercure topics to `turbo_stream_listen` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | Fix #213 | License | MIT Commits ------- 2469012 [Turbo] PHPStan 4734757 [Turbo] Simplify $topics e3b72c1 [Turbo] Add support for providing multiple mercure topics to `turbo_stream_listen`
- Loading branch information
Showing
8 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Bridge\Mercure; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TopicSet | ||
{ | ||
/** | ||
* @param array<string|object> $topics | ||
*/ | ||
public function __construct( | ||
private array $topics, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string|object> | ||
*/ | ||
public function getTopics(): array | ||
{ | ||
return $this->topics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Turbo/tests/Bridge/Mercure/TurboStreamListenRendererTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Tests\Bridge\Mercure; | ||
|
||
use App\Entity\Book; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; | ||
|
||
final class TurboStreamListenRendererTest extends KernelTestCase | ||
{ | ||
/** | ||
* @dataProvider provideTestCases | ||
* | ||
* @param array<mixed> $context | ||
*/ | ||
public function testRenderTurboStreamListen(string $template, array $context, string $expectedResult): void | ||
{ | ||
$twig = self::getContainer()->get('twig'); | ||
self::assertInstanceOf(\Twig\Environment::class, $twig); | ||
|
||
$this->assertSame($expectedResult, $twig->createTemplate($template)->render($context)); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1: array<mixed>, 2: string}> | ||
*/ | ||
public static function provideTestCases(): iterable | ||
{ | ||
$newEscape = (new \ReflectionClass(StimulusAttributes::class))->hasMethod('escape'); | ||
|
||
$book = new Book(); | ||
$book->id = 123; | ||
|
||
yield [ | ||
"{{ turbo_stream_listen('a_topic') }}", | ||
[], | ||
$newEscape | ||
? '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"' | ||
: '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"', | ||
]; | ||
|
||
yield [ | ||
"{{ turbo_stream_listen('App\\Entity\\Book') }}", | ||
[], | ||
$newEscape | ||
? '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"' | ||
: '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"', | ||
]; | ||
|
||
yield [ | ||
'{{ turbo_stream_listen(book) }}', | ||
['book' => $book], | ||
$newEscape | ||
? '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"' | ||
: '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"', | ||
]; | ||
|
||
yield [ | ||
"{{ turbo_stream_listen(['a_topic', 'App\\Entity\\Book', book]) }}", | ||
['book' => $book], | ||
$newEscape | ||
? '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"]"' | ||
: '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"]"', | ||
]; | ||
} | ||
} |