Skip to content

Commit 2d9f300

Browse files
author
matheo
committed
Fix non scalar value to anonymous component
1 parent 25f0ab8 commit 2d9f300

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

src/TwigComponent/src/ComponentFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
102102
continue;
103103
}
104104

105-
if (!\is_scalar($value) && null !== $value) {
106-
throw new \LogicException(sprintf('A "%s" prop was passed when creating the "%s" component. No matching %s property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $componentMetadata->getName(), $key, get_debug_type($value)));
107-
}
105+
$data[$key] = $value;
108106
}
109107

110108
return new MountedComponent(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\UX\TwigComponent\Tests\Fixtures;
4+
5+
class User
6+
{
7+
public function __construct(
8+
private readonly string $name,
9+
private readonly string $email,
10+
) {}
11+
12+
public function getName(): string
13+
{
14+
return $this->name;
15+
}
16+
17+
public function getEmail(): string
18+
{
19+
return $this->email;
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<twig:UserCard :user='user'/>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class='user-card'>
2+
<p>{{ user.name }}</p>
3+
<p>{{ user.email }}</p>
4+
</div>

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\TwigComponent\Tests\Integration;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\TwigComponent\Tests\Fixtures\User;
1516
use Twig\Environment;
1617

1718
/**
@@ -182,6 +183,16 @@ public function testRenderAnonymousComponentInNestedDirectory(): void
182183
$this->assertStringContainsString('class="primary"', $output);
183184
}
184185

186+
public function testRenderAnonymousComponentWithNonScalarProps(): void
187+
{
188+
$user = new User('Fabien', '[email protected]');
189+
190+
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_none_scalar_prop.html.twig', ['user' => $user]);
191+
192+
$this->assertStringContainsString('Fabien', $output);
193+
$this->assertStringContainsString('[email protected]', $output);
194+
}
195+
185196
private function renderComponent(string $name, array $data = []): string
186197
{
187198
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [

src/TwigComponent/tests/Integration/ComponentFactoryTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ public function testExceptionThrownIfRequiredMountParameterIsMissingFromPassedDa
9191
$this->createComponent('component_c');
9292
}
9393

94-
public function testExceptionThrownIfUnableToWritePassedDataToPropertyAndIsNotScalar(): void
95-
{
96-
$this->expectException(\LogicException::class);
97-
$this->expectExceptionMessage('But, the value is not a scalar (it\'s a stdClass)');
98-
99-
$this->createComponent('component_a', ['propB' => 'B', 'service' => new \stdClass()]);
100-
}
101-
10294
public function testStringableObjectCanBePassedToComponent(): void
10395
{
10496
$attributes = $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() {

0 commit comments

Comments
 (0)