Skip to content

Commit a8fa0ab

Browse files
committed
add tests
1 parent ae725d4 commit a8fa0ab

File tree

7 files changed

+111
-2
lines changed

7 files changed

+111
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Fixture\Component;
13+
14+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
15+
use Symfony\UX\TwigComponent\HasAttributesTrait;
16+
17+
#[AsTwigComponent('with_attributes')]
18+
class WithAttributes
19+
{
20+
use HasAttributesTrait;
21+
22+
public string $prop;
23+
}

src/TwigComponent/tests/Fixture/Kernel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentA;
2222
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentB;
2323
use Symfony\UX\TwigComponent\Tests\Fixture\Component\ComponentC;
24+
use Symfony\UX\TwigComponent\Tests\Fixture\Component\WithAttributes;
2425
use Symfony\UX\TwigComponent\Tests\Fixture\Service\ServiceA;
2526
use Symfony\UX\TwigComponent\TwigComponentBundle;
2627

@@ -55,6 +56,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
5556
$componentA = $c->register(ComponentA::class)->setAutoconfigured(true)->setAutowired(true);
5657
$componentB = $c->register('component_b', ComponentB::class)->setAutoconfigured(true)->setAutowired(true);
5758
$componentC = $c->register(ComponentC::class)->setAutoconfigured(true)->setAutowired(true);
59+
$withAttributes = $c->register(WithAttributes::class)->setAutoconfigured(true)->setAutowired(true);
5860

5961
$c->register('component_d', ComponentB::class)->addTag('twig.component', [
6062
'key' => 'component_d',
@@ -66,6 +68,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6668
$componentA->addTag('twig.component', ['key' => 'component_a']);
6769
$componentB->addTag('twig.component', ['key' => 'component_b', 'template' => 'components/custom1.html.twig']);
6870
$componentC->addTag('twig.component', ['key' => 'component_c']);
71+
$withAttributes->addTag('twig.component', ['key', 'with_attributes']);
6972
}
7073

7174
if ('missing_key' === $this->environment) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div{{ attributes.merge({ class: 'foo' }) }}>Component Content ({{ prop }})</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
{{ component('component_a', { propA: 'prop a value', propB: 'prop b value' }) }}
2+
{{ component('with_attributes', { prop: 'prop value 1', class: 'bar', style: 'color:red;' }) }}
3+
{{ component('with_attributes', { prop: 'prop value 2', attributes: { class: 'baz' }, style: 'color:red;' }) }}

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public function testCanCustomizeTemplateWithServiceTag(): void
5656

5757
$this->assertStringContainsString('Custom template 2', $output);
5858
}
59+
60+
public function testCanRenderComponentWithAttributes(): void
61+
{
62+
$output = self::getContainer()->get(Environment::class)->render('template_a.html.twig');
63+
64+
$this->assertStringContainsString('Component Content (prop value 1)', $output);
65+
$this->assertStringContainsString('<div class="foo bar" style="color:red;">', $output);
66+
$this->assertStringContainsString('Component Content (prop value 2)', $output);
67+
$this->assertStringContainsString('<div class="foo baz" style="color:red;">', $output);
68+
}
5969
}

src/TwigComponent/tests/Integration/ComponentFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function testCannotGetConfigByNameForNonRegisteredComponent(): void
211211
$factory = self::getContainer()->get('ux.twig_component.component_factory');
212212

213213
$this->expectException(\InvalidArgumentException::class);
214-
$this->expectExceptionMessage('Unknown component "invalid". The registered components are: component_a, component_b, component_c, component_d');
214+
$this->expectExceptionMessage('Unknown component "invalid". The registered components are: component_a');
215215

216216
$factory->configFor('invalid');
217217
}
@@ -222,7 +222,7 @@ public function testCannotGetConfigByClassForNonRegisteredComponent(): void
222222
$factory = self::getContainer()->get('ux.twig_component.component_factory');
223223

224224
$this->expectException(\InvalidArgumentException::class);
225-
$this->expectExceptionMessage('Unknown component class "Symfony\UX\TwigComponent\Tests\Integration\ComponentFactoryTest". The registered components are: component_a, component_b, component_c, component_d');
225+
$this->expectExceptionMessage('Unknown component class "Symfony\UX\TwigComponent\Tests\Integration\ComponentFactoryTest". The registered components are: component_a');
226226

227227
$factory->configFor(self::class);
228228
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Unit;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\TwigComponent\ComponentAttributes;
16+
17+
/**
18+
* @author Kevin Bond <[email protected]>
19+
*/
20+
final class ComponentAttributesTest extends TestCase
21+
{
22+
public function testCanConvertToString(): void
23+
{
24+
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);
25+
26+
$this->assertSame(' class="foo" style="color:black;"', (string) $attributes);
27+
}
28+
29+
public function testCanSetDefaults(): void
30+
{
31+
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);
32+
33+
$this->assertSame(
34+
['class' => 'foo', 'style' => 'color:black;'],
35+
$attributes->default('class', 'bar')->all()
36+
);
37+
$this->assertSame(
38+
['class' => 'foo', 'style' => 'color:black;', 'id' => '1'],
39+
$attributes->defaults(['id' => '1'])->all()
40+
);
41+
}
42+
43+
public function testCanMerge(): void
44+
{
45+
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);
46+
47+
$this->assertSame(
48+
['class' => 'bar foo', 'style' => 'font-size: 10; color:black;'],
49+
$attributes->merge(['class' => 'bar', 'style' => 'font-size: 10;'])->all()
50+
);
51+
$this->assertSame(
52+
' class="bar foo" style="font-size: 10; color:black;"',
53+
(string) $attributes->merge(['class' => 'bar', 'style' => 'font-size: 10;'])
54+
);
55+
}
56+
57+
public function testCanGetOnly(): void
58+
{
59+
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);
60+
61+
$this->assertSame(['class' => 'foo'], $attributes->only('class')->all());
62+
}
63+
64+
public function testCanGetWithout(): void
65+
{
66+
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);
67+
68+
$this->assertSame(['class' => 'foo'], $attributes->without('style')->all());
69+
}
70+
}

0 commit comments

Comments
 (0)