Skip to content

Commit

Permalink
Fix non scalar value to anonymous component
Browse files Browse the repository at this point in the history
  • Loading branch information
matheo committed Aug 8, 2023
1 parent 02ae73b commit 665638c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
continue;
}

if (!\is_scalar($value) && null !== $value) {
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)));
}
$data[$key] = $value;
}

return new MountedComponent(
Expand Down
21 changes: 21 additions & 0 deletions src/TwigComponent/tests/Fixtures/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\UX\TwigComponent\Tests\Fixtures;

class User
{
public function __construct(
private readonly string $name,
private readonly string $email,
) {}

public function getName(): string
{
return $this->name;
}

public function getEmail(): string
{
return $this->email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<twig:UserCard :user='user'/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class='user-card'>
<p>{{ user.name }}</p>
<p>{{ user.email }}</p>
</div>
11 changes: 11 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\TwigComponent\Tests\Integration;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\TwigComponent\Tests\Fixtures\User;
use Twig\Environment;

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

public function testRenderAnonymousComponentWithNonScalarProps(): void
{
$user = new User('Fabien', '[email protected]');

$output = self::getContainer()->get(Environment::class)->render('anonymous_component_none_scalar_prop.html.twig', ['user' => $user]);

$this->assertStringContainsString('Fabien', $output);
$this->assertStringContainsString('[email protected]', $output);
}

private function renderComponent(string $name, array $data = []): string
{
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [
Expand Down
8 changes: 0 additions & 8 deletions src/TwigComponent/tests/Integration/ComponentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ public function testExceptionThrownIfRequiredMountParameterIsMissingFromPassedDa
$this->createComponent('component_c');
}

public function testExceptionThrownIfUnableToWritePassedDataToPropertyAndIsNotScalar(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('But, the value is not a scalar (it\'s a stdClass)');

$this->createComponent('component_a', ['propB' => 'B', 'service' => new \stdClass()]);
}

public function testStringableObjectCanBePassedToComponent(): void
{
$attributes = $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() {
Expand Down

0 comments on commit 665638c

Please sign in to comment.