Skip to content

Commit

Permalink
Add snapshot tests to PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
opdavies committed Dec 16, 2023
1 parent fa99fc0 commit fd9a18d
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/Unit/SnapshotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;

/**
* @group snapshots
*/
class SnapshotTest extends TestCase
{
/**
* @var array<int, string>
*/
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<int, string>
*/
private function getFiles(string $directory): array
{
$files = [];

$finder = new Finder();
$finder->in($directory)->files();

foreach ($finder as $file) {
$files[] = $file->getRelativePathname();
}

return $files;
}
}

0 comments on commit fd9a18d

Please sign in to comment.