Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Symfony 5 support for PHPUnit 7 branch #70

Merged
merged 2 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/DispatcherWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Codeception\PHPUnit;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;

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)
{
//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 {
//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