102 lines
3.5 KiB
PHP
102 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Architecture;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
use RecursiveIteratorIterator;
|
|
use SplFileInfo;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ProjectCoverageStructureTest extends TestCase
|
|
{
|
|
/**
|
|
* Ensure every controller has at least one matching feature test area.
|
|
*/
|
|
public function test_controllers_are_accounted_for_by_feature_tests(): void
|
|
{
|
|
$controllers = $this->filesMatching(['/../../../app', '/../../../Modules'], '/Controller\.php$/');
|
|
$this->assertNotEmpty($controllers);
|
|
|
|
$testCorpus = '';
|
|
foreach ($this->filesMatching(['/../../Feature', '/../../../Modules'], '/Test\.php$/') as $testFile) {
|
|
$testCorpus .= file_get_contents($testFile) ?: '';
|
|
}
|
|
|
|
foreach ($controllers as $controller) {
|
|
$controllerName = pathinfo($controller, PATHINFO_FILENAME);
|
|
$resourceName = str_replace(['Controller', 'Dashboard'], '', $controllerName);
|
|
|
|
$this->assertTrue(
|
|
str_contains($testCorpus, $controllerName) || str_contains($testCorpus, $resourceName) || $controllerName === 'Controller',
|
|
"Controller [{$controllerName}] does not appear to be covered by the feature test corpus.",
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure the WMS baseline component is used without duplicate headers across module views.
|
|
*/
|
|
public function test_module_views_do_not_contain_duplicate_wms_page_hero_components(): void
|
|
{
|
|
$views = $this->filesMatching(['/../../../Modules'], '/\.blade\.php$/');
|
|
$this->assertNotEmpty($views);
|
|
|
|
foreach ($views as $view) {
|
|
if (str_ends_with($view, 'partials.blade.php') || str_ends_with($view, '_form.blade.php')) {
|
|
continue;
|
|
}
|
|
|
|
$contents = file_get_contents($view) ?: '';
|
|
|
|
$this->assertLessThanOrEqual(
|
|
1,
|
|
substr_count($contents, '<x-wms.page-hero'),
|
|
"View [{$view}] should not contain duplicate page headers.",
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure browser tests exist for desktop and mobile navigation flows.
|
|
*/
|
|
public function test_browser_navigation_tests_exist_for_mobile_and_desktop(): void
|
|
{
|
|
$browserTests = $this->filesMatching(['/../../Browser'], '/Test\.php$/');
|
|
$contents = implode("\n", array_map(static fn (string $file): string => file_get_contents($file) ?: '', $browserTests));
|
|
|
|
$this->assertStringContainsString('desktop sidebar', strtolower($contents));
|
|
$this->assertStringContainsString('mobile menu', strtolower($contents));
|
|
}
|
|
|
|
/**
|
|
* @param array<int, string> $relativeDirectories
|
|
* @return array<int, string>
|
|
*/
|
|
private function filesMatching(array $relativeDirectories, string $pattern): array
|
|
{
|
|
$files = [];
|
|
|
|
foreach ($relativeDirectories as $relativeDirectory) {
|
|
$directory = realpath(__DIR__ . $relativeDirectory);
|
|
|
|
if (! is_string($directory) || ! is_dir($directory)) {
|
|
continue;
|
|
}
|
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
|
|
|
|
foreach ($iterator as $file) {
|
|
if (! $file instanceof SplFileInfo || ! $file->isFile()) {
|
|
continue;
|
|
}
|
|
|
|
if (preg_match($pattern, $file->getFilename()) === 1) {
|
|
$files[] = $file->getPathname();
|
|
}
|
|
}
|
|
}
|
|
|
|
return array_values(array_unique($files));
|
|
}
|
|
}
|