test(php): 添加 compact 和 globals 相关功能测试用例

- 新增 CompactTest 类测试 compact('this') 在不同上下文中的行为
- 添加 CountGlobalsTest 类测试优化器路径中未定义变量检测逻辑
- 实现 globals 数组访问形式的编译通过验证
- 补充 optimizer-undefined-var.php 测试文件验证错误回退机制
- 添加 globals-array-access.php 测试文件验证数组访问正常编译
- 创建 global.php 示例文件展示全局变量使用方式
pull/3/head
韩天峰 2 months ago
parent 57517381a1
commit 785d9c9d6d
  1. 5
      examples/global.php
  2. 6
      phpunit/code/globals-array-access.php
  3. 5
      phpunit/code/optimizer-undefined-var.php
  4. 35
      phpunit/src/CompactTest.php
  5. 33
      phpunit/src/CountGlobalsTest.php

@ -0,0 +1,5 @@
<?php
$a = 1234;
$b = "hello";
$_POST['key'] = random_int(1, PHP_INT_MAX);
var_dump($GLOBALS);

@ -0,0 +1,6 @@
<?php
function main()
{
$GLOBALS['test_key'] = 'hello';
var_dump($GLOBALS['test_key']);
}

@ -0,0 +1,5 @@
<?php
function main()
{
var_dump(in_array($key, $array));
}

@ -0,0 +1,35 @@
<?php
use PhpAot\Php\CompilerTest;
use PhpAot\Php\Exception\TestError;
class CompactTest extends \BaseTest
{
public function testCompactThisOutsideClass(): void
{
$this->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'
);
}
}

@ -0,0 +1,33 @@
<?php
use PhpAot\Php\CompilerTest;
/**
* 从 count-globals.php 拆解的测试用例:
* - 优化器路径中未定义变量检测
* - $GLOBALS 数组访问形式正常编译
*/
class CountGlobalsTest extends \BaseTest
{
public function testOptimizerUndefinedVar()
{
// in_array($key, $array) 走优化器路径 (FuncCallOptimizer)
// 优化器检测到 $key 未定义后应回退到传统路径并给出明确错误
$this->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); // 无异常即通过
}
}
Loading…
Cancel
Save