From b1c6f247e058a0fb3a997f89aff24b2c926b45bb Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 17 Jun 2023 13:36:39 +0100 Subject: [PATCH] chore: uses snapshot testing in some visual testing --- composer.json | 2 +- src/Plugins/Help.php | 4 +++ ...n_with_data_set___________array_____.snap} | 0 ...__parallel______array____parallel___.snap} | 0 ...sual_snapshot_of_help_command_output.snap} | 1 + tests/Visual/Collision.php | 26 +++++-------------- tests/Visual/Help.php | 10 +------ tests/Visual/Parallel.php | 2 -- 8 files changed, 14 insertions(+), 31 deletions(-) rename tests/{.snapshots/collision.txt => .pest/snapshots/Visual/Collision/collision_with_data_set___________array_____.snap} (100%) rename tests/{.snapshots/collision-parallel.txt => .pest/snapshots/Visual/Collision/collision_with_data_set_______parallel______array____parallel___.snap} (100%) rename tests/{.snapshots/help-command.txt => .pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap} (98%) diff --git a/composer.json b/composer.json index 52ccdba9..25153077 100644 --- a/composer.json +++ b/composer.json @@ -76,7 +76,7 @@ "test:inline": "php bin/pest --colors=always --configuration=phpunit.inline.xml", "test:parallel": "php bin/pest --colors=always --exclude-group=integration --parallel --processes=10", "test:integration": "php bin/pest --colors=always --group=integration", - "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always", + "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always --update-snapshots", "test": [ "@test:refacto", "@test:lint", diff --git a/src/Plugins/Help.php b/src/Plugins/Help.php index cbbb13a4..b0643461 100644 --- a/src/Plugins/Help.php +++ b/src/Plugins/Help.php @@ -107,6 +107,10 @@ final class Help implements HandlesArguments 'arg' => '--parallel', 'desc' => 'Run tests in parallel', ], + [ + 'arg' => '--update-snapshots', + 'desc' => 'Update snapshots for tests using the "toMatchSnapshot" expectation', + ], ], ...$content['Execution']]; $content['Selection'] = [[ diff --git a/tests/.snapshots/collision.txt b/tests/.pest/snapshots/Visual/Collision/collision_with_data_set___________array_____.snap similarity index 100% rename from tests/.snapshots/collision.txt rename to tests/.pest/snapshots/Visual/Collision/collision_with_data_set___________array_____.snap diff --git a/tests/.snapshots/collision-parallel.txt b/tests/.pest/snapshots/Visual/Collision/collision_with_data_set_______parallel______array____parallel___.snap similarity index 100% rename from tests/.snapshots/collision-parallel.txt rename to tests/.pest/snapshots/Visual/Collision/collision_with_data_set_______parallel______array____parallel___.snap diff --git a/tests/.snapshots/help-command.txt b/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap similarity index 98% rename from tests/.snapshots/help-command.txt rename to tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap index f32c248b..c2eb7a74 100644 --- a/tests/.snapshots/help-command.txt +++ b/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap @@ -34,6 +34,7 @@ EXECUTION OPTIONS: --parallel ........................................... Run tests in parallel + --update-snapshots Update snapshots for tests using the "toMatchSnapshot" expectation --process-isolation ................ Run each test in a separate PHP process --globals-backup ................. Backup and restore $GLOBALS for each test --static-backup ......... Backup and restore static properties for each test diff --git a/tests/Visual/Collision.php b/tests/Visual/Collision.php index de6dad47..23f9b815 100644 --- a/tests/Visual/Collision.php +++ b/tests/Visual/Collision.php @@ -1,12 +1,6 @@ getOutput()); }; - if (getenv('REBUILD_SNAPSHOTS')) { - $outputContent = explode("\n", $output()); + $outputContent = explode("\n", $output()); + array_pop($outputContent); + array_pop($outputContent); + array_pop($outputContent); + + if (in_array('--parallel', $arguments)) { array_pop($outputContent); array_pop($outputContent); - array_pop($outputContent); - - if (in_array('--parallel', $arguments)) { - array_pop($outputContent); - array_pop($outputContent); - } - - file_put_contents($snapshot, implode("\n", $outputContent)); - - $this->markTestSkipped('Snapshot rebuilt.'); } - expect($output())->toContain(file_get_contents($snapshot)); + expect(implode("\n", $outputContent))->toMatchSnapshot(); })->with([ [['']], [['--parallel']], diff --git a/tests/Visual/Help.php b/tests/Visual/Help.php index a3a77a87..4f48171d 100644 --- a/tests/Visual/Help.php +++ b/tests/Visual/Help.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)); + expect($output())->toMatchSnapshot(); })->skipOnWindows(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index e7eb527e..e27fb3b7 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -11,8 +11,6 @@ $run = function () { $process->run(); - // expect($process->getExitCode())->toBe(0); - return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput()); };