From 30e7004ebd9ac751ab11c17af0f9369aa7d932c1 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Sun, 24 Nov 2019 16:44:46 +0200 Subject: [PATCH 1/2] Symfony5: DispatcherWrapper hides dispatch differences --- src/DispatcherWrapper.php | 27 +++++++++++++++++++++++++++ src/Listener.php | 17 +++++++++-------- src/ResultPrinter/UI.php | 6 +++++- 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/DispatcherWrapper.php diff --git a/src/DispatcherWrapper.php b/src/DispatcherWrapper.php new file mode 100644 index 0000000..2ccf51a --- /dev/null +++ b/src/DispatcherWrapper.php @@ -0,0 +1,27 @@ +dispatch($eventObject, $eventType); + } else { + //Symfony 2,3 or 4 + $dispatcher->dispatch($eventType, $eventObject); + } + + } +} diff --git a/src/Listener.php b/src/Listener.php index 6a127cb..0af5c05 100644 --- a/src/Listener.php +++ b/src/Listener.php @@ -1,17 +1,18 @@ dispatcher->dispatch('suite.start', new SuiteEvent($suite)); + $this->dispatch($this->dispatcher, 'suite.start', new SuiteEvent($suite)); } public function endTestSuite(\PHPUnit\Framework\TestSuite $suite) : void { - $this->dispatcher->dispatch('suite.end', new SuiteEvent($suite)); + $this->dispatch($this->dispatcher, 'suite.end', new SuiteEvent($suite)); } public function startTest(\PHPUnit\Framework\Test $test) : void { - $this->dispatcher->dispatch(Events::TEST_START, new TestEvent($test)); + $this->dispatch($this->dispatcher, Events::TEST_START, new TestEvent($test)); if (!$test instanceof TestInterface) { return; } @@ -119,7 +120,7 @@ public function endTest(\PHPUnit\Framework\Test $test, float $time) : void $this->fire(Events::TEST_AFTER, new TestEvent($test, $time)); } - $this->dispatcher->dispatch(Events::TEST_END, new TestEvent($test, $time)); + $this->dispatch($this->dispatcher, Events::TEST_END, new TestEvent($test, $time)); } protected function fire($event, TestEvent $eventType) @@ -127,9 +128,9 @@ protected function fire($event, TestEvent $eventType) $test = $eventType->getTest(); if ($test instanceof TestInterface) { foreach ($test->getMetadata()->getGroups() as $group) { - $this->dispatcher->dispatch($event . '.' . $group, $eventType); + $this->dispatch($this->dispatcher, $event . '.' . $group, $eventType); } } - $this->dispatcher->dispatch($event, $eventType); + $this->dispatch($this->dispatcher, $event, $eventType); } } diff --git a/src/ResultPrinter/UI.php b/src/ResultPrinter/UI.php index c7c6e92..f628c07 100644 --- a/src/ResultPrinter/UI.php +++ b/src/ResultPrinter/UI.php @@ -3,12 +3,15 @@ use Codeception\Event\FailEvent; use Codeception\Events; +use Codeception\PHPUnit\DispatcherWrapper; use Codeception\Test\Unit; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\EventDispatcher\EventDispatcher; class UI extends \PHPUnit\TextUI\ResultPrinter { + use DispatcherWrapper; + /** * @var EventDispatcher */ @@ -23,7 +26,8 @@ public function __construct(EventDispatcher $dispatcher, $options, $out = null) protected function printDefect(\PHPUnit\Framework\TestFailure $defect, int $count): void { $this->write("\n---------\n"); - $this->dispatcher->dispatch( + $this->dispatch( + $this->dispatcher, Events::TEST_FAIL_PRINT, new FailEvent($defect->failedTest(), null, $defect->thrownException(), $count) ); From f9ca17457c99cf3bf097bbba71d36ac0833fc892 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Sat, 21 Dec 2019 17:41:38 +0200 Subject: [PATCH 2/2] Improved detection of event-dispatcher version Previous detector used class from symfony/event-dispatcher-contracts so it wasn't precise enough. TraceableEventDispatcherInterface was a part of event-dispatcher, so it is more accurate --- src/DispatcherWrapper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/DispatcherWrapper.php b/src/DispatcherWrapper.php index 2ccf51a..5a9b014 100644 --- a/src/DispatcherWrapper.php +++ b/src/DispatcherWrapper.php @@ -4,6 +4,7 @@ use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface; trait DispatcherWrapper { @@ -15,7 +16,8 @@ trait DispatcherWrapper */ protected function dispatch(EventDispatcher $dispatcher, $eventType, Event $eventObject) { - if (class_exists('Symfony\Contracts\EventDispatcher\Event')) { + //TraceableEventDispatcherInterface was introduced in symfony/event-dispatcher 2.5 and removed in 5.0 + if (!interface_exists(TraceableEventDispatcherInterface::class)) { //Symfony 5 $dispatcher->dispatch($eventObject, $eventType); } else {