1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
|
<?php
namespace Illuminate\Tests\Events;
use Error;
use Exception;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class EventsDispatcherTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testBasicEventExecution()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo', function ($foo) {
$_SERVER['__event.test'] = $foo;
});
$response = $d->dispatch('foo', ['bar']);
$this->assertEquals([null], $response);
$this->assertSame('bar', $_SERVER['__event.test']);
// we can still add listeners after the event has fired
$d->listen('foo', function ($foo) {
$_SERVER['__event.test'] .= $foo;
});
$d->dispatch('foo', ['bar']);
$this->assertSame('barbar', $_SERVER['__event.test']);
}
public function testHaltingEventExecution()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo', function ($foo) {
$this->assertTrue(true);
return 'here';
});
$d->listen('foo', function ($foo) {
throw new Exception('should not be called');
});
$response = $d->dispatch('foo', ['bar'], true);
$this->assertSame('here', $response);
$response = $d->until('foo', ['bar']);
$this->assertSame('here', $response);
}
public function testResponseWhenNoListenersAreSet()
{
$d = new Dispatcher;
$response = $d->dispatch('foo');
$this->assertEquals([], $response);
$response = $d->dispatch('foo', [], true);
$this->assertNull($response);
}
public function testReturningFalseStopsPropagation()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo', function ($foo) {
return $foo;
});
$d->listen('foo', function ($foo) {
$_SERVER['__event.test'] = $foo;
return false;
});
$d->listen('foo', function ($foo) {
throw new Exception('should not be called');
});
$response = $d->dispatch('foo', ['bar']);
$this->assertSame('bar', $_SERVER['__event.test']);
$this->assertEquals(['bar'], $response);
}
public function testReturningFalsyValuesContinuesPropagation()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo', function () {
return 0;
});
$d->listen('foo', function () {
return [];
});
$d->listen('foo', function () {
return '';
});
$d->listen('foo', function () {
});
$response = $d->dispatch('foo', ['bar']);
$this->assertEquals([0, [], '', null], $response);
}
public function testContainerResolutionOfEventHandlers()
{
$d = new Dispatcher($container = m::mock(Container::class));
$container->shouldReceive('make')->once()->with(TestEventListener::class)->andReturn(new TestEventListener);
$d->listen('foo', TestEventListener::class.'@onFooEvent');
$response = $d->dispatch('foo', ['foo', 'bar']);
$this->assertEquals(['baz'], $response);
}
public function testContainerResolutionOfEventHandlersWithDefaultMethods()
{
$d = new Dispatcher(new Container);
$d->listen('foo', TestEventListener::class);
$response = $d->dispatch('foo', ['foo', 'bar']);
$this->assertEquals(['baz'], $response);
}
public function testQueuedEventsAreFired()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('update', function ($name) {
$_SERVER['__event.test'] = $name;
});
$d->push('update', ['name' => 'taylor']);
$d->listen('update', function ($name) {
$_SERVER['__event.test'] .= '_'.$name;
});
$this->assertFalse(isset($_SERVER['__event.test']));
$d->flush('update');
$d->listen('update', function ($name) {
$_SERVER['__event.test'] .= $name;
});
$this->assertSame('taylor_taylor', $_SERVER['__event.test']);
}
public function testQueuedEventsCanBeForgotten()
{
$_SERVER['__event.test'] = 'unset';
$d = new Dispatcher;
$d->push('update', ['name' => 'taylor']);
$d->listen('update', function ($name) {
$_SERVER['__event.test'] = $name;
});
$d->forgetPushed();
$d->flush('update');
$this->assertSame('unset', $_SERVER['__event.test']);
}
public function testMultiplePushedEventsWillGetFlushed()
{
$_SERVER['__event.test'] = '';
$d = new Dispatcher;
$d->push('update', ['name' => 'taylor ']);
$d->push('update', ['name' => 'otwell']);
$d->listen('update', function ($name) {
$_SERVER['__event.test'] .= $name;
});
$d->flush('update');
$this->assertSame('taylor otwell', $_SERVER['__event.test']);
}
public function testPushMethodCanAcceptObjectAsPayload()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->push(ExampleEvent::class, $e = new ExampleEvent);
$d->listen(ExampleEvent::class, function ($payload) {
$_SERVER['__event.test'] = $payload;
});
$d->flush(ExampleEvent::class);
$this->assertSame($e, $_SERVER['__event.test']);
}
public function testWildcardListeners()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo.bar', function () {
$_SERVER['__event.test'] = 'regular';
});
$d->listen('foo.*', function () {
$_SERVER['__event.test'] = 'wildcard';
});
$d->listen('bar.*', function () {
$_SERVER['__event.test'] = 'nope';
});
$response = $d->dispatch('foo.bar');
$this->assertEquals([null, null], $response);
$this->assertSame('wildcard', $_SERVER['__event.test']);
}
public function testWildcardListenersWithResponses()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo.bar', function () {
return 'regular';
});
$d->listen('foo.*', function () {
return 'wildcard';
});
$d->listen('bar.*', function () {
return 'nope';
});
$response = $d->dispatch('foo.bar');
$this->assertEquals(['regular', 'wildcard'], $response);
}
public function testWildcardListenersCacheFlushing()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo.*', function () {
$_SERVER['__event.test'] = 'cached_wildcard';
});
$d->dispatch('foo.bar');
$this->assertSame('cached_wildcard', $_SERVER['__event.test']);
$d->listen('foo.*', function () {
$_SERVER['__event.test'] = 'new_wildcard';
});
$d->dispatch('foo.bar');
$this->assertSame('new_wildcard', $_SERVER['__event.test']);
}
public function testListenersCanBeRemoved()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo', function () {
$_SERVER['__event.test'] = 'foo';
});
$d->forget('foo');
$d->dispatch('foo');
$this->assertFalse(isset($_SERVER['__event.test']));
}
public function testWildcardListenersCanBeRemoved()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo.*', function () {
$_SERVER['__event.test'] = 'foo';
});
$d->forget('foo.*');
$d->dispatch('foo.bar');
$this->assertFalse(isset($_SERVER['__event.test']));
}
public function testWildcardCacheIsClearedWhenListenersAreRemoved()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen('foo*', function () {
$_SERVER['__event.test'] = 'foo';
});
$d->dispatch('foo');
$this->assertSame('foo', $_SERVER['__event.test']);
unset($_SERVER['__event.test']);
$d->forget('foo*');
$d->dispatch('foo');
$this->assertFalse(isset($_SERVER['__event.test']));
}
public function testHasWildcardListeners()
{
$d = new Dispatcher;
$d->listen('foo', 'listener1');
$this->assertFalse($d->hasWildcardListeners('foo'));
$d->listen('foo*', 'listener1');
$this->assertTrue($d->hasWildcardListeners('foo'));
}
public function testListenersCanBeFound()
{
$d = new Dispatcher;
$this->assertFalse($d->hasListeners('foo'));
$d->listen('foo', function () {
//
});
$this->assertTrue($d->hasListeners('foo'));
}
public function testWildcardListenersCanBeFound()
{
$d = new Dispatcher;
$this->assertFalse($d->hasListeners('foo.*'));
$d->listen('foo.*', function () {
//
});
$this->assertTrue($d->hasListeners('foo.*'));
$this->assertTrue($d->hasListeners('foo.bar'));
}
public function testEventPassedFirstToWildcards()
{
$d = new Dispatcher;
$d->listen('foo.*', function ($event, $data) {
$this->assertSame('foo.bar', $event);
$this->assertEquals(['first', 'second'], $data);
});
$d->dispatch('foo.bar', ['first', 'second']);
$d = new Dispatcher;
$d->listen('foo.bar', function ($first, $second) {
$this->assertSame('first', $first);
$this->assertSame('second', $second);
});
$d->dispatch('foo.bar', ['first', 'second']);
}
public function testClassesWork()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(ExampleEvent::class, function () {
$_SERVER['__event.test'] = 'baz';
});
$d->dispatch(new ExampleEvent);
$this->assertSame('baz', $_SERVER['__event.test']);
}
public function testClassesWorkWithAnonymousListeners()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(function (ExampleEvent $event) {
$_SERVER['__event.test'] = 'qux';
});
$d->dispatch(new ExampleEvent);
$this->assertSame('qux', $_SERVER['__event.test']);
}
public function testEventClassesArePayload()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(ExampleEvent::class, function ($payload) {
$_SERVER['__event.test'] = $payload;
});
$d->dispatch($e = new ExampleEvent, ['foo']);
$this->assertSame($e, $_SERVER['__event.test']);
}
public function testInterfacesWork()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(SomeEventInterface::class, function () {
$_SERVER['__event.test'] = 'bar';
});
$d->dispatch(new AnotherEvent);
$this->assertSame('bar', $_SERVER['__event.test']);
}
public function testBothClassesAndInterfacesWork()
{
unset($_SERVER['__event.test']);
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen(AnotherEvent::class, function ($p) {
$_SERVER['__event.test'][] = $p;
$_SERVER['__event.test1'] = 'fooo';
});
$d->listen(SomeEventInterface::class, function ($p) {
$_SERVER['__event.test'][] = $p;
$_SERVER['__event.test2'] = 'baar';
});
$d->dispatch($e = new AnotherEvent, ['foo']);
$this->assertSame($e, $_SERVER['__event.test'][0]);
$this->assertSame($e, $_SERVER['__event.test'][1]);
$this->assertSame('fooo', $_SERVER['__event.test1']);
$this->assertSame('baar', $_SERVER['__event.test2']);
unset($_SERVER['__event.test1'], $_SERVER['__event.test2']);
}
public function testNestedEvent()
{
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen('event', function () use ($d) {
$d->listen('event', function () {
$_SERVER['__event.test'][] = 'fired 1';
});
$d->listen('event', function () {
$_SERVER['__event.test'][] = 'fired 2';
});
});
$d->dispatch('event');
$this->assertSame([], $_SERVER['__event.test']);
$d->dispatch('event');
$this->assertEquals(['fired 1', 'fired 2'], $_SERVER['__event.test']);
}
public function testDuplicateListenersWillFire()
{
$d = new Dispatcher;
$d->listen('event', TestListener::class);
$d->listen('event', TestListener::class);
$d->listen('event', TestListener::class.'@handle');
$d->listen('event', TestListener::class.'@handle');
$d->dispatch('event');
$this->assertEquals(4, TestListener::$counter);
TestListener::$counter = 0;
}
public function testGetListeners()
{
$d = new Dispatcher;
$d->listen(ExampleEvent::class, 'Listener1');
$d->listen(ExampleEvent::class, 'Listener2');
$listeners = $d->getListeners(ExampleEvent::class);
$this->assertCount(2, $listeners);
$d->listen(ExampleEvent::class, 'Listener3');
$listeners = $d->getListeners(ExampleEvent::class);
$this->assertCount(3, $listeners);
}
public function testListenersObjectsCreationOrder()
{
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen(TestEvent::class, TestListener1::class);
$d->listen(TestEvent::class, TestListener2::class);
$d->listen(TestEvent::class, TestListener3::class);
// Attaching events does not make any objects.
$this->assertEquals([], $_SERVER['__event.test']);
$d->dispatch(TestEvent::class);
// Dispatching event does not make an object of the event class.
$this->assertEquals([
'cons-1',
'handle-1',
'cons-2',
'handle-2',
'cons-3',
'handle-3',
], $_SERVER['__event.test']);
$d->dispatch(TestEvent::class);
// Event Objects are re-resolved on each dispatch. (No memoization)
$this->assertEquals([
'cons-1',
'handle-1',
'cons-2',
'handle-2',
'cons-3',
'handle-3',
'cons-1',
'handle-1',
'cons-2',
'handle-2',
'cons-3',
'handle-3',
], $_SERVER['__event.test']);
unset($_SERVER['__event.test']);
}
public function test_Listener_object_creation_is_lazy()
{
$d = new Dispatcher;
$d->listen(TestEvent::class, TestListener1::class);
$d->listen(TestEvent::class, TestListener2Falser::class);
$d->listen(TestEvent::class, TestListener3::class);
$d->listen(ExampleEvent::class, TestListener2::class);
$_SERVER['__event.test'] = [];
$d->dispatch(ExampleEvent::class);
// It only resolves relevant listeners not all.
$this->assertEquals(['cons-2', 'handle-2'], $_SERVER['__event.test']);
$_SERVER['__event.test'] = [];
$d->dispatch(TestEvent::class);
$this->assertEquals([
'cons-1',
'handle-1',
'cons-2-falser',
'handle-2-falser',
], $_SERVER['__event.test']);
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(TestEvent::class, TestListener1::class);
$d->listen(TestEvent::class, TestListener2Falser::class);
$d->listen(TestEvent::class, TestListener3::class);
$_SERVER['__event.test'] = [];
$d->dispatch(TestEvent::class, halt: true);
$this->assertEquals([
'cons-1',
'handle-1',
], $_SERVER['__event.test']);
unset($_SERVER['__event.test']);
}
public function testInvokeIsCalled()
{
// Only "handle" is called when both "handle" and "__invoke" exist on listener.
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen('myEvent', TestListenerInvokeyHandler::class);
$d->dispatch('myEvent');
$this->assertEquals(['__construct', 'handle'], $_SERVER['__event.test']);
// "__invoke" is called when there is no handle.
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen('myEvent', TestListenerInvokey::class);
$d->listen('myEvent', TestListenerInvokeyHandler::class);
$d->dispatch('myEvent', 'somePayload');
$this->assertEquals(['__construct', '__invoke_somePayload'], $_SERVER['__event.test']);
// It falls back to __invoke if the referenced method is not found.
$_SERVER['__event.test'] = [];
$d = new Dispatcher;
$d->listen('myEvent', [TestListenerInvokey::class, 'someAbsentMethod']);
$d->dispatch('myEvent', 'somePayload');
$this->assertEquals(['__construct', '__invoke_somePayload'], $_SERVER['__event.test']);
// It throws an "Error" when there is no method to be called.
$d = new Dispatcher;
$d->listen('myEvent', TestListenerLean::class);
$this->expectException(Error::class);
$this->expectExceptionMessage('Call to undefined method '.TestListenerLean::class.'::__invoke()');
$d->dispatch('myEvent', 'somePayload');
unset($_SERVER['__event.test']);
}
}
class TestListenerLean
{
//
}
class TestListenerInvokeyHandler
{
public function __construct()
{
$_SERVER['__event.test'][] = '__construct';
}
public function __invoke()
{
$_SERVER['__event.test'][] = '__invoke';
}
public function handle()
{
$_SERVER['__event.test'][] = 'handle';
}
}
class TestListenerInvokey
{
public function __construct()
{
$_SERVER['__event.test'][] = '__construct';
}
public function __invoke($payload)
{
$_SERVER['__event.test'][] = '__invoke_'.$payload;
return false;
}
}
class ExampleEvent
{
//
}
interface SomeEventInterface
{
//
}
class AnotherEvent implements SomeEventInterface
{
//
}
class TestEventListener
{
public function handle($foo, $bar)
{
return 'baz';
}
public function onFooEvent($foo, $bar)
{
return 'baz';
}
}
class TestListener
{
public static $counter = 0;
public function handle()
{
self::$counter++;
}
}
class TestEvent
{
public function __construct()
{
$_SERVER['__event.test'][] = 'cons-event-1';
}
}
class TestListener1
{
public function __construct()
{
$_SERVER['__event.test'][] = 'cons-1';
}
public function handle()
{
$_SERVER['__event.test'][] = 'handle-1';
return 'resp-1';
}
}
class TestListener2
{
public function __construct()
{
$_SERVER['__event.test'][] = 'cons-2';
}
public function handle()
{
$_SERVER['__event.test'][] = 'handle-2';
return 'resp-2';
}
}
class TestListener2Falser
{
public function __construct()
{
$_SERVER['__event.test'][] = 'cons-2-falser';
}
public function handle()
{
$_SERVER['__event.test'][] = 'handle-2-falser';
return false;
}
}
class TestListener3
{
public function __construct()
{
$_SERVER['__event.test'][] = 'cons-3';
}
public function handle()
{
$_SERVER['__event.test'][] = 'handle-3';
}
}
|