<?phpnamespace Tests;use App\Cryo\FreezeFile;/** Stack: the frames of a freeze file (.cryf) written by cryo_freeze(), their variables and the code of their line. */final class StackTest extends CryoTestCase{ public function testTheDemoFreezeShowsItsFramesInnermostFirst(): void { $form = $this->form(); $this->clickOn($form, 'navStack'); self::assertSame('stack', $form->view()); self::assertSame([ 'JobQueue::process() freeze_worker.php:29 2 args', 'runWorker() freeze_worker.php:43 1 args', '{main} freeze_worker.php:47', ], $this->items($form, 'frames')); $info = $this->text($form, 'freezeInfo'); self::assertStringContainsString('PHP 8.4.27 · frozen ', $info); self::assertStringContainsString('on cryo-demo · signed, zlib · 0 thaws · 3 frames · resumes after', $info); self::assertStringContainsString('Script: /mnt/demo/freeze_worker.php', $info); self::assertSame('worker.cryf', $this->text($form, 'freezeName')); } public function testTheInnermostFrameShowsItsVariablesAndItsLine(): void { $form = $this->form(); $this->clickOn($form, 'navStack'); $vars = $this->items($form, 'frameVars'); self::assertSame('$this = JobQueue {jobs: [5], done: 3}', $vars[0]); self::assertContains('$job = Job {id: 3, kind: "invoice", payload: [4]}', $vars); self::assertContains('$i = 2', $vars); self::assertSame('Variables of JobQueue::process()', $this->text($form, 'varsTitle')); self::assertMatchesRegularExpression("/^> 29 +cryo_freeze\\(\\\$snapshot, \\['mode' => 'continue', 'compress' => 'zlib'\\]\\);/m", $this->text($form, 'frameSource')); $this->game->shot('stack'); } public function testClickingTheOuterFramesShowsTheirVariables(): void { $form = $this->form(); $this->clickOn($form, 'navStack'); $this->clickRow($form, 'frames', 1); self::assertContains('$started = "batch-42"', $this->items($form, 'frameVars')); self::assertContains('$queue = JobQueue {jobs: [5], done: 3}', $this->items($form, 'frameVars')); self::assertMatchesRegularExpression('/^> 43 +return \$queue->process\(\$snapshot, 2\);/m', $this->text($form, 'frameSource')); $this->clickRow($form, 'frames', 2); self::assertSame(["\$config = ['retries' => 3, 'queue' => \"orders\"]"], $this->items($form, 'frameVars'), '{main}: the globals'); self::assertMatchesRegularExpression('/^> 47 +echo \'processed \', runWorker/m', $this->text($form, 'frameSource')); } public function testAWrongFreezeFileIsReportedAndOpenGoesToTheStack(): void { $form = $this->form(); $bad = sys_get_temp_dir() . '/not-a-freeze.cryf'; file_put_contents($bad, 'CRYO'); $form->openFiles([$bad]); $this->game->run(1); self::assertSame('stack', $form->view()); self::assertStringContainsString('not a .cryf file', $this->text($form, 'freezeInfo')); self::assertSame([], $this->items($form, 'frames')); @unlink($bad); $this->clickOn($form, 'demoFreeze'); self::assertCount(3, $this->items($form, 'frames')); } public function testReadingAFreezeRunsNoCodeOfIt(): void { $f = FreezeFile::open(dirname(__DIR__) . '/assets/demo/worker.cryf'); self::assertSame(2, $f->version); self::assertSame(FreezeFile::ZLIB | FreezeFile::SIGNED, $f->flags); self::assertSame('Job', $f->variables(0)['$job'][0]); self::assertFalse(class_exists('Job', false), 'objects stay __PHP_Incomplete_Class: no class is loaded, no __wakeup runs'); self::assertFalse(class_exists('JobQueue', false)); $raw = (string)file_get_contents(dirname(__DIR__) . '/assets/demo/worker.cryf'); $encrypted = substr($raw, 0, 6) . pack('v', FreezeFile::ENCRYPTED) . substr($raw, 8); $this->expectExceptionMessage('is encrypted'); FreezeFile::fromString($encrypted, 'x.cryf'); }}