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

Add option to limit export of (large) arrays (to speed up PHPUnit) #59

Merged
merged 8 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

final readonly class Exporter
{
public function __construct(
private int $shortenArraysLongerThan = 10
) {
}

/**
* Exports a value as a string.
*
Expand Down Expand Up @@ -69,7 +74,16 @@ public function shortenedRecursiveExport(array &$data, ?RecursionContext $proces
/* @noinspection UnusedFunctionResultInspection */
$processed->add($data);

$i = 0;
$count = count($data, COUNT_RECURSIVE);
staabm marked this conversation as resolved.
Show resolved Hide resolved

foreach ($array as $key => $value) {
if ($count > $this->shortenArraysLongerThan && $i > $this->shortenArraysLongerThan) {
$result[] = sprintf('...%d more elements', $count - $this->shortenArraysLongerThan);

break;
}

if (is_array($value)) {
if ($processed->contains($data[$key]) !== false) {
$result[] = '*RECURSION*';
Expand All @@ -79,6 +93,8 @@ public function shortenedRecursiveExport(array &$data, ?RecursionContext $proces
} else {
$result[] = $this->shortenedExport($value);
}

$i++;
}

return implode(', ', $result);
Expand Down
7 changes: 7 additions & 0 deletions tests/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ public static function provideNonBinaryMultibyteStrings(): array

public static function shortenedRecursiveExportProvider(): array
{
$bigArray = [];

for ($i = 0; $i < 20_000; $i++) {
$bigArray[] = 'cast(\'foo' . $i . '\' as blob)';
}

return [
'null' => [[null], 'null'],
'boolean true' => [[true], 'true'],
Expand All @@ -333,6 +339,7 @@ public static function shortenedRecursiveExportProvider(): array
'with assoc array key' => [['foo' => 'bar'], '\'bar\''],
'multidimensional array' => [[[1, 2, 3], [3, 4, 5]], '[1, 2, 3], [3, 4, 5]'],
'object' => [[new stdClass], 'stdClass Object ()'],
'big array' => [$bigArray, "'cast('foo0' as blob)', 'cast('foo1' as blob)', 'cast('foo2' as blob)', 'cast('foo3' as blob)', 'cast('foo4' as blob)', 'cast('foo5' as blob)', 'cast('foo6' as blob)', 'cast('foo7' as blob)', 'cast('foo8' as blob)', 'cast('foo9' as blob)', 'cast('foo10' as blob)', ...19992 more elements"],
];
}

Expand Down