Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,23 @@ uses Symfony's test client to render and make requests to your components::
// Assert that an event was not emitted
$this->assertComponentNotEmitEvent($testComponent->render(), 'decreaseEvent');

// dispatch events
$testComponent
->dispatchBrowserEvent('browserEvent')
->dispatchBrowserEvent('browserEvent', ['amount' => 2, 'unit' => 'kg']) // dispatch a browser event with arguments
;

// Assert that the event was dispatched
$this->assertComponentDispatchEvent($testComponent->render(), 'browserEvent')
// optionally, you can assert that the event was dispatched with specific data...
->withPayload(['amount' => 2, 'unit' => 'kg'])
// ... or only with a subset of data
->withPayloadSubset(['amount' => 2])
;

// Assert that an event was not dispatched
$this->assertComponentNotDispatchEvent($testComponent->render(), 'otherBrowserEvent');

// set live props
$testComponent
->set('count', 99)
Expand Down
21 changes: 21 additions & 0 deletions src/LiveComponent/src/Test/InteractsWithLiveComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Test\Util\AssertDispatchedEvent;
use Symfony\UX\LiveComponent\Test\Util\AssertEmittedEvent;
use Symfony\UX\TwigComponent\ComponentFactory;

Expand Down Expand Up @@ -59,4 +60,24 @@ protected function assertComponentNotEmitEvent(TestLiveComponent $testLiveCompon
{
self::assertNull($testLiveComponent->getEmittedEvent($testLiveComponent->render(), $eventName), \sprintf('The component "%s" did emit event "%s".', $testLiveComponent->getName(), $eventName));
}

protected function assertComponentDispatchEvent(TestLiveComponent $testLiveComponent, string $expectedEventName): AssertDispatchedEvent
{
$event = $testLiveComponent->getDispatchedEvent($testLiveComponent->render(), $expectedEventName);

self::assertNotNull(
$event,
\sprintf('The component "%s" did no dispatch event "%s".', $testLiveComponent->getName(), $expectedEventName)
);

return new AssertDispatchedEvent($this, $event['event'], $event['payload']);
}

protected function assertComponentNotDispatchEvent(TestLiveComponent $testLiveComponent, string $eventName): void
{
self::assertNull(
$testLiveComponent->getDispatchedEvent($testLiveComponent->render(), $eventName),
\sprintf('The component "%s" did dispatch event "%s".', $testLiveComponent->getName(), $eventName)
);
}
}
38 changes: 37 additions & 1 deletion src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\LiveComponent\Test;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -251,7 +252,7 @@ public function getEmittedEvent(RenderedComponent $render, string $eventName): ?
*/
public function getEmittedEvents(RenderedComponent $render): array
{
$emit = $render->crawler()->filter('[data-live-name-value]')->attr('data-live-events-to-emit-value');
$emit = $this->getComponentNameValue($render)->attr('data-live-events-to-emit-value');

if (null === $emit) {
return [];
Expand All @@ -260,6 +261,41 @@ public function getEmittedEvents(RenderedComponent $render): array
return json_decode($emit, associative: true, flags: \JSON_THROW_ON_ERROR);
}

/**
* @return ?array{data: array<string, int|float|string|bool|null>, event: non-empty-string}
*/
public function getDispatchedEvent(RenderedComponent $render, string $eventName): ?array
{
$events = $this->getDispatchedEvents($render);

foreach ($events as $event) {
if ($event['event'] === $eventName) {
return $event;
}
}

return null;
}

/**
* @return array<array{data: array<string, int|float|string|bool|null>, event: non-empty-string}>
*/
public function getDispatchedEvents(RenderedComponent $render): array
{
$dispatch = $this->getComponentNameValue($render)->attr('data-live-events-to-dispatch-value');

if (null === $dispatch) {
return [];
}

return json_decode($dispatch, associative: true, flags: \JSON_THROW_ON_ERROR);
}

private function getComponentNameValue(RenderedComponent $render): Crawler
{
return $render->crawler()->filter('[data-live-name-value]');
}

public function getName(): string
{
return $this->metadata->getName();
Expand Down
59 changes: 59 additions & 0 deletions src/LiveComponent/src/Test/Util/AssertDispatchedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\LiveComponent\Test\Util;

use PHPUnit\Framework\TestCase;

final class AssertDispatchedEvent
{
/**
* @param array<string, int|float|string|bool|null> $payload
*/
public function __construct(
private readonly TestCase $testCase,
private readonly string $eventName,
private readonly array $payload,
) {
}

/**
* @return self
*/
public function withPayloadSubset(array $expectedEventPayload): object
{
foreach ($expectedEventPayload as $key => $value) {
$this->testCase::assertArrayHasKey($key, $this->payload, \sprintf('The expected event "%s" data "%s" does not exists', $this->eventName, $key));
$this->testCase::assertSame(
$value,
$this->payload[$key],
\sprintf(
'The event "%s" data "%s" expect to be "%s", but "%s" given.',
$this->eventName,
$key,
$value,
$this->payload[$key]
)
);
}

return $this;
}

public function withPayload(array $expectedEventPayload): void
{
$this->testCase::assertEquals(
$expectedEventPayload,
$this->payload,
\sprintf('The event "%s" payload is different than expected.', $this->eventName)
);
}
}
11 changes: 8 additions & 3 deletions src/LiveComponent/tests/Fixtures/Component/ComponentWithEmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ final class ComponentWithEmit
use DefaultActionTrait;
use ComponentToolsTrait;

public $events = [];
public array $events = [];
public array $dispatchEvents = [];

#[LiveAction]
public function actionThatEmits(): void
Expand All @@ -36,9 +37,13 @@ public function actionThatEmits(): void
#[LiveAction]
public function actionThatDispatchesABrowserEvent(): void
{
$this->liveResponder->dispatchBrowserEvent(
$this->dispatchBrowserEvent(
'browser-event',
['fooKey' => 'barVal'],
[
'fooKey' => 'barVal',
'barKey' => 'fooVal',
],
);
$this->dispatchEvents = $this->liveResponder->getBrowserEventsToDispatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
Event: {{ event.event }}<br>
Data: {{ event.data|json_encode|raw }}<br>
{% endfor %}

{% for event in dispatchEvents %}
Event: {{ event.event }}<br>
Payload: {{ event.payload|json_encode|raw }}<br>
{% endfor %}
</div>
4 changes: 3 additions & 1 deletion src/LiveComponent/tests/Functional/LiveResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function testComponentCanDispatchBrowserEvents()
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
])
->assertSuccessful()
->assertSee('Event: browser-event')
->assertSee('Payload: {"fooKey":"barVal","barKey":"fooVal"}')
->crawler()
;

Expand All @@ -57,7 +59,7 @@ public function testComponentCanDispatchBrowserEvents()
$this->assertNotNull($browserDispatch);
$browserDispatchData = json_decode($browserDispatch, true);
$this->assertSame([
['event' => 'browser-event', 'payload' => ['fooKey' => 'barVal']],
['event' => 'browser-event', 'payload' => ['fooKey' => 'barVal', 'barKey' => 'fooVal']],
], $browserDispatchData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,76 @@ public function testComponentEmitsEventWithIncorrectDataFails()
'foo2' => 'bar2',
]);
}

