Skip to content

Commit c46b35b

Browse files
minor #112 Add compatibility with namespaced PHPUnit (nicolas-grekas)
This PR was merged into the 1.7-dev branch. Discussion ---------- Add compatibility with namespaced PHPUnit Replaces #106, fixes Bug-Debian: https://bugs.debian.org/882930 Commits ------- ab7a3ad Add compatibility with namespaced PHPUnit
2 parents ce7d99d + ab7a3ad commit c46b35b

File tree

14 files changed

+329
-143
lines changed

14 files changed

+329
-143
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clone_folder: c:\projects\polyfill
55

66
cache:
77
- c:\php -> appveyor.yml
8-
- vendor
8+
- vendor -> appveyor.yml
99

1010
init:
1111
- SET PATH=c:\php;%PATH%

src/Util/LegacyTestListener.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Polyfill\Util;
13+
14+
/**
15+
* @author Nicolas Grekas <[email protected]>
16+
*/
17+
class LegacyTestListener extends \PHPUnit_Framework_TestSuite implements \PHPUnit_Framework_TestListener
18+
{
19+
private $suite;
20+
private $trait;
21+
22+
public function __construct(\PHPUnit_Framework_TestSuite $suite = null)
23+
{
24+
if ($suite) {
25+
$this->suite = $suite;
26+
$this->setName($suite->getName().' with polyfills enabled');
27+
$this->addTest($suite);
28+
}
29+
$this->trait = new TestListenerTrait();
30+
}
31+
32+
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
33+
{
34+
$this->trait->startTestSuite($suite);
35+
}
36+
37+
protected function setUp()
38+
{
39+
TestListenerTrait::$enabledPolyfills = $this->suite->getName();
40+
}
41+
42+
protected function tearDown()
43+
{
44+
TestListenerTrait::$enabledPolyfills = false;
45+
}
46+
47+
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
48+
{
49+
$this->trait->addError($test, $e, $time);
50+
}
51+
52+
public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time)
53+
{
54+
}
55+
56+
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
57+
{
58+
$this->trait->addError($test, $e, $time);
59+
}
60+
61+
public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
62+
{
63+
}
64+
65+
public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
66+
{
67+
}
68+
69+
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
70+
{
71+
}
72+
73+
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
74+
{
75+
}
76+
77+
public function startTest(\PHPUnit_Framework_Test $test)
78+
{
79+
}
80+
81+
public function endTest(\PHPUnit_Framework_Test $test, $time)
82+
{
83+
}
84+
}

src/Util/TestListener.php

Lines changed: 64 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -11,144 +11,86 @@
1111

1212
namespace Symfony\Polyfill\Util;
1313

