8000 [Bridge/PhpUnit] Add PHPUnit 6 support · symfony/symfony@f6915c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6915c2

Browse files
[Bridge/PhpUnit] Add PHPUnit 6 support
1 parent 61d5353 commit f6915c2

13 files changed

+460
-220
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ script:
100100
- if [[ $deps = high ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --no-progress --ansi; $PHPUNIT --exclude-group tty,benchmark,intl-data'$LEGACY"$REPORT"; fi
101101
- if [[ $deps = low ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --no-progress --ansi --prefer-lowest --prefer-stable; $PHPUNIT --exclude-group tty,benchmark,intl-data'"$REPORT"; fi
102102
# Test the PhpUnit bridge using the original phpunit script
103-
- if [[ $deps = low ]]; then (cd src/Symfony/Bridge/PhpUnit && phpenv global 5.3 && php --version && composer update && phpunit); fi
103+
- if [[ $deps = low ]]; then (cd src/Symfony/Bridge/PhpUnit && wget https://phar.phpunit.de/phpunit-4.8.phar); fi
104+
- if [[ $deps = low ]]; then (cd src/Symfony/Bridge/PhpUnit && phpenv global 5.3 && php --version && composer update && php phpunit-4.8.phar); fi

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public static function register($mode = 0)
4141
return;
4242
}
4343

44+
$UtilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
45+
4446
$getMode = function () use ($mode) {
4547
static $memoizedMode = false;
4648

@@ -67,31 +69,34 @@ public static function register($mode = 0)
6769
'legacy' => array(),
6870
'other' => array(),
6971
);
70-
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode) {
72+
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode, $UtilPrefix) {
7173
$mode = $getMode();
7274
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode) {
73-
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
75+
$ErrorHandler = $UtilPrefix.'ErrorHandler';
76+
77+
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
7478
}
7579

7680
$trace = debug_backtrace(true);
7781
$group = 'other';
7882

7983
$i = count($trace);
80-
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) {
84+
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
8185
// No-op
8286
}
8387

8488
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
8589
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
8690
$method = $trace[$i]['function'];
91+
$Test = $UtilPrefix.'Test';
8792

8893
if (0 !== error_reporting()) {
8994
$group = 'unsilenced';
9095
} elseif (0 === strpos($method, 'testLegacy')
9196
|| 0 === strpos($method, 'provideLegacy')
9297
|| 0 === strpos($method, 'getLegacy')
9398
|| strpos($class, '\Legacy')
94-
|| in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true)
99+
|| in_array('legacy', $Test::getGroups($class, $method), true)
95100
) {
96101
$group = 'legacy';
97102
} else {
@@ -128,7 +133,7 @@ public static function register($mode = 0)
128133

129134
if (null !== $oldErrorHandler) {
130135
restore_error_handler();
131-
if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
136+
if (array($UtilPrefix.'ErrorHandler', 'handleError') === $oldErrorHandler) {
132137
restore_error_handler();
133138
self::register($mode);
134139
}

src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php

Lines changed: 24 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -11,234 +11,50 @@
1111

1212
namespace Symfony\Bridge\PhpUnit;
1313

14-
use Doctrine\Common\Annotations\AnnotationRegistry;
14+
use PHPUnit\Framework\BaseTestListener;
15+
use PHPUnit\Framework\Test;
16+
use PHPUnit\Framework\TestSuite;
17+
use PHPUnit\Framework\Warning;
18+
19+
if (class_exists('PHPUnit_Framework_BaseTestListener')) {
20+
class_alias('Symfony\Bridge\PhpUnit\TextUI\Legacy\SymfonyTestsListener', 'Symfony\Bridge\PhpUnit\SymfonyTestsListener');
21+
22+
return;
23+
}
1524

1625
/**
1726
* Collects and replays skipped tests.
1827
*
1928
* @author Nicolas Grekas <p@tchwork.com>
29+
*
30+
* @final
2031
*/
21-
class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
32+
class SymfonyTestsListener extends BaseTestListener
2233
{
23-
private static $globallyEnabled = false;
24-
private $state = -1;
25-
private $skippedFile = false;
26-
private $wasSkipped = array();
27-
private $isSkipped = array();
28-
private $expectedDeprecations = array();
29-
private $gatheredDeprecations = array();
30-
private $previousErrorHandler;
31-
private $testsWithWarnings;
34+
use SymfonyTestsListenerTrait;
3235

33-
/**
34-
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
35-
*/
36-
public function __construct(array $mockedNamespaces = array())
36+
public function startTestSuite(TestSuite $suite)
3737
{
38-
\PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\DeprecationErrorHandler'] = 1;
39-
\PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\SymfonyTestsListener'] = 1;
40-
41-
$warn = false;
42-
foreach ($mockedNamespaces as $type => $namespaces) {
43-
if (!is_array($namespaces)) {
44-
$namespaces = array($namespaces);
45-
}
46-
if (is_int($type)) {
47-
// @deprecated BC with v2.8 to v3.0
48-
$type = 'time-sensitive';
49-
$warn = true;
50-
}
51-
if ('time-sensitive' === $type) {
52-
foreach ($namespaces as $ns) {
53-
ClockMock::register($ns.'\DummyClass');
54-
}
55-
}
56-
if ('dns-sensitive' === $type) {
57-
foreach ($namespaces as $ns) {
58-
DnsMock::register($ns.'\DummyClass');
59-
}
60-
}
61-
}
62-
if (self::$globallyEnabled) {
63-
$this->state = -2;
64-
} else {
65-
self::$globallyEnabled = true;
66-
if ($warn) {
67-
echo "Clock-mocked namespaces for SymfonyTestsListener need to be nested in a \"time-sensitive\" key. This will be enforced in Symfony 4.0.\n";
68-
}
69-
}
38+
return $this->doStartTestSuite($suite);
7039
}
7140

72-
public function __destruct()
41+
public function addSkippedTest(Test $test, \Exception $e, $time)
7342
{
74-
if (0 < $this->state) {
75-
file_put_contents($this->skippedFile, '<?php return '.var_export($this->isSkipped, true).';');
76-
}
43+
return $this->doAddSkippedTest($test, $e, $time);
7744
}
7845

79-
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
46+
public function startTest(Test $test)
8047
{
81-
$suiteName = $suite->getName();
82-
$this->testsWithWarnings = array();
83-
84-
if (-1 === $this->state) {
85-
echo "Testing $suiteName\n";
86-
$this->state = 0;
87-
88-
if (!class_exists('Doctrine\Common\Annotations\AnnotationRegistry', false) && class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
89-
AnnotationRegistry::registerLoader('class_exists');
90-
}
91-
92-
if ($this->skippedFile = getenv('SYMFONY_PHPUNIT_SKIPPED_TESTS')) {
93-
$this->state = 1;
94-
95-
if (file_exists($this->skippedFile)) {
96-
$this->state = 2;
97-
98-
if (!$this->wasSkipped = require $this->skippedFile) {
99-
echo "All tests already ran successfully.\n";
100-
$suite->setTests(array());
101-
}
102-
}
103-
}
104-
$testSuites = array($suite);
105-
for ($i = 0; isset($testSuites[$i]); ++$i) {
106-
foreach ($testSuites[$i]->tests() as $test) {
107-
if ($test instanceof \PHPUnit_Framework_TestSuite) {
108-
if (!class_exists($test->getName(), false)) {
109-
$testSuites[] = $test;
110-
continue;
111-
}
112-
$groups = \PHPUnit_Util_Test::getGroups($test->getName());
113-
if (in_array('time-sensitive', $groups, true)) {
114-
ClockMock::register($test->getName());
115-
}
116-
if (in_array('dns-sensitive', $groups, true)) {
117-
DnsMock::register($test->getName());
118-
}
119-
}
120-
}
121-
}
122-
} elseif (2 === $this->state) {
123-
$skipped = array();
124-
foreach ($suite->tests() as $test) {
125-
if (!$test instanceof \PHPUnit_Framework_TestCase
126-
|| isset($this->wasSkipped[$suiteName]['*'])
127-
|| isset($this->wasSkipped[$suiteName][$test->getName()])) {
128-
$skipped[] = $test;
129-
}
130-
}
131-
$suite->setTests($skipped);
132-
}
48+
return $this->doStartTest($test);
13349
}
13450

135-
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
51+
public function addWarning(Test $test, Warning $e, $time)
13652
{
137-
if (0 < $this->state) {
138-
if ($test instanceof \PHPUnit_Framework_TestCase) {
139-
$class = get_class($test);
140-
$method = $test->getName();
141-
} else {
142-
$class = $test->getName();
143-
$method = '*';
144-
}
145-
146-
$this->isSkipped[$class][$method] = 1;
147-
}
53+
return $this->doAddWarning($test, $e, $time);
14854
}
14955

150-
public function startTest(\PHPUnit_Framework_Test $test)
56+
public function endTest(Test $test, $time)
15157
{
152-
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
153-
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false));
154-
155-
if (in_array('time-sensitive', $groups, true)) {
156-
ClockMock::register(get_class($test));
157-
ClockMock::withClockMock(true);
158-
}
159-
if (in_array('dns-sensitive', $groups, true)) {
160-
DnsMock::register(get_class($test));
161-
}
162-
163-
$annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($test), $test->getName(false));
164-
165-
if (isset($annotations['class']['expectedDeprecation'])) {
166-
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
167-
}
168-
if (isset($annotations['method']['expectedDeprecation'])) {
169-
if (!in_array('legacy', $groups, true)) {
170-
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0);
171-
}
172-
$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
173-
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
174-
}
175-
}
176-
}
177-
178-
public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time)
179-
{
180-
if ($test instanceof \PHPUnit_Framework_TestCase) {
181-
$this->testsWithWarnings[$test->getName()] = true;
182-
}
183-
}
184-
185-
public function endTest(\PHPUnit_Framework_Test $test, $time)
186-
{
187-
$className = get_class($test);
188-
$classGroups = \PHPUnit_Util_Test::getGroups($className);
189-
$groups = \PHPUnit_Util_Test::getGroups($className, $test->getName(false));
190-
191-
if ($this->expectedDeprecations) {
192-
restore_error_handler();
193-
194-
if (!in_array($test->getStatus(), array(\PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, \PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE, \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR), true)) {
195-
try {
196-
$prefix = "@expectedDeprecation:\n";
197-
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", $this->expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", $this->gatheredDeprecations)."\n");
198-
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
199-
$test->getTestResultObject()->addFailure($test, $e, $time);
200-
}
201-
}
202-
203-
$this->expectedDeprecations = $this->gatheredDeprecations = array();
204-
$this->previousErrorHandler = null;
205-
}
206-
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
207-
if (in_array('time-sensitive', $groups, true)) {
208-
ClockMock::withClockMock(false);
209-
}
210-
if (in_array('dns-sensitive', $groups, true)) {
211-
DnsMock::withMockedHosts(array());
212-
}
213-
}
214-
215-
if ($test instanceof \PHPUnit_Framework_TestCase && 0 === strpos($test->getName(), 'testLegacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $groups, true)) {
216-
$result = $test->getTestResultObject();
217-
218-
if (method_exists($result, 'addWarning')) {
219-
$result->addWarning($test, new \PHPUnit_Framework_Warning('Using the "testLegacy" prefix to mark tests as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time);
220-
}
221-
}
222-
223-
if ($test instanceof \PHPUnit_Framework_TestCase && strpos($className, '\Legacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $classGroups, true)) {
224-
$result = $test->getTestResultObject();
225-
226-
if (method_exists($result, 'addWarning')) {
227-
$result->addWarning($test, new \PHPUnit_Framework_Warning('Using the "Legacy" prefix to mark all tests of a class as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time);
228-
}
229-
}
230-
}
231-
232-
public function handleError($type, $msg, $file, $line, $context)
233-
{
234-
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
235-
$h = $this->previousErrorHandler;
236-
237-
return $h ? $h($type, $msg, $file, $line, $context) : false;
238-
}
239-
if (error_reporting()) {
240-
$msg = 'Unsilenced deprecation: '.$msg;
241-
}
242-
$this->gatheredDeprecations[] = $msg;
58+
return $this->doEndTest($test, $time);
24359
}
24460
}

0 commit comments

Comments
 (0)
0