From fd9a18dd9205beab1d30649524a59c0cf0aff491 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 16 Dec 2023 00:15:45 +0000 Subject: [PATCH] Add snapshot tests to PHPUnit --- tests/Unit/SnapshotTest.php | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 tests/Unit/SnapshotTest.php diff --git a/tests/Unit/SnapshotTest.php b/tests/Unit/SnapshotTest.php new file mode 100755 index 0000000..2c079c4 --- /dev/null +++ b/tests/Unit/SnapshotTest.php @@ -0,0 +1,70 @@ + + */ + private static $configs = [ + 'drupal', + 'drupal-commerce-kickstart', + 'drupal-localgov', + ]; + + public function testCompareFiles(): void + { + foreach (self::$configs as $config) { + $baseDir = getcwd() . "/tests/snapshots/output/{$config}"; + $generatedDir = getcwd() . "/.ignored/snapshots/output/{$config}"; + + $this->runCliTool($config); + + $baseFiles = $this->getFiles($baseDir); + + foreach ($baseFiles as $file) { + $this->assertFileEquals( + expected: $baseDir . '/' . $file, + actual: $generatedDir . '/' . $file, + message: "Files do not match: {$file}", + ); + } + } + } + + private function runCliTool(string $config): void + { + $cliCommand = sprintf( + "%s app:generate --config-file %s --output-dir %s", + getcwd() . '/bin/build-configs', + getcwd() . "/tests/snapshots/configs/{$config}.yaml", + getcwd() . "/.ignored/snapshots/output/{$config}", + ); + + exec($cliCommand); + } + + /** + * @return array + */ + private function getFiles(string $directory): array + { + $files = []; + + $finder = new Finder(); + $finder->in($directory)->files(); + + foreach ($finder as $file) { + $files[] = $file->getRelativePathname(); + } + + return $files; + } +}