14-
/**
15-
* @author Nicolas Grekas <[email protected]>
16-
*/
17-
class TestListener extends \PHPUnit_Framework_TestSuite implements \PHPUnit_Framework_TestListener
18-
{
19-
public static $enabledPolyfills;
20-
private $suite;
21-
22-
public function __construct(\PHPUnit_Framework_TestSuite $suite = null)
14+
use PHPUnit\Framework\AssertionFailedError;
15+
use PHPUnit\Framework\TestListener as TestListenerInterface;
16+
use PHPUnit\Framework\Test;
17+
use PHPUnit\Framework\TestSuite;
18+
use PHPUnit\Framework\Warning;
19+
20+
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
21+
class_alias('Symfony\Polyfill\Util\LegacyTestListener', 'Symfony\Polyfill\Util\TestListener');
22+
// Using an early return instead of a else does not work when using the PHPUnit phar due to some weird PHP behavior (the class
23+
// gets defined without executing the code before it and so the definition is not properly conditional)
24+
} else {
25+
/**
26+
* @author Nicolas Grekas <[email protected]>
27+
*/
28+
class TestListener extends TestSuite implements TestListenerInterface
2329
{
24-
if ($suite) {
25-
$this->suite = $suite;
26-
$this->setName($suite->getName().' with polyfills enabled');
27-
$this->addTest($suite);
30+
private $suite;
31+
private $trait;
32+
33+
public function __construct(TestSuite $suite = null)
34+
{
35+
if ($suite) {
36+
$this->suite = $suite;
37+
$this->setName($suite->getName().' with polyfills enabled');
38+
$this->addTest($suite);
39+
}
40+
$this->trait = new TestListenerTrait();
2841
}
29-
}
3042

31-
public function startTestSuite(\PHPUnit_Framework_TestSuite $mainSuite)
32-
{
33-
if (null !== self::$enabledPolyfills) {
34-
return;
43+
public function startTestSuite(TestSuite $suite)
44+
{
45+
$this->trait->startTestSuite($suite);
3546
}
36-
self::$enabledPolyfills = false;
3747

38-
foreach ($mainSuite->tests() as $suite) {
39-
$testClass = $suite->getName();
40-
if (!$tests = $suite->tests()) {
41-
continue;
42-
}
43-
if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) {
44-
$mainSuite->addTest(self::warning('Unknown naming convention for '.$testClass));
45-
continue;
46-
}
47-
if (!class_exists($m[1].$m[2])) {
48-
continue;
49-
}
50-
$testedClass = new \ReflectionClass($m[1].$m[2]);
51-
$bootstrap = new \SplFileObject(dirname($testedClass->getFileName()).'/bootstrap.php');
52-
$warnings = array();
53-
$defLine = null;
54-
55-
foreach (new \RegexIterator($bootstrap, '/return p\\\\'.$testedClass->getShortName().'::/') as $defLine) {
56-
if (!preg_match('/^\s*function (?P<name>[^\(]++)(?P<signature>\([^\)]*+\)) \{ (?<return>return p\\\\'.$testedClass->getShortName().'::[^\(]++)(?P<args>\([^\)]*+\)); \}$/', $defLine, $f)) {
57-
$warnings[] = self::warning('Invalid line in bootstrap.php: '.trim($defLine));
58-
continue;
59-
}
60-
$testNamespace = substr($testClass, 0, strrpos($testClass, '\\'));
61-
if (function_exists($testNamespace.'\\'.$f['name'])) {
62-
continue;
63-
}
64-
65-
try {
66-
$r = new \ReflectionFunction($f['name']);
67-
if ($r->isUserDefined()) {
68-
throw new \ReflectionException();
69-
}
70-
if (false !== strpos($f['signature'], '&')) {
71-
$defLine = sprintf('return \\%s%s', $f['name'], $f['args']);
72-
} else {
73-
$defLine = sprintf("return \\call_user_func_array('%s', func_get_args())", $f['name']);
74-
}
75-
} catch (\ReflectionException $e) {
76-
$defLine = sprintf("throw new \PHPUnit_Framework_SkippedTestError('Internal function not found: %s')", $f['name']);
77-
}
78-
79-
eval(<<<EOPHP
80-
namespace {$testNamespace};
81-
82-
use Symfony\Polyfill\Util\TestListener;
83-
use {$testedClass->getNamespaceName()} as p;
84-
85-
function {$f['name']}{$f['signature']}
86-
{
87-
if ('{$testClass}' === TestListener::\$enabledPolyfills) {
88-
{$f['return']}{$f['args']};
89-
}
90-
91-
{$defLine};
92-
}
93-
EOPHP
94-
);
95-
}
96-
if (!$warnings && null === $defLine) {
97-
$warnings[] = new \PHPUnit_Framework_SkippedTestError('No Polyfills found in bootstrap.php for '.$testClass);
98-
} else {
99-
$mainSuite->addTest(new static($suite));
100-
}
101-
}
102-
foreach ($warnings as $w) {
103-
$mainSuite->addTest($w);
48+
protected function setUp()
49+
{
50+
TestListenerTrait::$enabledPolyfills = $this->suite->getName();
10451
}
105-
}
10652

107-
protected function setUp()
108-
{
109-
self::$enabledPolyfills = $this->suite->getName();
110-
}
53+
protected function tearDown()
54+
{
55+
TestListenerTrait::$enabledPolyfills = false;
56+
}
11157

112-
protected function tearDown()
113-
{
114-
self::$enabledPolyfills = false;
115-
}
58+
public function addError(Test $test, \Exception $e, $time)
59+
{
60+
$this->trait->addError($test, $e, $time);
61+
}
11662

117-
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
118-
{
119-
if (false !== self::$enabledPolyfills) {
120-
$r = new \ReflectionProperty('Exception', 'message');
121-
$r->setAccessible(true);
122-
$r->setValue($e, 'Polyfills enabled, '.$r->getValue($e));
63+
public function addWarning(Test $test, Warning $e, $time)
64+
{
12365
}
124-
}
12566

126-
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
127-
{
128-
$this->addError($test, $e, $time);
129-
}
67+
public function addFailure(Test $test, AssertionFailedError $e, $time)
68+
{
69+
$this->trait->addError($test, $e, $time);
70+
}
13071

131-
public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
132-
{
133-
}
72+
public function addIncompleteTest(Test $test, \Exception $e, $time)
73+
{
74+
}
13475

135-
public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
136-
{
137-
}
76+
public function addRiskyTest(Test $test, \Exception $e, $time)
77+
{
78+
}
13879

139-
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
140-
{
141-
}
80+
public function addSkippedTest(Test $test, \Exception $e, $time)
81+
{
82+
}
14283

143-
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
144-
{
145-
}
84+
public function endTestSuite(TestSuite $suite)
85+
{
86+
}
14687

147-
public function startTest(\PHPUnit_Framework_Test $test)
148-
{
149-
}
88+
public function startTest(Test $test)
89+
{
90+
}
15091

151-
public function endTest(\PHPUnit_Framework_Test $test, $time)
152-
{
92+
public function endTest(Test $test, $time)
93+
{
94+
}
15395
}
15496
}

0 commit comments

Comments
 (0)