Skip to content

Commit 074bb80

Browse files
committed
prettier
1 parent 8927c51 commit 074bb80

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ public function onPostMount(PostMountEvent $event): void
3333
$data = $event->getData();
3434

3535
if (\array_key_exists('defer', $data)) {
36-
trigger_deprecation('symfony/ux-live-component', '2.17', 'The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead set to the value "defer"');
36+
trigger_deprecation('symfony/ux-live-component', '2.17', 'The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead set to the value "defer".');
3737
if ($data['defer']) {
3838
$event->addExtraMetadata('loading', 'defer');
3939
}
4040
unset($data['defer']);
4141
}
4242

4343
if (\array_key_exists('loading', $data)) {
44-
if (\in_array($data['loading'], ['defer', 'lazy'], true)) {
45-
$event->addExtraMetadata('loading', $data['loading']);
44+
// Ignored values: false / null / ''
45+
if ($loading = $data['loading']) {
46+
if (!\is_scalar($loading)) {
47+
throw new \InvalidArgumentException(sprintf('The "loading" attribute value must be scalar, "%s" passed.', get_debug_type($loading)));
48+
}
49+
if (!\in_array($loading, ['defer', 'lazy'], true)) {
50+
throw new \InvalidArgumentException(sprintf('Invalid "loading" attribute value "%s". Accepted values: "defer" and "lazy".', $loading));
51+
}
52+
$event->addExtraMetadata('loading', $loading);
4653
}
4754
unset($data['loading']);
4855
}

src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ public static function provideLoadingValues(): iterable
141141
{
142142
return [
143143
['lazy', false],
144-
['eager', true],
145-
['foo', true],
144+
[false, true],
146145
['', true],
147146
];
148147
}

src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testLoadingAttributeOverrideDeferAttribute()
4444
$subscriber = new DeferLiveComponentSubscriber();
4545
$event = $this->createPostMountEvent(['loading' => 'lazy', 'defer' => true]);
4646

47-
$this->expectDeprecation('Since symfony/ux-live-component 2.17: The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead.');
47+
$this->expectDeprecation('Since symfony/ux-live-component 2.17: The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead set to the value "defer".');
4848

4949
$subscriber->onPostMount($event);
5050

@@ -62,7 +62,7 @@ public function testDeferAttributeTriggerDeprecation()
6262
'defer' => true,
6363
]);
6464

65-
$this->expectDeprecation('Since symfony/ux-live-component 2.17: The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead.');
65+
$this->expectDeprecation('Since symfony/ux-live-component 2.17: The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead set to the value "defer".');
6666

6767
$subscriber->onPostMount($event);
6868
}
@@ -83,6 +83,31 @@ public function testLoadingAttributesAreRemoved()
8383
$this->assertArrayNotHasKey('loading-tag', $event->getData());
8484
}
8585

86+
/**
87+
* @dataProvider provideInvalidLoadingValues
88+
*/
89+
public function testInvalidLoadingValuesThrows(mixed $value)
90+
{
91+
$subscriber = new DeferLiveComponentSubscriber();
92+
$event = $this->createPostMountEvent([
93+
'loading' => $value,
94+
]);
95+
96+
$this->expectException(\InvalidArgumentException::class);
97+
98+
$subscriber->onPostMount($event);
99+
}
100+
101+
public static function provideInvalidLoadingValues()
102+
{
103+
return [
104+
['foo'],
105+
[true],
106+
[['foo']],
107+
['false'],
108+
];
109+
}
110+
86111
private function createPostMountEvent(array $data): PostMountEvent
87112
{
88113
$componentMetadata = new ComponentMetadata([]);

0 commit comments

Comments
 (0)