From e063f1d58e30e21049c6cb2a05913769abc17521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Mon, 20 Nov 2023 00:51:03 +0100 Subject: [PATCH 1/2] created test for #5567 --- tests/end-to-end/regression/5567.phpt | 32 +++++++++++++++++++ .../regression/5567/Issue5567Test.php | 23 +++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/end-to-end/regression/5567.phpt create mode 100644 tests/end-to-end/regression/5567/Issue5567Test.php diff --git a/tests/end-to-end/regression/5567.phpt b/tests/end-to-end/regression/5567.phpt new file mode 100644 index 00000000000..abcd46020ff --- /dev/null +++ b/tests/end-to-end/regression/5567.phpt @@ -0,0 +1,32 @@ +--TEST-- +https://github.com/sebastianbergmann/phpunit/issues/5567 +--FILE-- +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. diff --git a/tests/end-to-end/regression/5567/Issue5567Test.php b/tests/end-to-end/regression/5567/Issue5567Test.php new file mode 100644 index 00000000000..cbbcec219ad --- /dev/null +++ b/tests/end-to-end/regression/5567/Issue5567Test.php @@ -0,0 +1,23 @@ + + * + * 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); + } +} From a8355d7da9d0a6b3caabfcdfc97608dede9c43e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Mon, 20 Nov 2023 00:16:28 +0100 Subject: [PATCH 2/2] fix issue #5567 --- src/Util/Exporter.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Util/Exporter.php b/src/Util/Exporter.php index 4cbe4141ff8..797f1418cb9 100644 --- a/src/Util/Exporter.php +++ b/src/Util/Exporter.php @@ -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 @@ -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; @@ -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; } }