feat: improves exporter

This commit is contained in:
Nuno Maduro
2023-02-13 23:56:19 +00:00
parent dd4247e150
commit 427ee89ae6
8 changed files with 29 additions and 23 deletions

View File

@ -13,8 +13,8 @@ use Pest\Arch\SingleArchExpectation;
use Pest\Exceptions\InvalidExpectation;
use Pest\Expectation;
use Pest\Support\Arr;
use Pest\Support\Exporter;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\Exporter\Exporter;
/**
* @internal
@ -160,7 +160,7 @@ final class OppositeExpectation
{
$arguments = is_array($arguments) ? $arguments : [$arguments];
$exporter = new Exporter();
$exporter = Exporter::default();
$toString = fn ($argument): string => $exporter->shortenedExport($argument);

View File

@ -11,13 +11,13 @@ use InvalidArgumentException;
use Pest\Exceptions\InvalidExpectationValue;
use Pest\Matchers\Any;
use Pest\Support\Arr;
use Pest\Support\Exporter;
use Pest\Support\NullClosure;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException;
use ReflectionFunction;
use ReflectionNamedType;
use SebastianBergmann\Exporter\Exporter;
use Throwable;
/**
@ -896,9 +896,9 @@ final class Expectation
private function export(mixed $value): string
{
if ($this->exporter === null) {
$this->exporter = new Exporter();
$this->exporter = Exporter::default();
}
return $this->exporter->export($value);
return $this->exporter->shortenedExport($value);
}
}

View File

@ -11,10 +11,10 @@ use Pest\Factories\Covers\CoversFunction;
use Pest\Factories\Covers\CoversNothing;
use Pest\Factories\TestCaseMethodFactory;
use Pest\Support\Backtrace;
use Pest\Support\Exporter;
use Pest\Support\HigherOrderCallables;
use Pest\Support\NullClosure;
use Pest\TestSuite;
use SebastianBergmann\Exporter\Exporter;
/**
* @internal
@ -276,12 +276,13 @@ final class TestCall
*/
private function addChain(string $file, int $line, string $name, array $arguments = null): self
{
$exporter = Exporter::default();
$this->testCaseMethod
->chains
->add($file, $line, $name, $arguments);
if ($this->descriptionLess) {
$exporter = new Exporter();
Exporter::default();
if ($this->testCaseMethod->description !== null) {
$this->testCaseMethod->description .= ' → ';
}

View File

@ -9,7 +9,7 @@ use Generator;
use Pest\Exceptions\DatasetAlreadyExists;
use Pest\Exceptions\DatasetDoesNotExist;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Exporters\Exporter;
use Pest\Support\Exporter;
use function sprintf;
use Traversable;
@ -229,7 +229,7 @@ final class DatasetsRepository
*/
private static function getDatasetDescription(int|string $key, array $data): string
{
$exporter = new Exporter();
$exporter = Exporter::default();
if (is_int($key)) {
return sprintf('(%s)', $exporter->shortenedRecursiveExport($data));

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Pest\Exporters;
namespace Pest\Support;
use SebastianBergmann\Exporter\Exporter as BaseExporter;
use SebastianBergmann\RecursionContext\Context;
@ -18,16 +18,22 @@ final class Exporter
private const MAX_ARRAY_ITEMS = 3;
/**
* The PHPUnit exporter.
* Creates a new Exporter instance.
*/
private readonly BaseExporter $exporter;
public function __construct(
private readonly BaseExporter $exporter,
) {
// ...
}
/**
* Instantiate the class.
* Creates a new Exporter instance.
*/
public function __construct()
public static function default(): self
{
$this->exporter = new BaseExporter();
return new self(
new BaseExporter()
);
}
/**
@ -40,7 +46,7 @@ final class Exporter
$result = [];
$array = $data;
$itemsCount = 0;
$exporter = new self();
$exporter = self::default();
$context ??= new Context();
$context->add($data);