diff --git a/phpunit.xml b/phpunit.xml index 4aac1aa2..122f54e2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,6 +16,7 @@ ./tests + ./tests-external ./tests/.snapshots ./tests/.tests ./tests/Fixtures/Inheritance diff --git a/src/Repositories/SnapshotRepository.php b/src/Repositories/SnapshotRepository.php index 7f7a9573..c719f219 100644 --- a/src/Repositories/SnapshotRepository.php +++ b/src/Repositories/SnapshotRepository.php @@ -19,6 +19,7 @@ final class SnapshotRepository * Creates a snapshot repository instance. */ public function __construct( + private readonly string $rootPath, private readonly string $testsPath, private readonly string $snapshotsPath, ) {} @@ -103,7 +104,19 @@ final class SnapshotRepository */ private function getSnapshotFilename(): string { - $relativePath = str_replace($this->testsPath, '', TestSuite::getInstance()->getFilename()); + $testFile = TestSuite::getInstance()->getFilename(); + + if (str_starts_with($testFile, $this->testsPath)) { + // if the test file is in the tests directory + $startPath = $this->testsPath; + } else { + // if the test file is in the app, src, etc. directory + $startPath = $this->rootPath; + } + + // relative path: we use substr() and not str_replace() to remove the start path + // for instance, if the $startPath is /app/ and the $testFile is /app/app/tests/Unit/ExampleTest.php, we should only remove the first /app/ from the path + $relativePath = substr($testFile, strlen($startPath)); // remove extension from filename $relativePath = substr($relativePath, 0, (int) strrpos($relativePath, '.')); diff --git a/src/TestSuite.php b/src/TestSuite.php index ca35a040..e96a6ba9 100644 --- a/src/TestSuite.php +++ b/src/TestSuite.php @@ -78,6 +78,7 @@ final class TestSuite $this->afterAll = new AfterAllRepository; $this->rootPath = (string) realpath($rootPath); $this->snapshots = new SnapshotRepository( + $this->rootPath, implode(DIRECTORY_SEPARATOR, [$this->rootPath, $this->testPath]), implode(DIRECTORY_SEPARATOR, ['.pest', 'snapshots']), ); diff --git a/tests-external/Features/Expect/toMatchSnapshot.php b/tests-external/Features/Expect/toMatchSnapshot.php new file mode 100644 index 00000000..db7b9666 --- /dev/null +++ b/tests-external/Features/Expect/toMatchSnapshot.php @@ -0,0 +1,35 @@ +snapshotable = <<<'HTML' +
+
+
+

Snapshot

+
+
+
+ HTML; +}); + +test('pass with dataset', function ($data) { + TestSuite::getInstance()->snapshots->save($this->snapshotable); + [$filename] = TestSuite::getInstance()->snapshots->get(); + + expect($filename)->toStartWith('tests/.pest/snapshots-external/') + ->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') + ->and($this->snapshotable)->toMatchSnapshot(); +})->with(['my-datas-set-value']); + +describe('within describe', function () { + test('pass with dataset', function ($data) { + TestSuite::getInstance()->snapshots->save($this->snapshotable); + [$filename] = TestSuite::getInstance()->snapshots->get(); + + expect($filename)->toStartWith('tests/.pest/snapshots-external/') + ->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') + ->and($this->snapshotable)->toMatchSnapshot(); + }); +})->with(['my-datas-set-value']); \ No newline at end of file diff --git a/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/_within_describe__→_pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap b/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/_within_describe__→_pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap new file mode 100644 index 00000000..c2b4dc0a --- /dev/null +++ b/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/_within_describe__→_pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap @@ -0,0 +1,7 @@ +
+
+
+

Snapshot

+
+
+
\ No newline at end of file diff --git a/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap b/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap new file mode 100644 index 00000000..c2b4dc0a --- /dev/null +++ b/tests/.pest/snapshots-external/Features/Expect/toMatchSnapshot/pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap @@ -0,0 +1,7 @@ +
+
+
+

Snapshot

+
+
+
\ No newline at end of file diff --git a/tests/Features/Expect/toMatchSnapshot.php b/tests/Features/Expect/toMatchSnapshot.php index c3df8bac..c2515829 100644 --- a/tests/Features/Expect/toMatchSnapshot.php +++ b/tests/Features/Expect/toMatchSnapshot.php @@ -74,7 +74,8 @@ test('pass with dataset', function ($data) { TestSuite::getInstance()->snapshots->save($this->snapshotable); [$filename] = TestSuite::getInstance()->snapshots->get(); - expect($filename)->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') + expect($filename)->toStartWith('tests/.pest/snapshots/') + ->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') ->and($this->snapshotable)->toMatchSnapshot(); })->with(['my-datas-set-value']); @@ -83,7 +84,8 @@ describe('within describe', function () { TestSuite::getInstance()->snapshots->save($this->snapshotable); [$filename] = TestSuite::getInstance()->snapshots->get(); - expect($filename)->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') + expect($filename)->toStartWith('tests/.pest/snapshots/') + ->toEndWith('pass_with_dataset_with_data_set____my_datas_set_value______my_datas_set_value__.snap') ->and($this->snapshotable)->toMatchSnapshot(); }); })->with(['my-datas-set-value']);