Skip to content

Commit

Permalink
Merge pull request #67 from Codeception/8.0-symfony5
Browse files Browse the repository at this point in the history
Symfony5: DispatcherWrapper hides dispatch differences
  • Loading branch information
Naktibalda authored Nov 24, 2019
2 parents be94d9c + fdaf3f8 commit ee0c1fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
27 changes: 27 additions & 0 deletions src/DispatcherWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Codeception\PHPUnit;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;

trait DispatcherWrapper
{
/**
* Compatibility wrapper for dispatcher change between Symfony 4 and 5
* @param EventDispatcher $dispatcher
* @param string $eventType
* @param Event $eventObject
*/
protected function dispatch(EventDispatcher $dispatcher, $eventType, Event $eventObject)
{
if (class_exists('Symfony\Contracts\EventDispatcher\Event')) {
//Symfony 5
$dispatcher->dispatch($eventObject, $eventType);
} else {
//Symfony 2,3 or 4
$dispatcher->dispatch($eventType, $eventObject);
}

}
}
17 changes: 9 additions & 8 deletions src/Listener.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php
namespace Codeception\PHPUnit;

use Codeception\PHPUnit\DispatcherWrapper;
use Codeception\Event\FailEvent;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\TestInterface;
use Exception;
use PHPUnit\Framework\Test;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Listener implements \PHPUnit\Framework\TestListener
{
use DispatcherWrapper;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcher
*/
Expand Down Expand Up @@ -79,17 +80,17 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Throwable $e, flo

public function startTestSuite(\PHPUnit\Framework\TestSuite $suite) : void
{
$this->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;
}
Expand Down Expand Up @@ -119,17 +120,17 @@ 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)
{
$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);
}
}
6 changes: 5 additions & 1 deletion src/ResultPrinter/UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)
);
Expand Down

0 comments on commit ee0c1fa

Please sign in to comment.