Skip to content

Commit 04d4955

Browse files
committed
Add withData and withDataSubset functions
1 parent b433fd1 commit 04d4955

File tree

4 files changed

+60
-38
lines changed

4 files changed

+60
-38
lines changed

src/LiveComponent/src/Test/InteractsWithLiveComponents.php

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

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

5252
$this->assertNotNull($event, \sprintf('The component "%s" did not emit event "%s".', $testLiveComponent->getName(), $expectedEventName));
5353

54-
if (null === $expectedEventData) {
55-
return;
56-
}
54+
return new class($this, $event['event'], $event['data']) {
55+
/**
56+
* @param array<string, int|float|string|bool|null> $data
57+
*/
58+
public function __construct(private KernelTestCase $parent, private readonly string $eventName, private readonly array $data) {}
5759

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-
);
71-
}
60+
public function withDataSubset(array $expectedEventData): void
61+
{
62+
foreach ($expectedEventData as $key => $value) {
63+
$this->parent->assertArrayHasKey($key, $this->data, \sprintf('The expected event "%s" data "%s" does not exists', $this->eventName, $key));
64+
$this->parent->assertSame(
65+
$value,
66+
$this->data[$key],
67+
\sprintf(
68+
'The expected event "%s" data "%s" expected "%s" but "%s" given',
69+
$this->eventName,
70+
$key,
71+
$value,
72+
$this->data[$key]
73+
)
74+
);
75+
}
76+
}
77+
public function withData(array $expectedEventData): void
78+
{
79+
$this->parent->assertEquals($expectedEventData, $this->data, sprintf('The expected event "%s" data does not match.', $this->eventName));
80+
}
81+
};
7282
}
7383

7484
protected function assertComponentNotEmitEvent(TestLiveComponent $testLiveComponent, string $eventName): void

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ComponentWithEmit
2929
#[LiveAction]
3030
public function actionThatEmits(): void
3131
{
32-
$this->emit('event1', ['foo' => 'bar']);
32+
$this->emit('event1', ['foo' => 'bar', 'bar' => 'foo']);
3333
$this->events = $this->liveResponder->getEventsToEmit();
3434
}
3535

src/LiveComponent/tests/Functional/LiveResponderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testComponentCanEmitEvents(): void
3535
])
3636
->assertSuccessful()
3737
->assertSee('Event: event1')
38-
->assertSee('Data: {"foo":"bar"}');
38+
->assertSee('Data: {"foo":"bar","bar":"foo"}');
3939
}
4040

4141
public function testComponentCanDispatchBrowserEvents(): void

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

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

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

226226
$testComponent->call('actionThatEmits');
227227

228-
$this->assertComponentEmitEvent($testComponent, 'event1', [
228+
$this->assertComponentEmitEvent($testComponent, 'event1')->withData([
229229
'foo' => 'bar',
230+
'bar' => 'foo',
230231
]);
231232
}
232233

233-
public function testComponentDoesNotEmitUnexpectedEvent(): void
234+
public function testComponentEmitsExpectedEventDataFails(): void
234235
{
235236
$testComponent = $this->createLiveComponent('component_with_emit');
236237

237238
$testComponent->call('actionThatEmits');
238239

239-
$this->assertComponentNotEmitEvent($testComponent, 'event2');
240+
$this->expectException(AssertionFailedError::class);
241+
$this->expectExceptionMessage('The expected event "event1" data does not match');
242+
$this->assertComponentEmitEvent($testComponent, 'event1')->withData([
243+
'foo' => 'bar',
244+
]);
240245
}
241246

242-
public function testComponentDoesNotEmitUnexpectedEventFails(): void
247+
public function testComponentEmitsExpectedPartialEventData(): void
243248
{
244249
$testComponent = $this->createLiveComponent('component_with_emit');
245250

246251
$testComponent->call('actionThatEmits');
247252

248-
$this->expectException(AssertionFailedError::class);
249-
$this->expectExceptionMessage('The component "component_with_emit" did not emit event "event1".');
250-
$this->assertComponentNotEmitEvent($testComponent, 'event1');
253+
$this->assertComponentEmitEvent($testComponent, 'event1')->withDataSubset([
254+
'foo' => 'bar',
255+
]);
256+
}
257+
258+
public function testComponentDoesNotEmitUnexpectedEvent(): void
259+
{
260+
$testComponent = $this->createLiveComponent('component_with_emit');
261+
262+
$testComponent->call('actionThatEmits');
263+
264+
$this->assertComponentNotEmitEvent($testComponent, 'event2');
251265
}
252266

253-
public function testComponentEmitsEventWithNotFoundExpectedDataFails(): void
267+
public function testComponentDoesNotEmitUnexpectedEventFails(): void
254268
{
255269
$testComponent = $this->createLiveComponent('component_with_emit');
256270

257271
$testComponent->call('actionThatEmits');
258272

259273
$this->expectException(AssertionFailedError::class);
260-
$this->expectExceptionMessage('The expected event "event1" data "foo2" does not exists');
261-
$this->assertComponentEmitEvent($testComponent, 'event1', [
262-
'foo' => 'bar',
263-
'foo2' => 'bar2',
264-
]);
274+
$this->expectExceptionMessage('The component "component_with_emit" did not emit event "event1".');
275+
$this->assertComponentNotEmitEvent($testComponent, 'event1');
265276
}
266277

267-
public function testComponentEmitsEventWithNotValidExpectedDataFails(): void
278+
public function testComponentEmitsEventWithIncorrectDataFails(): void
268279
{
269280
$testComponent = $this->createLiveComponent('component_with_emit');
270281

271282
$testComponent->call('actionThatEmits');
272283

273284
$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',
285+
$this->expectExceptionMessage('The expected event "event1" data does not match.');
286+
$this->assertComponentEmitEvent($testComponent, 'event1')->withData([
287+
'foo' => 'bar',
288+
'foo2' => 'bar2',
277289
]);
278290
}
279291
}

0 commit comments

Comments
 (0)