Skip to content

Commit 2c5191f

Browse files
committed
Feedback, add tests and add getName function on TestLiveComponent
1 parent 5d73794 commit 2c5191f

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

src/LiveComponent/src/Test/InteractsWithLiveComponents.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,34 @@ protected function createLiveComponent(string $name, array $data = [], ?KernelBr
4545
);
4646
}
4747

48-
protected function assertComponentEmitEvent(TestLiveComponent $testLiveComponent, string $eventName, ?array $parameters = null): void
48+
protected function assertComponentEmitEvent(TestLiveComponent $testLiveComponent, string $expectedEventName, ?array $expectedEventData = null): void
4949
{
50-
$eventData = $testLiveComponent->getEmittedEvent($testLiveComponent->render(), $eventName);
50+
$event = $testLiveComponent->getEmittedEvent($testLiveComponent->render(), $expectedEventName);
5151

52-
$this->assertNotNull($eventData, \sprintf('Event %s not emitted', $eventName));
52+
$this->assertNotNull($event, \sprintf('The component "%s" did not emit event "%s".', $testLiveComponent->getName(), $expectedEventName));
5353

54-
if (null === $parameters) {
54+
if (null === $expectedEventData) {
5555
return;
5656
}
5757

58-
foreach ($parameters as $key => $value) {
59-
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key));
58+
foreach ($expectedEventData as $key => $value) {
59+
$this->assertArrayHasKey($key, $event['data'], \sprintf('The expected event "%s" data "%s" does not exists', $event['event'], $key));
60+
$this->assertSame(
61+
$value,
62+
$event['data'][$key],
63+
\sprintf(
64+
'The expected event "%s" data "%s" expected "%s" but "%s" given',
65+
$event['event'],
66+
$key,
67+
$value,
68+
$event['data'][$key]
69+
)
70+
);
6071
}
6172
}
6273

6374
protected function assertComponentNotEmitEvent(TestLiveComponent $testLiveComponent, string $eventName): void
6475
{
65-
$this->assertNull($testLiveComponent->getEmittedEvent($testLiveComponent->render(), $eventName), \sprintf('Event %s emitted', $eventName));
76+
$this->assertNull($testLiveComponent->getEmittedEvent($testLiveComponent->render(), $eventName), \sprintf('The component "%s" did not emit event "%s".', $testLiveComponent->getName(), $eventName));
6677
}
6778
}

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ private function flattenFormValues(array $values, string $prefix = ''): array
235235
*/
236236
public function getEmittedEvent(RenderedComponent $render, string $eventName): ?array
237237
{
238-
$eventsData = $this->getEmittedEvents($render);
238+
$events = $this->getEmittedEvents($render);
239239

240-
foreach ($eventsData as $eventData) {
241-
if ($eventData['event'] === $eventName) {
242-
return $eventData;
240+
foreach ($events as $event) {
241+
if ($event['event'] === $eventName) {
242+
return $event;
243243
}
244244
}
245245

@@ -251,13 +251,17 @@ public function getEmittedEvent(RenderedComponent $render, string $eventName): ?
251251
*/
252252
public function getEmittedEvents(RenderedComponent $render): array
253253
{
254-
$div = $render->crawler()->filter('[data-live-name-value]');
255-
$emit = $div->attr('data-live-events-to-emit-value');
254+
$emit = $render->crawler()->filter('[data-live-name-value]')->attr('data-live-events-to-emit-value');
256255

257256
if (null === $emit) {
258257
return [];
259258
}
260259

261260
return json_decode($emit, associative: true, flags: \JSON_THROW_ON_ERROR);
262261
}
262+
263+
public function getName(): string
264+
{
265+
return $this->metadata->getName();
266+
}
263267
}

src/LiveComponent/tests/Functional/Test/InteractsWithLiveComponentsTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function testSetLocaleRenderLocalizedComponent(): void
219219
$this->assertStringContainsString('Locale: de', $testComponent->render());
220220
}
221221

222-
public function testComponentEmitsExpectedEventWithParameters(): void
222+
public function testComponentEmitsExpectedEventWithExpectedEventData(): void
223223
{
224224
$testComponent = $this->createLiveComponent('component_with_emit');
225225

@@ -236,20 +236,44 @@ public function testComponentDoesNotEmitUnexpectedEvent(): void
236236

237237
$testComponent->call('actionThatEmits');
238238

239-
$this->assertComponentNotEmitEvent($testComponent, 'non_emitted_event');
239+
$this->assertComponentNotEmitEvent($testComponent, 'event2');
240240
}
241241

242-
public function testComponentEmitsEventWithWrongParametersFails(): void
242+
public function testComponentDoesNotEmitUnexpectedEventFails(): void
243243
{
244244
$testComponent = $this->createLiveComponent('component_with_emit');
245245

246246
$testComponent->call('actionThatEmits');
247247

248248
$this->expectException(AssertionFailedError::class);
249-
$this->expectExceptionMessage('EventData (foo2) is not valid');
249+
$this->expectExceptionMessage('The component "component_with_emit" did not emit event "event1".');
250+
$this->assertComponentNotEmitEvent($testComponent, 'event1');
251+
}
252+
253+
public function testComponentEmitsEventWithNotFoundExpectedDataFails(): void
254+
{
255+
$testComponent = $this->createLiveComponent('component_with_emit');
256+
257+
$testComponent->call('actionThatEmits');
258+
259+
$this->expectException(AssertionFailedError::class);
260+
$this->expectExceptionMessage('The expected event "event1" data "foo2" does not exists');
250261
$this->assertComponentEmitEvent($testComponent, 'event1', [
251262
'foo' => 'bar',
252263
'foo2' => 'bar2',
253264
]);
254265
}
266+
267+
public function testComponentEmitsEventWithNotValidExpectedDataFails(): void
268+
{
269+
$testComponent = $this->createLiveComponent('component_with_emit');
270+
271+
$testComponent->call('actionThatEmits');
272+
273+
$this->expectException(AssertionFailedError::class);
274+
$this->expectExceptionMessage('The expected event "event1" data "foo" expected "bar2" but "bar" given');
275+
$this->assertComponentEmitEvent($testComponent, 'event1', [
276+
'foo' => 'bar2',
277+
]);
278+
}
255279
}

0 commit comments

Comments
 (0)