-
-
Notifications
You must be signed in to change notification settings - Fork 364
[LiveComponent] Add assert in test live component #2712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
c994d18
to
a43398b
Compare
bc67a41
to
3f1ce0c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor wordings and types, but otherwise that's fine, thanks :)
foreach ($parameters as $key => $value) { | ||
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will helps by displaying a nice diff between expected vs actual data:
foreach ($parameters as $key => $value) { | |
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key)); | |
} | |
$this->assertEquals(expectedEventData, $event['data'], sprintf('The expected event "%s" data does not match.', $eventName)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep but with the global assertEquals, we must pass all expectedEventDatas.
If the event contains ['a' => 'b', 'c' => 'd'];
And I expect only ['a' => 'b'];
It throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but I think that's the desired behavior. Asserting the whole event data makes more sense to me, but I may be biaised and I understand that sometimes you expect to assert an array subset, but it must be explicit.
I've seen it tons of time on multiple projects, where sometimes you assert everything, or sometimes only a subset, and every time that's a mess because the method-name / PHPUnit's way of working does not help.
I can suggest you this, so everyone would be happy:
// classic way
$this->assertComponentEmitEvent($component, 'foo'); // do not care about event data
$this->assertComponentEmitEventWithData($component, 'foo', []); // assert whole array
$this->assertComponentEmitEventWithDataSubset($component, 'foo', []); // assert subset array
// or a bit exotic
$this->assertComponentEmitEvent($component, 'foo'); // do not care about event data
$this->assertComponentEmitEvent($component, 'foo')->withData([]); // assert whole array
$this->assertComponentEmitEvent($component, 'foo')->withDataSubset([]); // assert subset array
WDYT?
258f0ee
to
d859d84
Compare
Co-authored-by: Hugo Alliaume <[email protected]>
Co-authored-by: Hugo Alliaume <[email protected]>
Co-authored-by: Hugo Alliaume <[email protected]>
32e10b9
to
2c5191f
Compare
This MR simplify liveComponent event testing :
Actually with the Test Helper, we can't simply test whether an event has been dispatched from a component.
This PR add 2 asserts :