forked from phly/phly-event-dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorEmittingDispatcherTest.php
73 lines (61 loc) · 2.44 KB
/
ErrorEmittingDispatcherTest.php
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
<?php
/**
* @see https://github.com/phly/phly-event-dispatcher for the canonical source repository
* @copyright Copyright (c) 2019 Matthew Weier O'Phinney (https:/mwop.net)
* @license https://github.com/phly/phly-event-dispatcher/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace PhlyTest\EventDispatcher;
use Phly\EventDispatcher\ErrorEmittingDispatcher;
use Phly\EventDispatcher\ErrorEvent;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use RuntimeException;
class ErrorEmittingDispatcherTest extends TestCase
{
use ProphecyTrait;
use CommonDispatcherTests;
public function setUp(): void
{
$this->provider = $this->prophesize(ListenerProviderInterface::class);
$this->dispatcher = new ErrorEmittingDispatcher($this->provider->reveal());
}
public function getDispatcher() : EventDispatcherInterface
{
return $this->dispatcher;
}
public function getListenerProvider() : ObjectProphecy
{
return $this->provider;
}
public function testDispatchesErrorEventIfAListenerRaisesAnExceptionAndThenReThrows(): void
{
$event = new TestAsset\TestEvent();
$exception = new RuntimeException('TRIGGERED');
$errorRaisingListener = function (TestAsset\TestEvent $event) use ($exception): void {
throw $exception;
};
$errorSpy = (object) ['caught' => 0];
$errorListener = function (ErrorEvent $e) use ($errorSpy, $exception, $event, $errorRaisingListener): void {
TestCase::assertSame($event, $e->getEvent());
TestCase::assertSame($errorRaisingListener, $e->getListener());
TestCase::assertSame($exception, $e->getThrowable());
$errorSpy->caught += 1;
};
$this->provider
->getListenersForEvent($event)
->willReturn([$errorRaisingListener])
->shouldBeCalledTimes(1);
$this->provider
->getListenersForEvent(Argument::type(ErrorEvent::class))
->willReturn([$errorListener])
->shouldBeCalledTimes(1);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('TRIGGERED');
$this->dispatcher->dispatch($event);
}
}