Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue 5567 #5590

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Util/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function is_array;
use function is_scalar;
use SebastianBergmann\RecursionContext\Context;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
Expand All @@ -26,7 +27,7 @@ public static function export(mixed $value, bool $exportObjects = false): string
return '{enable export of objects to see this value}';
}

private static function isScalarOrArrayOfScalars(mixed $value): bool
private static function isScalarOrArrayOfScalars(mixed &$value, Context $context = null): bool
{
if (is_scalar($value)) {
return true;
Expand All @@ -36,8 +37,19 @@ private static function isScalarOrArrayOfScalars(mixed $value): bool
return false;
}

foreach ($value as $_value) {
if (!self::isScalarOrArrayOfScalars($_value)) {
if (!$context) {
$context = new Context;
}

if ($context->contains($value) !== false) {
return true;
}

$array = $value;
$context->add($value);

foreach ($array as &$_value) {
if (!self::isScalarOrArrayOfScalars($_value, $context)) {
return false;
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/end-to-end/regression/5567.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5567
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = __DIR__ . '/5567/Issue5567Test.php';

require_once __DIR__ . '/../../bootstrap.php';
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

F 1 / 1 (100%)

Time: %s, Memory: %s MB

There was 1 failure:

1) PHPUnit\TestFixture\Issue5567\Issue5567Test::testAnythingThatFailsWithRecursiveArray
Failed asserting that Array &0 [
'self' => Array &1 [
'self' => Array &1,
],
] is false.

%sIssue5567Test.php:%d

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
23 changes: 23 additions & 0 deletions tests/end-to-end/regression/5567/Issue5567Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5567;

use PHPUnit\Framework\TestCase;

final class Issue5567Test extends TestCase
{
public function testAnythingThatFailsWithRecursiveArray(): void
{
$array = [];
$array['self'] = &$array;

$this->assertFalse($array);
}
}