Skip to content

Commit

Permalink
[ticket/16549] Fix tests
Browse files Browse the repository at this point in the history
1. If $service array is empty, $service[0] call will throw fatal error in PHP 8.
2. Division by zero was promoted to fatal error
and throws DivisionByZeroError exception in PHP 8+
3. Adjust make_clickable() logic to avoid 'Undefined array key 3' error.

PHPBB3-16549
  • Loading branch information
rxu committed Oct 18, 2020
1 parent fff200b commit fe38280
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpBB/includes/functions_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ function make_clickable($text, $server_url = false, string $class = 'postlink')
if (preg_match($magic_args[0], $text, $matches))
{
// Only apply $class from the corresponding function call argument (excepting emails which never has a class)
if ($magic_args[3] != $static_class && $magic_args[1] != MAGIC_URL_EMAIL)
if ($magic_args[1] != MAGIC_URL_EMAIL && $magic_args[3] != $static_class)
{
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions tests/error_collector_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function test_collection()
$collector->install();

// Cause a warning
1/0; $line = __LINE__;
// Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;

$collector->uninstall();

Expand All @@ -39,7 +40,7 @@ public function test_collection()

// Unfortunately $error_contents will contain the full path here,
// because the tests directory is outside of phpbb root path.
$this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents);
$this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
$this->assertStringEndsWith(" line $line", $error_contents);
}

Expand All @@ -49,7 +50,8 @@ public function test_collection_with_mask()
$collector->install();

// Cause a warning
1/0; $line = __LINE__;
// Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;

// Cause a "Notice: unserialize(): Error at offset 0 of 27 bytes in ..."
// "Undefined array index" used earlier was promoted to warning in PHP 8.0,
Expand All @@ -69,7 +71,7 @@ public function test_collection_with_mask()

// Unfortunately $error_contents will contain the full path here,
// because the tests directory is outside of phpbb root path.
$this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents);
$this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
$this->assertStringEndsWith(" line $line", $error_contents);
}
}
2 changes: 1 addition & 1 deletion tests/mock/container_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
if ($this->has($id))
{
$service = $this->services[$id];
if (is_array($service) && is_callable($service[0]))
if (is_array($service) && isset($service[0]) && is_callable($service[0]))
{
return call_user_func_array($service[0], $service[1]);
}
Expand Down

0 comments on commit fe38280

Please sign in to comment.