You've already forked php-auth
generated from siteworxpro/Php-Template
Initial commit
This commit is contained in:
177
tests/Events/DispatcherTest.php
Normal file
177
tests/Events/DispatcherTest.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
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');
|
||||
}
|
||||
}
|
||||
47
tests/Events/Listeners/ConnectedTest.php
Normal file
47
tests/Events/Listeners/ConnectedTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Events\Listeners;
|
||||
|
||||
use Illuminate\Database\Events\ConnectionEstablished;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Siteworxpro\App\Events\Listeners\Database\Connected;
|
||||
use Siteworxpro\App\Log\Logger;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ConnectedTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws \ReflectionException
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$inputBuffer = fopen('php://memory', 'r+');
|
||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()->bind(Logger::class, fn() => $logger);
|
||||
}
|
||||
|
||||
public function testHandlesEvent()
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
$connectedEvent = $this->createMock(ConnectionEstablished::class);
|
||||
$listener = new Connected();
|
||||
|
||||
$listener->__invoke($connectedEvent);
|
||||
}
|
||||
|
||||
public function testThrowsException()
|
||||
{
|
||||
$this->expectException(\TypeError::class);
|
||||
$listener = new Connected();
|
||||
$listener->__invoke(new \stdClass());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user