Skip to content

Commit 38d8590

Browse files
kbondweaverryan
authored andcommitted
[Live] add test helper
1 parent c6adf0a commit 38d8590

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.11.0
44

55
- Support ...spread operator with html syntax (requires Twig 3.7.0 or higher)
6+
- Add `RenderedComponent::crawler()` and `toString()` methods.
67

78
## 2.9.0
89

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"twig/twig": "^2.14.7|^3.0.4"
3535
},
3636
"require-dev": {
37+
"symfony/css-selector": "^5.4|^6.0",
38+
"symfony/dom-crawler": "^5.4|^6.0",
3739
"symfony/framework-bundle": "^5.4|^6.0",
3840
"symfony/phpunit-bridge": "^6.0",
3941
"symfony/stimulus-bundle": "^2.9.1",

doc/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,9 @@ You can test how your component is mounted and rendered using the
10781078
);
10791079

10801080
$this->assertStringContainsString('bar', $rendered);
1081+
1082+
// use the crawler
1083+
$this->assertCount(5, $rendered->crawler('ul li'));
10811084
}
10821085

10831086
public function testEmbeddedComponentRenders(): void

src/Test/RenderedComponent.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\UX\TwigComponent\Test;
1313

14+
use Symfony\Component\DomCrawler\Crawler;
15+
1416
/**
1517
* @author Kevin Bond <[email protected]>
1618
*/
@@ -23,6 +25,20 @@ public function __construct(private string $html)
2325
{
2426
}
2527

28+
public function crawler(): Crawler
29+
{
30+
if (!class_exists(Crawler::class)) {
31+
throw new \LogicException(sprintf('"symfony/dom-crawler" is required to use "%s()" (install with "composer require symfony/dom-crawler").', __METHOD__));
32+
}
33+
34+
return new Crawler($this->html);
35+
}
36+
37+
public function toString(): string
38+
{
39+
return $this->html;
40+
}
41+
2642
public function __toString(): string
2743
{
2844
return $this->html;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
propA: {{ propA }}
22
propB: {{ this.propB }}
33
service: {{ this.service.value }}
4+
5+
<ul>
6+
<li>foo</li>
7+
<li>bar</li>
8+
</ul>

tests/Integration/Test/InteractsWithTwigComponentsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function testCanRenderComponent(string $name): void
5050
$this->assertStringContainsString('propA: prop a value', $rendered);
5151
$this->assertStringContainsString('propB: prop b value', $rendered);
5252
$this->assertStringContainsString('service: service a value', $rendered);
53+
$this->assertCount(2, $rendered->crawler()->filter('ul li'));
5354
}
5455

5556
/**

0 commit comments

Comments
 (0)