ldc-cocotte/tests/Architecture/CodingStandardsTest.php

127 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Architecture;
use Shared\Filesystem\TemporaryFileFactory;
use Shared\Helpers\UuidHelper;
use Symfony\Component\Finder\Finder;
// Coding standards
arch()->preset()->php();
arch()->preset()->security()->ignoring([
TemporaryFileFactory::class,
]);
arch('it has no shell execution functions are used')
->expect(['exec', 'shell_exec', 'system', 'passthru', 'proc_open'])
->not->toBeUsed();
arch('it has no eval usage')
->expect(['eval'])
->not->toBeUsed();
arch('it has no dangerous file functions')
->expect(['file_get_contents', 'file_put_contents', 'fopen'])
->not->toBeUsed();
arch('it has no globals are used')
->expect(['$GLOBALS', 'global'])
->not->toBeUsed();
arch('it has no singletons')
->expect(['getInstance', 'instance'])
->not->toBeUsed();
arch('it has no insecure hashing')
->expect(['md5', 'sha1'])
->not->toBeUsed();
arch('it has no sleep function are used')
->expect(['sleep', 'usleep'])
->not->toBeUsed();
arch('it has no debug globals are used')
->expect(['var_dump', 'die', 'print_r'])
->not->toBeUsed();
arch('all app source files use strict types')
->expect('App')
->toUseStrictTypes();
arch('all domain source files use strict types')
->expect('Domain')
->toUseStrictTypes();
arch('all shared source files use strict types')
->expect('Shared')
->toUseStrictTypes();
arch('all infrastructure source files use strict types')
->expect('Infrastructure')
->toUseStrictTypes();
arch('Ramsey Uuid factory is encapsulated in UuidHelper')
->expect('Ramsey\Uuid\Uuid')
->not->toBeUsed()
->ignoring([UuidHelper::class]);
arch('Symfony Uid is encapsulated in UuidHelper')
->expect('Symfony\Component\Uid')
->not->toBeUsed()
->ignoring([UuidHelper::class]);
it('enforces PSR-4 namespaces and bans anti-patterns across tests', function (): void {
// GIVEN
$testRoot = dirname(__DIR__);
$finder = Finder::create()
->files()
->in([$testRoot . '/Unit', $testRoot . '/Integration', $testRoot . '/Feature'])
->name('*Test.php');
// WHEN & THEN
foreach ($finder as $file) {
$relativePath = str_replace($testRoot . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$expectedNs = 'Tests\\' . str_replace(DIRECTORY_SEPARATOR, '\\', dirname($relativePath));
$contents = $file->getContents();
expect($contents)->toContain("namespace {$expectedNs};")
->and($contents)->not->toContain('expect(true)->toBeTrue()')
->and($contents)->not->toContain('newInstanceWithoutConstructor')
->and($contents)->not->toContain('Schema::create(');
if (! str_ends_with($relativePath, 'UuidHelperTest.php')) {
expect($contents)->not->toContain('use Ramsey\\Uuid\\Uuid;')
->and($contents)->not->toContain('Ramsey\\Uuid\\Uuid::')
->and($contents)->not->toContain('Symfony\\Component\\Uid');
}
}
});
it('keeps unit tests isolated from Laravel runtime helpers and facades', function (): void {
// GIVEN
$unitFinder = Finder::create()
->files()
->in(dirname(__DIR__) . '/Unit')
->name('*Test.php');
$forbiddenPatterns = [
'uses(TestCase::class)',
'uses(RefreshDatabase::class)',
'Illuminate\\Support\\Facades\\',
'Pest\\Laravel\\',
'UploadedFile::fake',
];
// WHEN & THEN
foreach ($unitFinder as $file) {
$contents = $file->getContents();
foreach ($forbiddenPatterns as $pattern) {
expect($contents)->not->toContain($pattern);
}
expect(preg_match('/(?<![:>\w$-])(app|resolve|now|collect)\s*\(/', $contents))->toBe(0);
}
});