From 355a2349aff52f9241b8bf71864580baed5ea0b4 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 18 Jun 2023 01:01:29 +0100 Subject: [PATCH] feat: allows array formats --- src/Mixins/Expectation.php | 8 ++- .../toMatchSnapshot/pass_with__toArray_.snap | 3 + .../toMatchSnapshot/pass_with_array.snap | 3 + ...sual_snapshot_of_help_command_output.snap} | 0 tests/.snapshots/coverage.txt | 60 ------------------- tests/.snapshots/success.txt | 8 ++- tests/Features/Expect/toMatchSnapshot.php | 28 +++++++++ tests/Visual/Parallel.php | 2 +- tests/Visual/Version.php | 12 +--- 9 files changed, 49 insertions(+), 75 deletions(-) create mode 100644 tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray_.snap create mode 100644 tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array.snap rename tests/{.snapshots/version-command.txt => .pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap} (100%) delete mode 100644 tests/.snapshots/coverage.txt diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index da6c2107..c6876530 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -9,6 +9,7 @@ use Closure; use DateTimeInterface; use Error; use InvalidArgumentException; +use JsonSerializable; use Pest\Exceptions\InvalidExpectationValue; use Pest\Matchers\Any; use Pest\Support\Arr; @@ -23,6 +24,7 @@ use ReflectionFunction; use ReflectionNamedType; use Stringable; use Throwable; +use Traversable; /** * @internal @@ -809,7 +811,11 @@ final class Expectation is_object($this->value) && method_exists($this->value, '__toString') => $this->value->__toString(), is_object($this->value) && method_exists($this->value, 'toString') => $this->value->toString(), $this->value instanceof \Illuminate\Testing\TestResponse => $this->value->getContent(), // @phpstan-ignore-line - default => InvalidExpectationValue::expected('Stringable|string'), + is_array($this->value) => json_encode($this->value, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), + $this->value instanceof Traversable => json_encode(iterator_to_array($this->value), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), + $this->value instanceof JsonSerializable => json_encode($this->value->jsonSerialize(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), + is_object($this->value) && method_exists($this->value, 'toArray') => json_encode($this->value->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT), + default => InvalidExpectationValue::expected('array|object|string'), }; $testCase = TestSuite::getInstance()->test; diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray_.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray_.snap new file mode 100644 index 00000000..afd4f5f9 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with__toArray_.snap @@ -0,0 +1,3 @@ +{ + "key": "
\n
\n
\n

Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>" +} \ No newline at end of file diff --git a/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array.snap b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array.snap new file mode 100644 index 00000000..afd4f5f9 --- /dev/null +++ b/tests/.pest/snapshots/Features/Expect/toMatchSnapshot/pass_with_array.snap @@ -0,0 +1,3 @@ +{ + "key": "
\n
\n
\n

Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>" +} \ No newline at end of file diff --git a/tests/.snapshots/version-command.txt b/tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap similarity index 100% rename from tests/.snapshots/version-command.txt rename to tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap diff --git a/tests/.snapshots/coverage.txt b/tests/.snapshots/coverage.txt deleted file mode 100644 index e7cf7549..00000000 --- a/tests/.snapshots/coverage.txt +++ /dev/null @@ -1,60 +0,0 @@ - - PASS Tests\Playground - ✓ basic - - Tests: 1 passed - Time: 0.20s - Cov: 6.49% - - Actions/AddsDefaults ........................................... 0.0 % - Actions/AddsTests .............................................. 0.0 % - Actions/LoadStructure .......................................... 0.0 % - Actions/ValidatesConfiguration ................................. 0.0 % - Actions/ValidatesEnvironment ................................... 0.0 % - Concerns/TestCase 40..54, 71..88, 123..126, 147 ............... 44.4 % - Console/Command ................................................ 0.0 % - Contracts/HasPrintableTestCaseName ............................. 0.0 % - Contracts/Plugins/AddsOutput ................................ 100.0 % - Contracts/Plugins/HandlesArguments .......................... 100.0 % - Datasets ....................................................... 0.0 % - Exceptions/AfterAllAlreadyExist ................................ 0.0 % - Exceptions/AfterEachAlreadyExist ............................... 0.0 % - Exceptions/AttributeNotSupportedYet ............................ 0.0 % - Exceptions/BeforeEachAlreadyExist .............................. 0.0 % - Exceptions/DatasetAlreadyExist ................................. 0.0 % - Exceptions/DatasetDoesNotExist ................................. 0.0 % - Exceptions/FileOrFolderNotFound ................................ 0.0 % - Exceptions/InvalidConsoleArgument .............................. 0.0 % - Exceptions/InvalidPestCommand .................................. 0.0 % - Exceptions/InvalidUsesPath ..................................... 0.0 % - Exceptions/ShouldNotHappen ..................................... 0.0 % - Exceptions/TestAlreadyExist .................................... 0.0 % - Exceptions/TestCaseAlreadyInUse ................................ 0.0 % - Exceptions/TestCaseClassOrTraitNotFound ........................ 0.0 % - Factories/TestCaseFactory 111..133, 141..204 ................... 8.2 % - Laravel/Commands/PestDatasetCommand ............................ 0.0 % - Laravel/Commands/PestInstallCommand ............................ 0.0 % - Laravel/Commands/PestTestCommand ............................... 0.0 % - Laravel/PestServiceProvider .................................... 0.0 % - PendingObjects/AfterEachCall ................................... 0.0 % - PendingObjects/BeforeEachCall .................................. 0.0 % - PendingObjects/TestCall ........................................ 0.0 % - PendingObjects/UsesCall ........................................ 0.0 % - Plugin ......................................................... 0.0 % - Repositories/AfterAllRepository ................................ 0.0 % - Repositories/AfterEachRepository 28..33 ....................... 60.0 % - Repositories/BeforeAllRepository ............................... 0.0 % - Repositories/BeforeEachRepository 26..31 ...................... 20.0 % - Repositories/TestRepository .................................... 0.0 % - Support/Backtrace .............................................. 0.0 % - Support/ChainableClosure .................................... 100.0 % - Support/Container .............................................. 0.0 % - Support/ExceptionTrace 25..32 ................................. 28.6 % - Support/HigherOrderMessage ..................................... 0.0 % - Support/HigherOrderMessageCollection 24..25, 33, 43 ........... 50.0 % - Support/HigherOrderTapProxy .................................... 0.0 % - Support/NullClosure ......................................... 100.0 % - Support/Reflection ............................................. 0.0 % - Support/Str .................................................... 0.0 % - TestSuite 80..87, 95..101, 105 ................................ 20.0 % - globals ........................................................ 0.0 % diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 4927d35c..45efb415 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -673,6 +673,8 @@ ✓ pass with toString ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') + ✓ pass with toArray + ✓ pass with array ✓ failures ✓ failures with custom message ✓ not failures @@ -1079,7 +1081,7 @@ ✓ todo ✓ todo in parallel - PASS Tests\Visual\Version - ✓ visual snapshot of help command output + WARN Tests\Visual\Version + - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 18 skipped, 741 passed (1784 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 742 passed (1787 assertions) \ No newline at end of file diff --git a/tests/Features/Expect/toMatchSnapshot.php b/tests/Features/Expect/toMatchSnapshot.php index 8887e2e1..29a63a59 100644 --- a/tests/Features/Expect/toMatchSnapshot.php +++ b/tests/Features/Expect/toMatchSnapshot.php @@ -75,6 +75,34 @@ describe('within describe', function () { }); })->with(['my-datas-set-value']); +test('pass with `toArray`', function () { + TestSuite::getInstance()->snapshots->save($this, json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT)); + + $object = new class($this->snapshotable) + { + public function __construct(protected string $snapshotable) + { + } + + public function toArray() + { + return [ + 'key' => $this->snapshotable, + ]; + } + }; + + expect($object)->toMatchSnapshot()->toMatchSnapshot(); +}); + +test('pass with array', function () { + TestSuite::getInstance()->snapshots->save($this, json_encode(['key' => $this->snapshotable], JSON_PRETTY_PRINT)); + + expect([ + 'key' => $this->snapshotable, + ])->toMatchSnapshot()->toMatchSnapshot(); +}); + test('failures', function () { TestSuite::getInstance()->snapshots->save($this, $this->snapshotable); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index d44a5de3..eff61789 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 730 passed (1769 assertions)') + ->toContain('Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 15 skipped, 732 passed (1773 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows(); diff --git a/tests/Visual/Version.php b/tests/Visual/Version.php index 7db09e3c..50b156b8 100644 --- a/tests/Visual/Version.php +++ b/tests/Visual/Version.php @@ -1,8 +1,6 @@ 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'])); @@ -11,11 +9,5 @@ test('visual snapshot of help command output', function () { return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); }; - if (getenv('REBUILD_SNAPSHOTS')) { - file_put_contents($snapshot, $output()); - - $this->markTestSkipped('Snapshot rebuilt.'); - } - - expect($output())->toContain(file_get_contents($snapshot)); -})->skipOnWindows(); + expect($output())->toMatchSnapshot(); +})->skipOnWindows()->skip(! getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'));