You've already forked Php-Template
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m58s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Failing after 3m48s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 4m11s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 4m23s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 4m18s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 3m0s
173 lines
4.9 KiB
PHP
173 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\Tests\Events;
|
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
|
use Siteworxpro\Tests\Unit;
|
|
|
|
class DispatcherTest extends Unit
|
|
{
|
|
|
|
/**
|
|
* @throws \Throwable
|
|
* @throws BindingResolutionException
|
|
*/
|
|
public function testRegistersListeners(): void
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$eventFired = false;
|
|
$dispatcher->listen('TestEvent', function ($event) use (&$eventFired) {
|
|
$this->assertEquals('TestEvent', $event);
|
|
$eventFired = true;
|
|
});
|
|
|
|
$dispatcher->dispatch('TestEvent');
|
|
$this->assertTrue($eventFired, 'The TestEvent listener was not fired.');
|
|
}
|
|
|
|
/**
|
|
* @throws BindingResolutionException
|
|
*/
|
|
public function testPushesEvents()
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$eventsFired = 0;
|
|
$dispatcher->listen('PushedEvent1', function ($event) use (&$eventsFired) {
|
|
$eventsFired++;
|
|
$this->assertEquals('PushedEvent1', $event);
|
|
});
|
|
|
|
$dispatcher->listen('PushedEvent2', function ($event) use (&$eventsFired) {
|
|
$eventsFired++;
|
|
$this->assertEquals('PushedEvent2', $event);
|
|
});
|
|
|
|
$dispatcher->push('PushedEvent1');
|
|
$dispatcher->push('PushedEvent2');
|
|
|
|
unset($dispatcher); // Trigger destructor
|
|
$this->assertEquals(2, $eventsFired);
|
|
}
|
|
|
|
/**
|
|
* @throws BindingResolutionException
|
|
* @throws \Throwable
|
|
*/
|
|
public function testFlushEvent(): void
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$eventFired = false;
|
|
$dispatcher->listen('FlushEvent', function ($event) use (&$eventFired) {
|
|
$this->assertEquals('FlushEvent', $event);
|
|
$eventFired = true;
|
|
});
|
|
|
|
$dispatcher->push('FlushEvent');
|
|
$dispatcher->flush('FlushEvent');
|
|
|
|
$this->assertTrue($eventFired, 'The FlushEvent listener was not fired.');
|
|
}
|
|
|
|
public function testHasListeners(): void
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$this->assertFalse(
|
|
$dispatcher->hasListeners(
|
|
'NonExistentEvent'
|
|
),
|
|
'Expected no listeners for NonExistentEvent.'
|
|
);
|
|
|
|
$dispatcher->listen('ExistingEvent', function () {
|
|
// Listener logic
|
|
});
|
|
|
|
$this->assertTrue(
|
|
$dispatcher->hasListeners(
|
|
'ExistingEvent'
|
|
),
|
|
'Expected listeners for ExistingEvent.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws BindingResolutionException
|
|
* @throws \Throwable
|
|
*/
|
|
public function testForgetEvent(): void
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$eventFired = false;
|
|
$dispatcher->listen('ForgetEvent', function ($event) use (&$eventFired) {
|
|
$this->assertEquals('ForgetEvent', $event);
|
|
$eventFired = true;
|
|
});
|
|
|
|
$dispatcher->push('ForgetEvent');
|
|
$dispatcher->forget('ForgetEvent');
|
|
|
|
unset($dispatcher); // Trigger destructor
|
|
|
|
$this->assertFalse($eventFired, 'The ForgetEvent listener was fired but should have been forgotten.');
|
|
}
|
|
|
|
/**
|
|
* @throws BindingResolutionException
|
|
*/
|
|
public function testForgetPushed()
|
|
{
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$dispatcher->listen('EventToForget', function () {
|
|
$this->fail('The EventToForget listener was fired but should have been forgotten.');
|
|
});
|
|
|
|
$dispatcher->push('EventToForget');
|
|
$dispatcher->forgetPushed();
|
|
|
|
unset($dispatcher); // Trigger destructor
|
|
}
|
|
|
|
public function testToArray(): void
|
|
{
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
|
|
$dispatcher->listen('ArrayEvent', function () {
|
|
// Listener logic
|
|
});
|
|
|
|
$arrayRepresentation = $dispatcher->toArray();
|
|
$this->assertArrayHasKey('ArrayEvent', $arrayRepresentation);
|
|
}
|
|
|
|
/**
|
|
* @throws BindingResolutionException
|
|
* @throws \Throwable
|
|
*/
|
|
public function testSubscriber()
|
|
{
|
|
$subscriber = $this->getMockBuilder('Siteworxpro\App\Events\Subscribers\Subscriber')
|
|
->onlyMethods(['handle'])
|
|
->getMock();
|
|
|
|
$subscriber->expects($this->once())
|
|
->method('handle')
|
|
->with('SubscribedEvent', [])
|
|
->willReturn(null);
|
|
|
|
$dispatcher = $this->getContainer()->make('Siteworxpro\App\Events\Dispatcher');
|
|
$dispatcher->subscribe($subscriber);
|
|
|
|
$dispatcher->dispatch('SubscribedEvent');
|
|
}
|
|
}
|