Skip to content

Instantly share code, notes, and snippets.

@dillingham
Last active October 27, 2024 14:22
Show Gist options
  • Save dillingham/3f71297e3264681897b960e11b63e127 to your computer and use it in GitHub Desktop.
Save dillingham/3f71297e3264681897b960e11b63e127 to your computer and use it in GitHub Desktop.
<?php
test('All controllers must have corresponding test files with correct paths', function () {
$controllerPath = base_path('app/Http/Controllers');
$testPath = base_path('tests/Feature');
$controllerFiles = collect(File::allFiles($controllerPath))
->filter(fn($file) => $file->getExtension() === 'php')
->map(function ($file) use ($controllerPath) {
return str_replace([$controllerPath . '/', '.php'], '', $file->getPathname());
});
$testFiles = collect(File::allFiles($testPath))
->filter(fn($file) => $file->getExtension() === 'php')
->map(function ($file) use ($testPath) {
return str_replace([$testPath . '/', 'Test.php'], '', $file->getPathname());
});
$missingTests = $controllerFiles->diff($testFiles);
if ($missingTests->isNotEmpty()) {
echo 'Missing or incorrectly placed tests for controllers:' . PHP_EOL;
foreach ($missingTests as $missingTest) {
echo "- $missingTest" . PHP_EOL;
}
}
expect($missingTests->isEmpty())->toBeTrue(
'There are controllers without corresponding test files, see list above.'
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment