diff --git a/src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php b/src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php index bfc90d68ad9..c585caa6f25 100644 --- a/src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php +++ b/src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php @@ -33,7 +33,7 @@ public function onPostMount(PostMountEvent $event): void $data = $event->getData(); if (\array_key_exists('defer', $data)) { - 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"'); + 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".'); if ($data['defer']) { $event->addExtraMetadata('loading', 'defer'); } @@ -41,8 +41,15 @@ public function onPostMount(PostMountEvent $event): void } if (\array_key_exists('loading', $data)) { - if (\in_array($data['loading'], ['defer', 'lazy'], true)) { - $event->addExtraMetadata('loading', $data['loading']); + // Ignored values: false / null / '' + if ($loading = $data['loading']) { + if (!\is_scalar($loading)) { + throw new \InvalidArgumentException(sprintf('The "loading" attribute value must be scalar, "%s" passed.', get_debug_type($loading))); + } + if (!\in_array($loading, ['defer', 'lazy'], true)) { + throw new \InvalidArgumentException(sprintf('Invalid "loading" attribute value "%s". Accepted values: "defer" and "lazy".', $loading)); + } + $event->addExtraMetadata('loading', $loading); } unset($data['loading']); } diff --git a/src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php b/src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php index 5175679b4d3..c06b23d9660 100644 --- a/src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php +++ b/src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php @@ -141,8 +141,7 @@ public static function provideLoadingValues(): iterable { return [ ['lazy', false], - ['eager', true], - ['foo', true], + [false, true], ['', true], ]; } diff --git a/src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php b/src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php index 218a54c50e7..eb2dd082084 100644 --- a/src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php +++ b/src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php @@ -44,7 +44,7 @@ public function testLoadingAttributeOverrideDeferAttribute() $subscriber = new DeferLiveComponentSubscriber(); $event = $this->createPostMountEvent(['loading' => 'lazy', 'defer' => true]); - $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.'); + $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".'); $subscriber->onPostMount($event); @@ -62,7 +62,7 @@ public function testDeferAttributeTriggerDeprecation() 'defer' => true, ]); - $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.'); + $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".'); $subscriber->onPostMount($event); } @@ -83,6 +83,31 @@ public function testLoadingAttributesAreRemoved() $this->assertArrayNotHasKey('loading-tag', $event->getData()); } + /** + * @dataProvider provideInvalidLoadingValues + */ + public function testInvalidLoadingValuesThrows(mixed $value) + { + $subscriber = new DeferLiveComponentSubscriber(); + $event = $this->createPostMountEvent([ + 'loading' => $value, + ]); + + $this->expectException(\InvalidArgumentException::class); + + $subscriber->onPostMount($event); + } + + public static function provideInvalidLoadingValues() + { + return [ + ['foo'], + [true], + [['foo']], + ['false'], + ]; + } + private function createPostMountEvent(array $data): PostMountEvent { $componentMetadata = new ComponentMetadata([]);