mirror of
https://github.com/opdavies/build-configs.git
synced 2025-03-13 05:26:56 +00:00
Add snapshot tests to PHPUnit
This commit is contained in:
parent
fa99fc006f
commit
fd9a18dd92
70
tests/Unit/SnapshotTest.php
Executable file
70
tests/Unit/SnapshotTest.php
Executable file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue