Skip to content

Commit e3a9104

Browse files
committed
Adding test and clarifying lofic
1 parent 532c25d commit e3a9104

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/LiveComponent/src/Util/LiveFormUtility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function removePathsNotInData(array $paths, array $data): array
4545

4646
public static function doesFormContainAnyErrors(FormView $formView): bool
4747
{
48-
if ($formView->vars['errors'] ?? null && \count($formView->vars['errors']) > 0) {
48+
if (($formView->vars['errors'] ?? null) && \count($formView->vars['errors']) > 0) {
4949
return true;
5050
}
5151

src/LiveComponent/tests/Unit/Util/LiveFormUtilityTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Unit\Util;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Form\FormView;
1516
use Symfony\UX\LiveComponent\Util\LiveFormUtility;
1617

1718
final class LiveFormUtilityTest extends TestCase
@@ -55,4 +56,50 @@ public function getPathsTests(): iterable
5556
['name', 'posts.1.title'],
5657
];
5758
}
59+
60+
/**
61+
* @dataProvider provideFormContainsAnyErrorsTests
62+
*/
63+
public function testDoesFormContainAnyErrors(FormView $formView, bool $expected)
64+
{
65+
$this->assertSame($expected, LiveFormUtility::doesFormContainAnyErrors($formView));
66+
}
67+
68+
public function provideFormContainsAnyErrorsTests()
69+
{
70+
yield 'no_errors_key' => [
71+
new FormView(),
72+
false,
73+
];
74+
75+
$view = new FormView();
76+
$view->vars['errors'] = [];
77+
yield 'error_is_empty' => [
78+
$view,
79+
false,
80+
];
81+
82+
$view2 = new FormView();
83+
$view2->vars['errors'] = ['error'];
84+
yield 'error_is_not_empty' => [
85+
$view2,
86+
true,
87+
];
88+
89+
$view3 = new FormView();
90+
$view3->children[] = new FormView();
91+
yield 'error_is_empty_in_children' => [
92+
$view3,
93+
false,
94+
];
95+
96+
$view4 = new FormView();
97+
$childView = new FormView();
98+
$childView->vars['errors'] = ['error'];
99+
$view4->children[] = $childView;
100+
yield 'error_is_not_empty_in_children' => [
101+
$view4,
102+
true,
103+
];
104+
}
58105
}

0 commit comments

Comments
 (0)