Skip to content

Commit dccc6f2

Browse files
authored
Merge pull request microsoft#328 from TysonAndre/update-composer
Support testing with phpunit 7 as well
2 parents a38e03b + b476ac7 commit dccc6f2

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": ">=7.0"
77
},
88
"require-dev": {
9-
"phpunit/phpunit": "^6.4"
9+
"phpunit/phpunit": "^6.4|^7.5.20"
1010
},
1111
"license": "MIT",
1212
"authors": [

tests/CallbackTestListener.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*---------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
use PHPUnit\Framework\Test;
8+
use PHPUnit\Framework\TestListener;
9+
use PHPUnit\Framework\TestListenerDefaultImplementation;
10+
use PHPUnit\Framework\AssertionFailedError;
11+
12+
if (PHP_VERSION_ID >= 70100) {
13+
// PHPUnit 7 requires a return type of void, which is impossible in php 7.0
14+
class CallbackTestListener implements TestListener {
15+
private $cb;
16+
public function __construct(Closure $cb) {
17+
$this->cb = $cb;
18+
}
19+
use TestListenerDefaultImplementation;
20+
// php 7.1 does not support param type widening.
21+
function addFailure(Test $test, AssertionFailedError $e, float $time): void {
22+
($this->cb)($test);
23+
}
24+
}
25+
} else {
26+
class CallbackTestListener implements TestListener {
27+
private $cb;
28+
public function __construct(Closure $cb) {
29+
$this->cb = $cb;
30+
}
31+
use TestListenerDefaultImplementation;
32+
function addFailure(Test $test, AssertionFailedError $e, $time) {
33+
($this->cb)($test);
34+
}
35+
}
36+
}

tests/LexicalGrammarTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PHPUnit\Framework\BaseTestListener;
1212
use PHPUnit\Framework\AssertionFailedError;
1313

14+
require_once __DIR__ . '/CallbackTestListener.php';
15+
1416
class LexicalGrammarTest extends TestCase {
1517
const FILE_PATTERN = __DIR__ . "/cases/lexical/*";
1618
public function run(TestResult $result = null) : TestResult {
@@ -19,14 +21,11 @@ public function run(TestResult $result = null) : TestResult {
1921
exec("git -C " . dirname(self::FILE_PATTERN) . " checkout *.php.tokens");
2022
}
2123

22-
$result->addListener(new class() extends BaseTestListener {
23-
function addFailure(Test $test, AssertionFailedError $e, $time) {
24-
if (isset($test->expectedTokensFile) && isset($test->tokens)) {
25-
file_put_contents($test->expectedTokensFile, str_replace("\r\n", "\n", $test->tokens));
26-
}
27-
parent::addFailure($test, $e, $time);
24+
$result->addListener(new CallbackTestListener(function (Test $test) {
25+
if (isset($test->expectedTokensFile) && isset($test->tokens)) {
26+
file_put_contents($test->expectedTokensFile, str_replace("\r\n", "\n", $test->tokens));
2827
}
29-
});
28+
}));
3029

3130
$result = parent::run($result);
3231
return $result;

tests/ParserGrammarTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@
88
use PHPUnit\Framework\Test;
99
use PHPUnit\Framework\TestCase;
1010
use PHPUnit\Framework\TestResult;
11-
use PHPUnit\Framework\BaseTestListener;
11+
use PHPUnit\Framework\TestListener;
12+
use PHPUnit\Framework\TestListenerDefaultImplementation;
1213
use PHPUnit\Framework\AssertionFailedError;
1314

15+
require_once __DIR__ . '/CallbackTestListener.php';
16+
1417
class ParserGrammarTest extends TestCase {
1518
public function run(TestResult $result = null) : TestResult {
1619
if (!isset($GLOBALS["GIT_CHECKOUT_PARSER"])) {
1720
$GLOBALS["GIT_CHECKOUT_PARSER"] = true;
1821
exec("git -C " . dirname(self::FILE_PATTERN) . " checkout *.php.tree *.php.diag");
1922
}
2023

21-
$result->addListener(new class() extends BaseTestListener {
22-
function addFailure(Test $test, AssertionFailedError $e, $time) {
23-
if (isset($test->expectedTokensFile) && isset($test->tokens)) {
24-
file_put_contents($test->expectedTokensFile, str_replace("\r\n", "\n", $test->tokens));
25-
}
26-
if (isset($test->expectedDiagnosticsFile) && isset($test->diagnostics)) {
27-
file_put_contents($test->expectedDiagnosticsFile, str_replace("\r\n", "\n", $test->diagnostics));
28-
}
29-
parent::addFailure($test, $e, $time);
24+
$result->addListener(new CallbackTestListener(function (Test $test) {
25+
if (isset($test->expectedTokensFile) && isset($test->tokens)) {
26+
file_put_contents($test->expectedTokensFile, str_replace("\r\n", "\n", $test->tokens));
27+
}
28+
if (isset($test->expectedDiagnosticsFile) && isset($test->diagnostics)) {
29+
file_put_contents($test->expectedDiagnosticsFile, str_replace("\r\n", "\n", $test->diagnostics));
3030
}
31-
});
31+
}));
3232

3333
$result = parent::run($result);
3434
return $result;

0 commit comments

Comments
 (0)