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

View File

@ -11,13 +11,13 @@ use InvalidArgumentException;
use Pest\Exceptions\InvalidExpectationValue; use Pest\Exceptions\InvalidExpectationValue;
use Pest\Matchers\Any; use Pest\Matchers\Any;
use Pest\Support\Arr; use Pest\Support\Arr;
use Pest\Support\Exporter;
use Pest\Support\NullClosure; use Pest\Support\NullClosure;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\ExpectationFailedException;
use ReflectionFunction; use ReflectionFunction;
use ReflectionNamedType; use ReflectionNamedType;
use SebastianBergmann\Exporter\Exporter;
use Throwable; use Throwable;
/** /**
@ -896,9 +896,9 @@ final class Expectation
private function export(mixed $value): string private function export(mixed $value): string
{ {
if ($this->exporter === null) { 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\Covers\CoversNothing;
use Pest\Factories\TestCaseMethodFactory; use Pest\Factories\TestCaseMethodFactory;
use Pest\Support\Backtrace; use Pest\Support\Backtrace;
use Pest\Support\Exporter;
use Pest\Support\HigherOrderCallables; use Pest\Support\HigherOrderCallables;
use Pest\Support\NullClosure; use Pest\Support\NullClosure;
use Pest\TestSuite; use Pest\TestSuite;
use SebastianBergmann\Exporter\Exporter;
/** /**
* @internal * @internal
@ -276,12 +276,13 @@ final class TestCall
*/ */
private function addChain(string $file, int $line, string $name, array $arguments = null): self private function addChain(string $file, int $line, string $name, array $arguments = null): self
{ {
$exporter = Exporter::default();
$this->testCaseMethod $this->testCaseMethod
->chains ->chains
->add($file, $line, $name, $arguments); ->add($file, $line, $name, $arguments);
if ($this->descriptionLess) { if ($this->descriptionLess) {
$exporter = new Exporter(); Exporter::default();
if ($this->testCaseMethod->description !== null) { if ($this->testCaseMethod->description !== null) {
$this->testCaseMethod->description .= ' → '; $this->testCaseMethod->description .= ' → ';
} }

View File

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

View File

@ -2,7 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace Pest\Exporters; namespace Pest\Support;
use SebastianBergmann\Exporter\Exporter as BaseExporter; use SebastianBergmann\Exporter\Exporter as BaseExporter;
use SebastianBergmann\RecursionContext\Context; use SebastianBergmann\RecursionContext\Context;
@ -18,16 +18,22 @@ final class Exporter
private const MAX_ARRAY_ITEMS = 3; 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 = []; $result = [];
$array = $data; $array = $data;
$itemsCount = 0; $itemsCount = 0;
$exporter = new self(); $exporter = self::default();
$context ??= new Context(); $context ??= new Context();
$context->add($data); $context->add($data);

View File

@ -919,5 +919,4 @@
PASS Tests\Visual\Version PASS Tests\Visual\Version
✓ visual snapshot of help command output ✓ visual snapshot of help command output
Tests: 4 incomplete, 4 todos, 18 skipped, 627 passed (1514 assertions) Tests: 4 incomplete, 4 todos, 18 skipped, 636 passed (1556 assertions)

View File

@ -62,15 +62,15 @@ test('fails with wrong value and plain key with dots', function () use ($test_ar
test('not failures', function () use ($test_array) { test('not failures', function () use ($test_array) {
expect($test_array)->not->toHaveKey('c'); expect($test_array)->not->toHaveKey('c');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'c'"); })->throws(ExpectationFailedException::class, "Expecting Array () not to have key 'c'");
test('not failures with nested key', function () use ($test_array) { test('not failures with nested key', function () use ($test_array) {
expect($test_array)->not->toHaveKey('d.e'); expect($test_array)->not->toHaveKey('d.e');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'd.e'"); })->throws(ExpectationFailedException::class, "Expecting Array () not to have key 'd.e'");
test('not failures with plain key with dots', function () use ($test_array) { test('not failures with plain key with dots', function () use ($test_array) {
expect($test_array)->not->toHaveKey('key.with.dots'); expect($test_array)->not->toHaveKey('key.with.dots');
})->throws(ExpectationFailedException::class, "Expecting Array (...) not to have key 'key.with.dots'"); })->throws(ExpectationFailedException::class, "Expecting Array () not to have key 'key.with.dots'");
test('not failures with correct value', function () use ($test_array) { test('not failures with correct value', function () use ($test_array) {
expect($test_array)->not->toHaveKey('c', 'world'); expect($test_array)->not->toHaveKey('c', 'world');

View File

@ -13,6 +13,6 @@ $run = function () {
}; };
test('parallel', function () use ($run) { test('parallel', function () use ($run) {
expect($run())->toContain('Running 650 tests using 3 processes') expect($run())->toContain('Running 652 tests using 3 processes')
->toContain('Tests: 4 incomplete, 4 todos, 15 skipped, 627 passed (1546 assertions)'); ->toContain('Tests: 4 incomplete, 4 todos, 15 skipped, 629 passed (1548 assertions)');
}); });