feat: allows array formats

This commit is contained in:
Nuno Maduro
2023-06-18 01:01:29 +01:00
parent 7e815cc985
commit 355a2349af
9 changed files with 49 additions and 75 deletions

View File

@ -9,6 +9,7 @@ use Closure;
use DateTimeInterface; use DateTimeInterface;
use Error; use Error;
use InvalidArgumentException; use InvalidArgumentException;
use JsonSerializable;
use Pest\Exceptions\InvalidExpectationValue; use Pest\Exceptions\InvalidExpectationValue;
use Pest\Matchers\Any; use Pest\Matchers\Any;
use Pest\Support\Arr; use Pest\Support\Arr;
@ -23,6 +24,7 @@ use ReflectionFunction;
use ReflectionNamedType; use ReflectionNamedType;
use Stringable; use Stringable;
use Throwable; use Throwable;
use Traversable;
/** /**
* @internal * @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(),
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 $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; $testCase = TestSuite::getInstance()->test;

View File

@ -0,0 +1,3 @@
{
"key": " <div class=\"container\">\n <div class=\"row\">\n <div class=\"col-md-12\">\n <h1>Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>"
}

View File

@ -0,0 +1,3 @@
{
"key": " <div class=\"container\">\n <div class=\"row\">\n <div class=\"col-md-12\">\n <h1>Snapshot<\/h1>\n <\/div>\n <\/div>\n <\/div>"
}

View File

@ -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 %

View File

@ -673,6 +673,8 @@
✓ pass with toString ✓ pass with toString
✓ pass with dataset with ('my-datas-set-value') ✓ pass with dataset with ('my-datas-set-value')
✓ within describe → 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
✓ failures with custom message ✓ failures with custom message
✓ not failures ✓ not failures
@ -1079,7 +1081,7 @@
✓ todo ✓ todo
✓ todo in parallel ✓ todo in parallel
PASS Tests\Visual\Version WARN Tests\Visual\Version
visual snapshot of help command output - visual snapshot of help command output
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 18 skipped, 741 passed (1784 assertions) Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 742 passed (1787 assertions)

View File

@ -75,6 +75,34 @@ describe('within describe', function () {
}); });
})->with(['my-datas-set-value']); })->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 () { test('failures', function () {
TestSuite::getInstance()->snapshots->save($this, $this->snapshotable); TestSuite::getInstance()->snapshots->save($this, $this->snapshotable);

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) { test('parallel', function () use ($run) {
expect($run('--exclude-group=integration')) 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'); ->toContain('Parallel: 3 processes');
})->skipOnWindows(); })->skipOnWindows();

View File

@ -1,8 +1,6 @@
<?php <?php
test('visual snapshot of help command output', function () { test('visual snapshot of help command output', function () {
$snapshot = __DIR__.'/../.snapshots/version-command.txt';
$output = function () { $output = function () {
$process = (new Symfony\Component\Process\Process(['php', 'bin/pest', '--version'], null, ['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'])); $process = (new Symfony\Component\Process\Process(['php', 'bin/pest', '--version'], null, ['COLLISION_PRINTER' => '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()); return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
}; };
if (getenv('REBUILD_SNAPSHOTS')) { expect($output())->toMatchSnapshot();
file_put_contents($snapshot, $output()); })->skipOnWindows()->skip(! getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'));
$this->markTestSkipped('Snapshot rebuilt.');
}
expect($output())->toContain(file_get_contents($snapshot));
})->skipOnWindows();