64 lines
3.3 KiB
PHP
64 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Process;
|
||
|
||
use PHPUnit\Framework\Attributes\Group;
|
||
use Symfony\Component\Finder\Finder;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* The limit of the process tester's isolation (docs/Prozessarchitektur/Architektur/04 §3.5, requirement T2): native PHP
|
||
* calls that bypass Laravel's managers – own database connections, sockets, curl, mail(), writing files by path,
|
||
* programs, reading the environment – cannot be stopped at run time on shared hosting. They are kept out of the
|
||
* application by this static check (like MailTestModeGateTest for mail): new ways out must use the framework's
|
||
* facades, which the tester controls. Every exception is named here with its reason.
|
||
*/
|
||
#[Group('sandbox')]
|
||
class SideEffectPathsTest extends TestCase
|
||
{
|
||
/** Forbidden native calls (regular expression => what it is). */
|
||
private const FORBIDDEN = [
|
||
'/new\s+\\\\?PDO\b/' => 'own database connection',
|
||
'/->getPdo\(/' => 'raw database handle',
|
||
'/\b(fsockopen|pfsockopen|stream_socket_client|socket_create)\s*\(/' => 'socket',
|
||
'/\bcurl_[a-z_]+\s*\(/' => 'curl',
|
||
'/(?<![\w>:$])(?<!function )mail\s*\(/' => 'mail()',
|
||
'/(?<![\w>:$])(?<!function )(file_put_contents|fopen|fwrite|unlink|rename|copy|touch|mkdir)\s*\(/' => 'file by path',
|
||
'/(?<![\w>:])(exec|shell_exec|passthru|system|popen|proc_open)\s*\(/' => 'program',
|
||
'/(?<![\w>:$])env\s*\(/' => 'environment outside config/',
|
||
'/\bgetenv\s*\(/' => 'environment outside config/',
|
||
];
|
||
|
||
/** Files allowed to use a forbidden call, with the reason. */
|
||
private const ALLOWED = [
|
||
'Console/Commands/BackupDatabase.php' => 'backup of the real database; console only, never reachable in a tester run (no scheduler there)',
|
||
'Console/Commands/ProcessCheck.php' => 'writes MODULMATRIX.md (developer command)',
|
||
'Console/Commands/ProcessSandboxRun.php' => 'writes the report into the run directory',
|
||
'Console/Commands/ProcessSandboxTemplate.php' => 'builds the template file',
|
||
'Filament/Resources/Guests/Pages/ListGuests.php' => 'CSV download to php://output',
|
||
'Process/Tester/EffectLedger.php' => 'appends to the run ledger',
|
||
'Process/Tester/ProcessSandbox.php' => 'slot locks and the check of proc_open',
|
||
'Process/Tester/SandboxBootstrapper.php' => 'reads FEWO_SANDBOX_RUN and copies the template before the application starts',
|
||
'Process/Tester/SandboxSnapshot.php' => 'none expected',
|
||
];
|
||
|
||
public function test_no_native_way_around_the_framework_in_the_application(): void
|
||
{
|
||
$violations = [];
|
||
foreach (Finder::create()->files()->in(app_path())->name('*.php') as $file) {
|
||
$relative = str_replace('\\', '/', $file->getRelativePathname());
|
||
if (array_key_exists($relative, self::ALLOWED)) {
|
||
continue;
|
||
}
|
||
$code = (string) preg_replace('~//[^\n]*|/\*.*?\*/~s', '', $file->getContents());
|
||
foreach (self::FORBIDDEN as $pattern => $what) {
|
||
if (preg_match($pattern, $code, $match) === 1) {
|
||
$violations[] = "{$relative}: {$what} ({$match[0]})";
|
||
}
|
||
}
|
||
}
|
||
|
||
$this->assertSame([], $violations, 'Native calls bypass the process tester\'s isolation – use Laravel facades (DB, Http, Mail, Storage, Process).');
|
||
}
|
||
}
|