Skip to content

Commit

Permalink
fixed compatibility with PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2020
1 parent 15ed6f3 commit f19ff4c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/CodeCoverage/PhpParser.php
Expand Up @@ -76,7 +76,8 @@ public function parse(string $code): \stdClass

switch (is_array($token) ? $token[0] : $token) {
case T_NAMESPACE:
$namespace = ltrim(self::fetch($tokens, [T_STRING, T_NS_SEPARATOR]) . '\\', '\\');
$namespace = self::fetch($tokens, [T_STRING, PHP_VERSION_ID < 80000 ? T_NS_SEPARATOR : T_NAME_QUALIFIED]);
$namespace = ltrim($namespace . '\\', '\\');
break;

case T_CLASS:
Expand Down
48 changes: 24 additions & 24 deletions tests/Framework/Assert.error.phpt
Expand Up @@ -8,37 +8,37 @@ require __DIR__ . '/../bootstrap.php';


Assert::error(function () {
$a++;
$a = &pi();
}, E_NOTICE);

Assert::error(function () {
$a++;
$a = &pi();
}, 'E_NOTICE');

Assert::error(function () {
$a++;
}, E_NOTICE, 'Undefined variable: a');
$a = &pi();
}, E_NOTICE, 'Only variables should be assigned by reference');

Assert::error(function () {
$a++;
@$x++;
$b++;
$a = &pi();
@$a++;
$a = &pi();
}, [
[E_NOTICE, 'Undefined variable: a'],
[E_NOTICE, 'Undefined variable: b'],
[E_NOTICE, 'Only variables should be assigned by reference'],
[E_NOTICE, 'Only variables should be assigned by reference'],
]);

Assert::error(function () {
$a++;
$b++;
$a = &pi();
$a = &pi();
}, [
[E_NOTICE],
[E_NOTICE],
]);

Assert::error(function () {
$a++;
$b++;
$a = &pi();
$a = &pi();
}, [E_NOTICE, E_NOTICE]
);

Expand All @@ -49,29 +49,29 @@ Assert::exception(function () {

Assert::exception(function () {
Assert::error(function () {
$a++;
$a = &pi();
}, E_WARNING);
}, Tester\AssertException::class, 'E_WARNING was expected, but E_NOTICE (Undefined variable: a) was generated in file %a% on line %d%');
}, Tester\AssertException::class, 'E_WARNING was expected, but E_NOTICE (Only variables should be assigned by reference) was generated in file %a% on line %d%');

Assert::exception(function () {
Assert::error(function () {
$a++;
$a = &pi();
}, E_NOTICE, 'Abc');
}, Tester\AssertException::class, "E_NOTICE with a message matching 'Abc' was expected but got 'Undefined variable: a'");
}, Tester\AssertException::class, "E_NOTICE with a message matching 'Abc' was expected but got 'Only variables should be assigned by reference'");

Assert::exception(function () {
Assert::error(function () {
$a++;
$b++;
}, E_NOTICE, 'Undefined variable: a');
}, Tester\AssertException::class, 'Generated more errors than expected: E_NOTICE (Undefined variable: b) was generated in file %a% on line %d%');
$a = &pi();
$a = &pi();
}, E_NOTICE, 'Only variables should be assigned by reference');
}, Tester\AssertException::class, 'Generated more errors than expected: E_NOTICE (Only variables should be assigned by reference) was generated in file %a% on line %d%');

Assert::exception(function () {
Assert::error(function () {
$a++;
$a = &pi();
}, [
[E_NOTICE, 'Undefined variable: a'],
[E_NOTICE, 'Undefined variable: b'],
[E_NOTICE, 'Only variables should be assigned by reference'],
[E_NOTICE, 'Only variables should be assigned by reference'],
]);
}, Tester\AssertException::class, 'Error was expected, but was not generated');

