|
11 | 11 |
|
12 | 12 | namespace Symfony\UX\LiveComponent\Tests\Functional\Test;
|
13 | 13 |
|
| 14 | +use PHPUnit\Framework\AssertionFailedError; |
14 | 15 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
15 | 16 | use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
16 | 17 | use Symfony\Component\Security\Core\User\InMemoryUser;
|
@@ -217,4 +218,75 @@ public function testSetLocaleRenderLocalizedComponent(): void
|
217 | 218 | $testComponent->setRouteLocale('de');
|
218 | 219 | $this->assertStringContainsString('Locale: de', $testComponent->render());
|
219 | 220 | }
|
| 221 | + |
| 222 | + public function testComponentEmitsExpectedEventData(): void |
| 223 | + { |
| 224 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 225 | + |
| 226 | + $testComponent->call('actionThatEmits'); |
| 227 | + |
| 228 | + $this->assertComponentEmitEvent($testComponent, 'event1')->withData([ |
| 229 | + 'foo' => 'bar', |
| 230 | + 'bar' => 'foo', |
| 231 | + ]); |
| 232 | + } |
| 233 | + |
| 234 | + public function testComponentEmitsExpectedEventDataFails(): void |
| 235 | + { |
| 236 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 237 | + |
| 238 | + $testComponent->call('actionThatEmits'); |
| 239 | + |
| 240 | + $this->expectException(AssertionFailedError::class); |
| 241 | + $this->expectExceptionMessage('The expected event "event1" data does not match'); |
| 242 | + $this->assertComponentEmitEvent($testComponent, 'event1')->withData([ |
| 243 | + 'foo' => 'bar', |
| 244 | + ]); |
| 245 | + } |
| 246 | + |
| 247 | + public function testComponentEmitsExpectedPartialEventData(): void |
| 248 | + { |
| 249 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 250 | + |
| 251 | + $testComponent->call('actionThatEmits'); |
| 252 | + |
| 253 | + $this->assertComponentEmitEvent($testComponent, 'event1') |
| 254 | + ->withDataSubset(['foo' => 'bar']) |
| 255 | + ->withDataSubset(['bar' => 'foo']) |
| 256 | + ; |
| 257 | + } |
| 258 | + |
| 259 | + public function testComponentDoesNotEmitUnexpectedEvent(): void |
| 260 | + { |
| 261 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 262 | + |
| 263 | + $testComponent->call('actionThatEmits'); |
| 264 | + |
| 265 | + $this->assertComponentNotEmitEvent($testComponent, 'event2'); |
| 266 | + } |
| 267 | + |
| 268 | + public function testComponentDoesNotEmitUnexpectedEventFails(): void |
| 269 | + { |
| 270 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 271 | + |
| 272 | + $testComponent->call('actionThatEmits'); |
| 273 | + |
| 274 | + $this->expectException(AssertionFailedError::class); |
| 275 | + $this->expectExceptionMessage('The component "component_with_emit" did not emit event "event1".'); |
| 276 | + $this->assertComponentNotEmitEvent($testComponent, 'event1'); |
| 277 | + } |
| 278 | + |
| 279 | + public function testComponentEmitsEventWithIncorrectDataFails(): void |
| 280 | + { |
| 281 | + $testComponent = $this->createLiveComponent('component_with_emit'); |
| 282 | + |
| 283 | + $testComponent->call('actionThatEmits'); |
| 284 | + |
| 285 | + $this->expectException(AssertionFailedError::class); |
| 286 | + $this->expectExceptionMessage('The expected event "event1" data does not match.'); |
| 287 | + $this->assertComponentEmitEvent($testComponent, 'event1')->withData([ |
| 288 | + 'foo' => 'bar', |
| 289 | + 'foo2' => 'bar2', |
| 290 | + ]); |
| 291 | + } |
220 | 292 | }
|
0 commit comments