Skip to content

Commit f19f6e5

Browse files
committed
Completing test
1 parent b80b6f9 commit f19f6e5

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

src/Turbo/src/Bridge/Mercure/Broadcaster.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\UX\Turbo\Bridge\Mercure;
1313

14+
use Doctrine\Common\Util\ClassUtils;
1415
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1516
use Symfony\Component\Mercure\HubInterface;
1617
use Symfony\Component\Mercure\Update;
18+
use Symfony\Component\VarExporter\LazyObjectInterface;
1719
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
1820

1921
/**
@@ -61,7 +63,11 @@ public function broadcast(object $entity, string $action, array $options): void
6163
return;
6264
}
6365

64-
$entityClass = $entity::class;
66+
if ($entity instanceof LazyObjectInterface) {
67+
$entityClass = get_parent_class($entity);
68+
} else {
69+
$entityClass = ClassUtils::getClass($entity);
70+
}
6571

6672
if (!isset($options['rendered_action'])) {
6773
throw new \InvalidArgumentException(sprintf('Cannot broadcast entity of class "%s" as option "rendered_action" is missing.', $entityClass));

src/Turbo/src/Broadcaster/TwigBroadcaster.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\UX\Turbo\Broadcaster;
1313

14+
use Doctrine\Common\Util\ClassUtils;
15+
use Symfony\Component\VarExporter\LazyObjectInterface;
1416
use Twig\Environment;
1517

1618
/**
@@ -42,8 +44,15 @@ public function broadcast(object $entity, string $action, array $options): void
4244
$options['id'] = $id;
4345
}
4446

47+
// handle proxies (both styles)
48+
if ($entity instanceof LazyObjectInterface) {
49+
$class = get_parent_class($entity);
50+
} else {
51+
$class = ClassUtils::getClass($entity);
52+
}
53+
4554
if (null === $template = $options['template'] ?? null) {
46-
$template = $entity::class;
55+
$template = $class;
4756
foreach ($this->templatePrefixes as $namespace => $prefix) {
4857
if (str_starts_with($template, $namespace)) {
4958
$template = substr_replace($template, $prefix, 0, \strlen($namespace));

src/Turbo/tests/BroadcastTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function setUp(): void
3333
parent::setUp();
3434
}
3535

36-
public function testBroadcast(): void
36+
public function testBroadcastBasic(): void
3737
{
3838
($client = self::createPantherClient())->request('GET', '/books');
3939

@@ -94,7 +94,12 @@ public function testBroadcastWithProxy(): void
9494
// testing that Artist is updated, even though it's saved as Proxy
9595
($client = self::createPantherClient())->request('GET', '/artistFromSong');
9696

97-
$crawler = $client->submitForm('Submit');
97+
// submit first time to create the artist
98+
$client->submitForm('Submit');
99+
$this->assertSelectorWillContain('#artists', 'testing artist');
100+
101+
// submit again to update the artist
102+
$client->submitForm('Submit');
98103
$this->assertSelectorWillContain('#artists', 'testing artist after change');
99104
// this part is from the stream template
100105
$this->assertSelectorWillContain('#artists', ', updated)');

src/Turbo/tests/app/Kernel.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use Symfony\Component\HttpFoundation\Response;
3434
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3535
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
36-
use Symfony\Component\Mercure\Hub;
3736
use Symfony\Component\Mercure\HubInterface;
3837
use Symfony\Component\Mercure\Update;
3938
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
@@ -133,7 +132,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void
133132
$routes->add('songs', '/songs')->controller('kernel::songs');
134133
$routes->add('artists', '/artists')->controller('kernel::artists');
135134
$routes->add('artist', '/artists/{id}')->controller('kernel::artist');
136-
$routes->add('artist', '/artistFromSong')->controller('kernel::artistFromSong');
135+
$routes->add('artist_from_song', '/artistFromSong')->controller('kernel::artistFromSong');
137136
}
138137

139138
public function getProjectDir(): string
@@ -267,24 +266,29 @@ public function artist(Request $request, EntityManagerInterface $doctrine, Envir
267266

268267
public function artistFromSong(Request $request, EntityManagerInterface $doctrine, Environment $twig): Response
269268
{
270-
if (!$request->isMethod('POST')) {
271-
$artist = new Artist();
272-
$artist->name = 'testing artist';
269+
$song = null;
270+
if ($request->isMethod('POST')) {
271+
// on first post, create the objects
272+
// on second, update the artist
273+
$id = $request->get('id');
274+
if (!$id) {
275+
$artist = new Artist();
276+
$artist->name = 'testing artist';
273277

274-
$song = new Song();
275-
$song->artist = $artist;
276-
$song->title = 'testing song title';
278+
$song = new Song();
279+
$song->artist = $artist;
280+
$song->title = 'testing song title';
277281

278-
$doctrine->persist($artist);
279-
$doctrine->persist($song);
280-
$doctrine->flush();
281-
} else {
282-
$id = $request->get('id');
283-
$song = $doctrine->find(Song::class, $id);
284-
$artist = $song->artist;
285-
$artist->name = $artist->name.' after change';
282+
$doctrine->persist($artist);
283+
$doctrine->persist($song);
284+
$doctrine->flush();
285+
} else {
286+
$song = $doctrine->find(Song::class, $id);
287+
$artist = $song->artist;
288+
$artist->name = $artist->name.' after change';
286289

287-
$doctrine->flush();
290+
$doctrine->flush();
291+
}
288292
}
289293

290294
return new Response($twig->render('artist_from_song.html.twig', [

src/Turbo/tests/app/templates/artist_from_song.html.twig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
<div id="artists" {{ turbo_stream_listen('App\\Entity\\Artist') }}></div>
77

8-
<form method="post" enctype="application/x-www-form-urlencoded">
9-
<input name="id" value="{{ song.id }}">
10-
<button>Submit</button>
11-
</form>
8+
<turbo-frame id="api">
9+
<form method="post" enctype="application/x-www-form-urlencoded">
10+
<input name="id" value="{{ song ? song.id }}">
11+
<button>Submit</button>
12+
</form>
13+
</turbo-frame>
1214
{% endblock %}

0 commit comments

Comments
 (0)