Expand Down
2 changes: 1 addition & 1 deletion tests/Framework/Assert.noError.phpt
Expand Up @@ -13,7 +13,7 @@ Assert::noError(function () {

Assert::exception(function () {
Assert::noError(function () {
$a++;
$a = &pi();
});
}, Tester\AssertException::class, 'Generated more errors than expected: E_NOTICE %a%');

Expand Down
4 changes: 2 additions & 2 deletions tests/Framework/FileMock.phpt
Expand Up @@ -18,7 +18,7 @@ test(function () {
$cases = [
'r' => $tmp = [
[E_USER_WARNING, 'fopen(mock://none): failed to open stream: No such file or directory'],
[E_WARNING, 'fopen(mock://none): failed to open stream: "Tester\FileMock::stream_open" call failed'],
[E_WARNING, 'fopen(mock://none): %[fF]%ailed to open stream: "Tester\FileMock::stream_open" call failed'],
],
'r+' => $tmp,
'w' => [],
Expand Down Expand Up @@ -55,7 +55,7 @@ test(function () {
'a+' => [],
'x' => $tmp = [
[E_USER_WARNING, 'fopen(mock://%i%.): failed to open stream: File exists'],
[E_WARNING, 'fopen(mock://%i%.): failed to open stream: "Tester\FileMock::stream_open" call failed'],
[E_WARNING, 'fopen(mock://%i%.): %[fF]%ailed to open stream: "Tester\FileMock::stream_open" call failed'],
],
'x+' => $tmp,
'c' => [],
Expand Down
1 change: 1 addition & 0 deletions tests/Framework/Helpers.escapeArg.phpt
Expand Up @@ -9,6 +9,7 @@ require __DIR__ . '/../bootstrap.php';


$win = defined('PHP_WINDOWS_VERSION_BUILD');
setlocale(LC_CTYPE, 'en_US.UTF-8'); // to not strip non-ASCII characters

Assert::same($win ? '""' : "''", Helpers::escapeArg(''));
Assert::same($win ? '"啪lu钮ou膷k媒"' : "'啪lu钮ou膷k媒'", Helpers::escapeArg('啪lu钮ou膷k媒'));
Expand Down
14 changes: 7 additions & 7 deletions tests/Framework/TestCase.annotationThrows.phpt
Expand Up @@ -57,28 +57,28 @@ class MyTest extends Tester\TestCase
/** @throws E_NOTICE */
public function testNotice()
{
$a++;
$a = &pi();
}


/** @throws E_NOTICE Undefined variable: a */
/** @throws E_NOTICE Only variables should be assigned by reference */
public function testNoticeMessage()
{
$a++;
$a = &pi();
}


/** @throws E_WARNING */
public function testBadError()
{
$a++;
$a = &pi();
}


/** @throws E_NOTICE With message */
public function testNoticeBadMessage()
{
$a++;
$a = &pi();
}


Expand Down Expand Up @@ -142,8 +142,8 @@ $test->runTest('testNoticeMessage');

Assert::exception(function () use ($test) {
$test->runTest('testBadError');
}, Tester\AssertException::class, 'E_WARNING was expected, but E_NOTICE (Undefined variable: a) was generated in %a%testBadError()');
}, Tester\AssertException::class, 'E_WARNING was expected, but E_NOTICE (Only variables should be assigned by reference) was generated in %a%testBadError()');

Assert::exception(function () use ($test) {
$test->runTest('testNoticeBadMessage');
}, Tester\AssertException::class, "E_NOTICE with a message matching 'With message' was expected but got 'Undefined variable: a' in testNoticeBadMessage()");
}, Tester\AssertException::class, "E_NOTICE with a message matching 'With message' was expected but got 'Only variables should be assigned by reference' in testNoticeBadMessage()");
2 changes: 1 addition & 1 deletion tests/Runner/Runner.edge.phpt
Expand Up @@ -45,7 +45,7 @@ $runner->run();

Assert::same([Test::FAILED, 'Exited with error code 231 (expected 0)'], $logger->results['shutdown.exitCode.a.phptx']);

$bug65275 = PHP_SAPI === 'cli';
$bug65275 = PHP_SAPI === 'cli' && PHP_VERSION_ID < 80000;
Assert::same($bug65275 ? [Test::FAILED, 'Exited with error code 231 (expected 0)'] : [Test::PASSED, null], $logger->results['shutdown.exitCode.b.phptx']);

Assert::same([Test::SKIPPED, 'just skipping'], $logger->results['skip.phptx']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Runner/Runner.multiple-fails.phpt
Expand Up @@ -79,8 +79,8 @@ Assert::same(Test::FAILED, $logger->results['testcase-pre-fail.phptx'][0]);

Assert::match(
defined('PHPDBG_VERSION')
? '%A%Parse error: syntax error, unexpected end of file in %a%testcase-syntax-error.phptx on line %d%'
: 'Parse error: syntax error, unexpected end of file in %a%testcase-syntax-error.phptx on line %d%',
? '%A%Parse error: %a% in %a%testcase-syntax-error.phptx on line %d%'
: 'Parse error: %a% in %a%testcase-syntax-error.phptx on line %d%',
trim($logger->results['testcase-syntax-error.phptx'][1])
);
Assert::same(Test::FAILED, $logger->results['testcase-syntax-error.phptx'][0]);
Expand Down

0 comments on commit f19ff4c

Please sign in to comment.