From 785d9c9d6ddde4390b67214d8ce004531ca8ad44 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 20 Jun 2026 20:30:11 +0800 Subject: [PATCH] =?UTF-8?q?test(php):=20=E6=B7=BB=E5=8A=A0=20compact=20?= =?UTF-8?q?=E5=92=8C=20globals=20=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 CompactTest 类测试 compact('this') 在不同上下文中的行为 - 添加 CountGlobalsTest 类测试优化器路径中未定义变量检测逻辑 - 实现 globals 数组访问形式的编译通过验证 - 补充 optimizer-undefined-var.php 测试文件验证错误回退机制 - 添加 globals-array-access.php 测试文件验证数组访问正常编译 - 创建 global.php 示例文件展示全局变量使用方式 --- examples/global.php | 5 ++++ phpunit/code/globals-array-access.php | 6 ++++ phpunit/code/optimizer-undefined-var.php | 5 ++++ phpunit/src/CompactTest.php | 35 ++++++++++++++++++++++++ phpunit/src/CountGlobalsTest.php | 33 ++++++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 examples/global.php create mode 100644 phpunit/code/globals-array-access.php create mode 100644 phpunit/code/optimizer-undefined-var.php create mode 100644 phpunit/src/CompactTest.php create mode 100644 phpunit/src/CountGlobalsTest.php diff --git a/examples/global.php b/examples/global.php new file mode 100644 index 00000000..e94162fb --- /dev/null +++ b/examples/global.php @@ -0,0 +1,5 @@ +exec( + 'Cannot use compact("this") outside of class method', + 'compact-this-outside-class.php' + ); + } + + public function testCompactThisInsideClass(): void + { + $testFile = __DIR__ . '/../code/compact-this-inside-class.php'; + $compiler = CompilerTest::create(ROOT_PATH); + $compiler->addFiles([$testFile]); + $compiler->prepareFile($testFile); + $compiler->convertFile($testFile); + + $this->assertTrue(true); // 无异常即通过 + } + + public function testCompactThisMixedContext(): void + { + // compact('this') 在类方法内可通过,在普通函数内应 fatal + $this->exec( + 'Cannot use compact("this") outside of class method', + 'compact_no_this.php' + ); + } +} diff --git a/phpunit/src/CountGlobalsTest.php b/phpunit/src/CountGlobalsTest.php new file mode 100644 index 00000000..6a67f3ff --- /dev/null +++ b/phpunit/src/CountGlobalsTest.php @@ -0,0 +1,33 @@ +exec( + 'Undefined variable `$key`', + 'optimizer-undefined-var.php' + ); + } + + public function testGlobalsArrayAccessAllowed() + { + // $GLOBALS['key'] 数组访问形式应正常通过编译 + $testFile = __DIR__ . '/../code/globals-array-access.php'; + $compiler = CompilerTest::create(ROOT_PATH); + $compiler->addFiles([$testFile]); + $compiler->prepareFile($testFile); + $compiler->convertFile($testFile); + + $this->assertTrue(true); // 无异常即通过 + } +}