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); // 无异常即通过 + } +}