public function testAssertComponentDispatchEvent()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->assertComponentDispatchEvent($testComponent, 'browser-event')
->withPayload([
'fooKey' => 'barVal',
'barKey' => 'fooVal',
]);
}

public function testAssertComponentDispatchEventFails()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The event "browser-event" payload is different than expected.');
$this->assertComponentDispatchEvent($testComponent, 'browser-event')->withPayload([
'fooKey' => 'barVal',
]);
}

public function testComponentDispatchesExpectedPartialEventData()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->assertComponentDispatchEvent($testComponent, 'browser-event')
->withPayloadSubset(['fooKey' => 'barVal'])
->withPayloadSubset(['barKey' => 'fooVal'])
;
}

public function testComponentDoesNotDispatchUnexpectedEvent()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->assertComponentNotDispatchEvent($testComponent, 'browser-event2');
}

public function testComponentDoesNotDispatchUnexpectedEventFails()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The component "component_with_emit" did dispatch event "browser-event".');
$this->assertComponentNotDispatchEvent($testComponent, 'browser-event');
}

public function testComponentDispatchesEventWithIncorrectDataFails()
{
$testComponent = $this->createLiveComponent('component_with_emit');

$testComponent->call('actionThatDispatchesABrowserEvent');

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The event "browser-event" payload is different than expected.');
$this->assertComponentDispatchEvent($testComponent, 'browser-event')->withPayload([
'fooKey' => 'barVal',
'fooKey2' => 'barVal2',
]);
}
}
Loading