Skip to content

Commit

Permalink
Add a TestEventService
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Dec 3, 2024
1 parent 38c064f commit ed719bc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"silverstripe/versioned": "^1.13 || ^2.0",
"psr/event-dispatcher": "^1.0",
"psr/event-dispatcher-implementation": "^1.0",
"archipro/revolt-event-dispatcher": "^0.0.0"
"archipro/revolt-event-dispatcher": "^0.1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
28 changes: 28 additions & 0 deletions src/Service/TestEventService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Service;

use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\ListenerProvider;
use SilverStripe\Core\Injector\Injector;

/**
* Extension of the AsyncEventDispatcher for testing purposes.
*
* This service will throw exceptions when errors occur to make it easier to debug issues.
*/
class TestEventService extends EventService
{
public function __construct() {
$listenerProvider = Injector::inst()->get(ListenerProvider::class);
$dispatcher = new AsyncEventDispatcher($listenerProvider, null, AsyncEventDispatcher::THROW_ON_ERROR);
parent::__construct($dispatcher, $listenerProvider);
}

public static function bootstrap(): self
{
$service = new self();
Injector::inst()->registerService($service, AsyncEventDispatcher::class);
return $service;
}
}
37 changes: 37 additions & 0 deletions tests/php/Service/TestEventServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Tests\Service;

use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;
use ArchiPro\Silverstripe\EventDispatcher\Service\TestEventService;
use ArchiPro\Silverstripe\EventDispatcher\Tests\TestListenerLoader;
use Exception;
use Revolt\EventLoop;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;

class TestEventServiceTest extends SapphireTest
{
private EventService $service;

protected function setUp(): void
{
$this->service = TestEventService::bootstrap();
}

public function testEventDispatch(): void
{
// Create test event
$event = new class () {};

// Add test listener
$this->service->addListener(get_class($event), function ($event) {
throw new Exception('Test exception');
});

// Dispatch event
$result = $this->service->dispatch($event);

EventLoop::run();
}
}

0 comments on commit ed719bc

Please sign in to comment.