|
17 | 17 | use Symfony\UX\LiveComponent\Tests\ContainerBC; |
18 | 18 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1; |
19 | 19 | use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2; |
| 20 | +use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6; |
20 | 21 | use Symfony\UX\LiveComponent\Tests\Fixture\Entity\Entity1; |
21 | 22 | use Symfony\UX\TwigComponent\ComponentFactory; |
22 | 23 | use Zenstruck\Browser\Response\HtmlResponse; |
@@ -240,4 +241,43 @@ public function testCanRedirectFromComponentAction(): void |
240 | 241 | ->assertJsonMatches('redirect_url', '/') |
241 | 242 | ; |
242 | 243 | } |
| 244 | + |
| 245 | + public function testInjectsLiveArgs(): void |
| 246 | + { |
| 247 | + /** @var LiveComponentHydrator $hydrator */ |
| 248 | + $hydrator = self::getContainer()->get('ux.live_component.component_hydrator'); |
| 249 | + |
| 250 | + /** @var ComponentFactory $factory */ |
| 251 | + $factory = self::getContainer()->get('ux.twig_component.component_factory'); |
| 252 | + |
| 253 | + /** @var Component6 $component */ |
| 254 | + $component = $factory->create('component6'); |
| 255 | + |
| 256 | + $dehydrated = $hydrator->dehydrate($component); |
| 257 | + $token = null; |
| 258 | + |
| 259 | + $dehydrated['args'] = http_build_query(['arg1' => 'hello', 'arg2' => 666, 'custom' => '33.3']); |
| 260 | + |
| 261 | + $this->browser() |
| 262 | + ->throwExceptions() |
| 263 | + ->get('/_components/component6?'.http_build_query($dehydrated)) |
| 264 | + ->assertSuccessful() |
| 265 | + ->assertHeaderContains('Content-Type', 'html') |
| 266 | + ->assertContains('Arg1: not provided') |
| 267 | + ->assertContains('Arg2: not provided') |
| 268 | + ->assertContains('Arg3: not provided') |
| 269 | + ->use(function (HtmlResponse $response) use (&$token) { |
| 270 | + // get a valid token to use for actions |
| 271 | + $token = $response->crawler()->filter('div')->first()->attr('data-live-csrf-value'); |
| 272 | + }) |
| 273 | + ->post('/_components/component6/inject?'.http_build_query($dehydrated), [ |
| 274 | + 'headers' => ['X-CSRF-TOKEN' => $token], |
| 275 | + ]) |
| 276 | + ->assertSuccessful() |
| 277 | + ->assertHeaderContains('Content-Type', 'html') |
| 278 | + ->assertContains('Arg1: hello') |
| 279 | + ->assertContains('Arg2: 666') |
| 280 | + ->assertContains('Arg3: 33.3') |
| 281 | + ; |
| 282 | + } |
243 | 283 | } |
0 commit comments