Skip to content

Commit eb5e0a9

Browse files
committed
add/refactor tests
1 parent 4b1cf7c commit eb5e0a9

File tree

6 files changed

+58
-19
lines changed

6 files changed

+58
-19
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\UX\LiveComponent\Tests\Fixture\Component;
4+
5+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
6+
use Symfony\UX\LiveComponent\DefaultActionTrait;
7+
use Symfony\UX\TwigComponent\HasAttributesTrait;
8+
9+
/**
10+
* @author Kevin Bond <[email protected]>
11+
*/
12+
#[AsLiveComponent('with_attributes')]
13+
final class ComponentWithAttributes
14+
{
15+
use DefaultActionTrait;
16+
use HasAttributesTrait;
17+
}

src/LiveComponent/tests/Fixture/Kernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
2828
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
2929
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6;
30+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\ComponentWithAttributes;
3031
use Symfony\UX\TwigComponent\TwigComponentBundle;
3132
use Twig\Environment;
3233

@@ -67,13 +68,15 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6768
$componentB = $c->register(Component2::class)->setAutoconfigured(true)->setAutowired(true);
6869
$componentC = $c->register(Component3::class)->setAutoconfigured(true)->setAutowired(true);
6970
$componentF = $c->register(Component6::class)->setAutoconfigured(true)->setAutowired(true);
71+
$withAttributes = $c->register(ComponentWithAttributes::class)->setAutoconfigured(true)->setAutowired(true);
7072

7173
if (self::VERSION_ID < 50300) {
7274
// add tag manually
7375
$componentA->addTag('twig.component', ['key' => 'component1', 'live' => true])->addTag('controller.service_arguments');
7476
$componentB->addTag('twig.component', ['key' => 'component2', 'live' => true, 'default_action' => 'defaultAction'])->addTag('controller.service_arguments');
7577
$componentC->addTag('twig.component', ['key' => 'component3', 'live' => true])->addTag('controller.service_arguments');
7678
$componentF->addTag('twig.component', ['key' => 'component6', 'live' => true])->addTag('controller.service_arguments');
79+
$componentF->addTag('twig.component', ['key' => 'with_attributes', 'live' => true])->addTag('controller.service_arguments');
7780
}
7881

7982
$sessionConfig = self::VERSION_ID < 50300 ? ['storage_id' => 'session.storage.mock_file'] : ['storage_factory_id' => 'session.storage.factory.mock_file'];

src/LiveComponent/tests/Fixture/templates/components/component1.html.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<div
2-
{{ init_live_component() }}
3-
>
1+
<div{{ attributes }}>
42
Prop1: {{ this.prop1.id }}
53
Prop2: {{ this.prop2|date('Y-m-d g:i') }}
64
Prop3: {{ this.prop3 }}

src/LiveComponent/tests/Fixture/templates/components/component6.html.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<div
2-
{{ init_live_component(this) }}
3-
>
1+
<div{{ attributes }}>
42
Arg1: {{ this.called ? this.arg1 : 'not provided' }}
53
Arg2: {{ this.called ? this.arg2 : 'not provided' }}
64
Arg3: {{ this.called ? this.arg3 : 'not provided' }}

src/LiveComponent/tests/Functional/Twig/LiveComponentExtensionTest.php renamed to src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\UX\LiveComponent\Tests\Functional\Twig;
12+
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Zenstruck\Browser\Test\HasBrowser;
1616

1717
/**
1818
* @author Kevin Bond <[email protected]>
1919
*/
20-
final class LiveComponentExtensionTest extends KernelTestCase
20+
final class AddLiveAttributesSubscriberTest extends KernelTestCase
2121
{
2222
use HasBrowser;
2323

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
1919
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
2020
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
21+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\ComponentWithAttributes;
2122
use Symfony\UX\LiveComponent\Tests\Fixture\Entity\Entity1;
22-
use Symfony\UX\TwigComponent\ComponentAttributes;
2323
use Symfony\UX\TwigComponent\ComponentFactory;
24-
use Symfony\UX\TwigComponent\HasAttributesTrait;
2524
use function Zenstruck\Foundry\create;
2625
use Zenstruck\Foundry\Test\Factories;
2726
use Zenstruck\Foundry\Test\ResetDatabase;
@@ -273,22 +272,46 @@ public function testCanDehydrateAndHydrateComponentsWithAttributes(): void
273272
/** @var LiveComponentHydrator $hydrator */
274273
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
275274

276-
$component = new class() {
277-
use HasAttributesTrait;
278-
};
279-
$instance = clone $component;
280-
$instance->attributes = new ComponentAttributes($attributes = ['class' => 'foo']);
275+
/** @var ComponentFactory $factory */
276+
$factory = self::getContainer()->get('ux.twig_component.component_factory');
281277

282-
$this->assertSame($attributes, $instance->attributes->all());
278+
/** @var ComponentWithAttributes $component */
279+
$component = $factory->create('with_attributes', $attributes = ['class' => 'foo']);
283280

284-
$dehydrated = $hydrator->dehydrate($instance);
281+
$this->assertSame($attributes, $component->attributes->all());
282+
283+
$dehydrated = $hydrator->dehydrate($component);
285284

286285
$this->assertArrayHasKey('attributes', $dehydrated);
287286
$this->assertSame($attributes, $dehydrated['attributes']);
288-
$this->assertFalse(isset($component->prop));
289287

290-
$hydrator->hydrate($component, $dehydrated);
288+
$hydrator->hydrate($component = $factory->get('with_attributes'), $dehydrated);
291289

292290
$this->assertSame($attributes, $component->attributes->all());
293291
}
292+
293+
public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void
294+
{
295+
/** @var LiveComponentHydrator $hydrator */
296+
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
297+
298+
/** @var ComponentFactory $factory */
299+
$factory = self::getContainer()->get('ux.twig_component.component_factory');
300+
301+
/** @var ComponentWithAttributes $component */
302+
$component = $factory->create('with_attributes');
303+
304+
$this->assertSame([], $component->attributes->all());
305+
306+
$dehydrated = $hydrator->dehydrate($component);
307+
308+
$this->assertArrayHasKey('attributes', $dehydrated);
309+
$this->assertSame([], $dehydrated['attributes']);
310+
311+
$component = $factory->get('with_attributes');
312+
313+
$hydrator->hydrate($component, $dehydrated);
314+
315+
$this->assertSame([], $component->attributes->all());
316+
}
294317
}

0 commit comments

Comments
 (0)