Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre committed Mar 6, 2024
1 parent 8927c51 commit 074bb80
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ 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');
}
unset($data['defer']);
}

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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public static function provideLoadingValues(): iterable
{
return [
['lazy', false],
['eager', true],
['foo', true],
[false, true],
['', true],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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([]);
Expand Down

0 comments on commit 074bb80

Please sign in to comment.