-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxime Rainville
committed
Dec 3, 2024
1 parent
38c064f
commit ed719bc
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |