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

Commit bc6e12f

Browse files
authored
Merge pull request #72 from Codeception/6.0-symfony5
Symfony 5 support for PHPUnit 5.7
2 parents b608324 + a1ec89b commit bc6e12f

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Diff for: src/DispatcherWrapper.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Codeception\PHPUnit;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\EventDispatcher\EventDispatcher;
7+
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
8+
9+
trait DispatcherWrapper
10+
{
11+
/**
12+
* Compatibility wrapper for dispatcher change between Symfony 4 and 5
13+
* @param EventDispatcher $dispatcher
14+
* @param string $eventType
15+
* @param Event $eventObject
16+
*/
17+
protected function dispatch(EventDispatcher $dispatcher, $eventType, Event $eventObject)
18+
{
19+
//TraceableEventDispatcherInterface was introduced in symfony/event-dispatcher 2.5 and removed in 5.0
20+
if (!interface_exists(TraceableEventDispatcherInterface::class)) {
21+
//Symfony 5
22+
$dispatcher->dispatch($eventObject, $eventType);
23+
} else {
24+
//Symfony 2,3 or 4
25+
$dispatcher->dispatch($eventType, $eventObject);
26+
}
27+
28+
}
29+
}

Diff for: src/Listener.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<?php
22
namespace Codeception\PHPUnit;
33

4+
use Codeception\PHPUnit\DispatcherWrapper;
45
use Codeception\Event\FailEvent;
56
use Codeception\Event\SuiteEvent;
67
use Codeception\Event\TestEvent;
78
use Codeception\Events;
89
use Codeception\TestInterface;
9-
use Exception;
10-
use PHPUnit\Framework\Test;
1110
use Symfony\Component\EventDispatcher\EventDispatcher;
1211

1312
class Listener implements \PHPUnit\Framework\TestListener
1413
{
14+
use DispatcherWrapper;
15+
1516
/**
1617
* @var \Symfony\Component\EventDispatcher\EventDispatcher
1718
*/
@@ -34,7 +35,7 @@ public function __construct(EventDispatcher $dispatcher)
3435
* @param float $time
3536
* @since Method available since Release 4.0.0
3637
*/
37-
public function addRiskyTest(\PHPUnit\Framework\Test $test, Exception $e, $time)
38+
public function addRiskyTest(\PHPUnit\Framework\Test $test, \Exception $e, $time)
3839
{
3940
}
4041

@@ -79,17 +80,17 @@ public function addSkippedTest(\PHPUnit\Framework\Test $test, \Exception $e, $ti
7980

8081
public function startTestSuite(\PHPUnit\Framework\TestSuite $suite)
8182
{
82-
$this->dispatcher->dispatch('suite.start', new SuiteEvent($suite));
83+
$this->dispatch($this->dispatcher, 'suite.start', new SuiteEvent($suite));
8384
}
8485

8586
public function endTestSuite(\PHPUnit\Framework\TestSuite $suite)
8687
{
87-
$this->dispatcher->dispatch('suite.end', new SuiteEvent($suite));
88+
$this->dispatch($this->dispatcher, 'suite.end', new SuiteEvent($suite));
8889
}
8990

9091
public function startTest(\PHPUnit\Framework\Test $test)
9192
{
92-
$this->dispatcher->dispatch(Events::TEST_START, new TestEvent($test));
93+
$this->dispatch($this->dispatcher, Events::TEST_START, new TestEvent($test));
9394
if (!$test instanceof TestInterface) {
9495
return;
9596
}
@@ -119,17 +120,17 @@ public function endTest(\PHPUnit\Framework\Test $test, $time)
119120
$this->fire(Events::TEST_AFTER, new TestEvent($test, $time));
120121
}
121122

122-
$this->dispatcher->dispatch(Events::TEST_END, new TestEvent($test, $time));
123+
$this->dispatch($this->dispatcher, Events::TEST_END, new TestEvent($test, $time));
123124
}
124125

125126
protected function fire($event, TestEvent $eventType)
126127
{
127128
$test = $eventType->getTest();
128129
if ($test instanceof TestInterface) {
129130
foreach ($test->getMetadata()->getGroups() as $group) {
130-
$this->dispatcher->dispatch($event . '.' . $group, $eventType);
131+
$this->dispatch($this->dispatcher, $event . '.' . $group, $eventType);
131132
}
132133
}
133-
$this->dispatcher->dispatch($event, $eventType);
134+
$this->dispatch($this->dispatcher, $event, $eventType);
134135
}
135136
}

Diff for: src/ResultPrinter/UI.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
use Codeception\Event\FailEvent;
55
use Codeception\Events;
6+
use Codeception\PHPUnit\DispatcherWrapper;
67
use Codeception\Test\Unit;
78
use Symfony\Component\Console\Output\OutputInterface;
89
use Symfony\Component\EventDispatcher\EventDispatcher;
910

1011
class UI extends \PHPUnit\TextUI\ResultPrinter
1112
{
13+
use DispatcherWrapper;
14+
1215
/**
1316
* @var EventDispatcher
1417
*/
@@ -23,7 +26,8 @@ public function __construct(EventDispatcher $dispatcher, $options, $out = null)
2326
protected function printDefect(\PHPUnit\Framework\TestFailure $defect, $count)
2427
{
2528
$this->write("\n---------\n");
26-
$this->dispatcher->dispatch(
29+
$this->dispatch(
30+
$this->dispatcher,
2731
Events::TEST_FAIL_PRINT,
2832
new FailEvent($defect->failedTest(), null, $defect->thrownException(), $count)
2933
);

0 commit comments

Comments
 (0)