Skip to content

Commit 2a6206e

Browse files
committed
Extract withData and withDataSubset in dedicated class AssertEmittedEvent
1 parent 197b02c commit 2a6206e

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

src/LiveComponent/src/Test/InteractsWithLiveComponents.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1515
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Symfony\UX\LiveComponent\Test\Util\AssertEmittedEvent;
1617
use Symfony\UX\TwigComponent\ComponentFactory;
1718

1819
/**
@@ -54,42 +55,7 @@ protected function assertComponentEmitEvent(TestLiveComponent $testLiveComponent
5455

5556
$this->assertNotNull($event, \sprintf('The component "%s" did not emit event "%s".', $testLiveComponent->getName(), $expectedEventName));
5657

57-
return new class($this, $event['event'], $event['data']) {
58-
/**
59-
* @param array<string, int|float|string|bool|null> $data
60-
*/
61-
public function __construct(private KernelTestCase $parent, private readonly string $eventName, private readonly array $data)
62-
{
63-
}
64-
65-
/**
66-
* @return self
67-
*/
68-
public function withDataSubset(array $expectedEventData): object
69-
{
70-
foreach ($expectedEventData as $key => $value) {
71-
$this->parent->assertArrayHasKey($key, $this->data, \sprintf('The expected event "%s" data "%s" does not exists', $this->eventName, $key));
72-
$this->parent->assertSame(
73-
$value,
74-
$this->data[$key],
75-
\sprintf(
76-
'The expected event "%s" data "%s" expected "%s" but "%s" given',
77-
$this->eventName,
78-
$key,
79-
$value,
80-
$this->data[$key]
81-
)
82-
);
83-
}
84-
85-
return $this;
86-
}
87-
88-
public function withData(array $expectedEventData): void
89-
{
90-
$this->parent->assertEquals($expectedEventData, $this->data, \sprintf('The expected event "%s" data does not match.', $this->eventName));
91-
}
92-
};
58+
return new AssertEmittedEvent($this, $event['event'], $event['data']);
9359
}
9460

9561
protected function assertComponentNotEmitEvent(TestLiveComponent $testLiveComponent, string $eventName): void
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\LiveComponent\Test\Util;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
16+
final class AssertEmittedEvent
17+
{
18+
/**
19+
* @param array<string, int|float|string|bool|null> $data
20+
*/
21+
public function __construct(private KernelTestCase $parent, private readonly string $eventName, private readonly array $data)
22+
{
23+
}
24+
25+
/**
26+
* @return self
27+
*/
28+
public function withDataSubset(array $expectedEventData): object
29+
{
30+
foreach ($expectedEventData as $key => $value) {
31+
$this->parent->assertArrayHasKey($key, $this->data, \sprintf('The expected event "%s" data "%s" does not exists', $this->eventName, $key));
32+
$this->parent->assertSame(
33+
$value,
34+
$this->data[$key],
35+
\sprintf(
36+
'The expected event "%s" data "%s" expected "%s" but "%s" given',
37+
$this->eventName,
38+
$key,
39+
$value,
40+
$this->data[$key]
41+
)
42+
);
43+
}
44+
45+
return $this;
46+
}
47+
48+
public function withData(array $expectedEventData): void
49+
{
50+
$this->parent->assertEquals($expectedEventData, $this->data, \sprintf('The expected event "%s" data does not match.', $this->eventName));
51+
}
52+
}

0 commit comments

Comments
 (0)