Skip to content

Commit 331ac28

Browse files
committed
complete tests
1 parent cf0a8ea commit 331ac28

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

src/LiveComponent/src/Controller/BatchActionController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Controller;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1617
use Symfony\Component\HttpKernel\HttpKernelInterface;
1718
use Symfony\UX\TwigComponent\MountedComponent;
@@ -27,7 +28,7 @@ public function __construct(private HttpKernelInterface $kernel)
2728
{
2829
}
2930

30-
public function __invoke(Request $request, MountedComponent $mounted, string $serviceId, array $actions): void
31+
public function __invoke(Request $request, MountedComponent $mounted, string $serviceId, array $actions): ?Response
3132
{
3233
$request->attributes->set('_mounted_component', $mounted);
3334

@@ -41,10 +42,13 @@ public function __invoke(Request $request, MountedComponent $mounted, string $se
4142
'_route' => 'live_component',
4243
]);
4344

44-
$this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
45+
$response = $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
4546

46-
// todo handle redirects
47-
// todo handle exceptions
47+
if ($response->isRedirection()) {
48+
return $response;
49+
}
4850
}
51+
52+
return null;
4953
}
5054
}

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ public function onKernelController(ControllerEvent $event): void
160160
throw new NotFoundHttpException(sprintf('The action "%s" either doesn\'t exist or is not allowed in "%s". Make sure it exist and has the LiveAction attribute above it.', $action, \get_class($component)));
161161
}
162162

163-
if ($request->attributes->has('_mounted_component')) {
164-
// sub-request
165-
$event->setController([$request->attributes->get('_mounted_component')->getComponent(), $action]);
166-
} else {
163+
if ($event->isMainRequest()) {
167164
$data = $this->parseDataFor($request);
168165

169166
$request->attributes->set('_component_action_args', $data['args']);
@@ -172,6 +169,9 @@ public function onKernelController(ControllerEvent $event): void
172169
$data['data'],
173170
$request->attributes->get('_component_name')
174171
));
172+
} else {
173+
// sub-request
174+
$event->setController([$request->attributes->get('_mounted_component')->getComponent(), $action]);
175175
}
176176

177177
$actionArguments = $request->attributes->get('_component_action_args', []);

src/LiveComponent/tests/Fixtures/Component/WithActions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
1313

14+
use Symfony\Component\HttpFoundation\RedirectResponse;
1415
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1516
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1617
use Symfony\UX\LiveComponent\Attribute\LiveAction;
@@ -31,4 +32,16 @@ public function add(#[LiveArg] string $what, UrlGeneratorInterface $router): voi
3132
{
3233
$this->items[] = $what;
3334
}
35+
36+
#[LiveAction]
37+
public function redirect(UrlGeneratorInterface $router): RedirectResponse
38+
{
39+
return new RedirectResponse($router->generate('homepage'));
40+
}
41+
42+
#[LiveAction]
43+
public function exception(): void
44+
{
45+
throw new \RuntimeException('Exception message');
46+
}
3447
}

src/LiveComponent/tests/Functional/Controller/BatchActionControllerTest.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,65 @@ public function testCanBatchActions(): void
6868

6969
public function testCsrfTokenIsChecked(): void
7070
{
71-
$this->markTestIncomplete();
72-
}
71+
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_actions'));
7372

74-
public function testMustBeLiveComponent(): void
75-
{
76-
$this->markTestIncomplete();
73+
$this->browser()
74+
->post('/_components/with_actions/_batch', ['json' => [
75+
'data' => $dehydrated,
76+
'actions' => [],
77+
]])
78+
->assertStatus(400)
79+
;
7780
}
7881

7982
public function testRedirect(): void
8083
{
81-
$this->markTestIncomplete();
84+
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_actions'));
85+
86+
$this->browser()
87+
->throwExceptions()
88+
->get('/_components/with_actions', ['json' => ['data' => $dehydrated]])
89+
->assertSuccessful()
90+
->interceptRedirects()
91+
->use(function (HtmlResponse $response, KernelBrowser $browser) {
92+
$browser->post('/_components/with_actions/_batch', [
93+
'json' => [
94+
'data' => json_decode($response->crawler()->filter('ul')->first()->attr('data-live-data-value')),
95+
'actions' => [
96+
['name' => 'add', 'args' => ['what' => 'second']],
97+
['name' => 'redirect'],
98+
['name' => 'add', 'args' => ['what' => 'fourth']],
99+
],
100+
],
101+
'headers' => ['X-CSRF-TOKEN' => $response->crawler()->filter('ul')->first()->attr('data-live-csrf-value')],
102+
]);
103+
})
104+
->assertRedirectedTo('/')
105+
;
82106
}
83107

84108
public function testException(): void
85109
{
86-
$this->markTestIncomplete();
110+
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_actions'));
111+
112+
$this->browser()
113+
->get('/_components/with_actions', ['json' => ['data' => $dehydrated]])
114+
->assertSuccessful()
115+
->use(function (HtmlResponse $response, KernelBrowser $browser) {
116+
$browser->post('/_components/with_actions/_batch', [
117+
'json' => [
118+
'data' => json_decode($response->crawler()->filter('ul')->first()->attr('data-live-data-value')),
119+
'actions' => [
120+
['name' => 'add', 'args' => ['what' => 'second']],
121+
['name' => 'exception'],
122+
['name' => 'add', 'args' => ['what' => 'fourth']],
123+
],
124+
],
125+
'headers' => ['X-CSRF-TOKEN' => $response->crawler()->filter('ul')->first()->attr('data-live-csrf-value')],
126+
]);
127+
})
128+
->assertStatus(500)
129+
->assertContains('Exception message')
130+
;
87131
}
88132
}

0 commit comments

Comments
 (0)