test(assign): 添加变量重新赋值类型检查的测试用例

- 更新了现有测试用例的消息格式以包含具体的变量和类型信息
- 添加了对象类型到各种基本类型的重新赋值测试用例(int、float、bool、str、array)
- 添加了基本类型到对象类型的重新赋值测试用例(int、float、bool、array)
- 添加了字符串数组到各种基本类型的重新赋值测试用例(int、float、bool)
- 添加了标量类型到数组类型的重新赋值测试用例(int、float、bool、str)
- 新增了多个PHP代码文件用于测试不同类型的重新赋值场景
- 重命名了原有的字符串到对象重新赋值测试文件
pull/1/head
韩天峰 3 months ago
parent 5d150a2b02
commit f331591b65
  1. 7
      phpunit/code/re-assign-array-to-bool.php
  2. 7
      phpunit/code/re-assign-array-to-float.php
  3. 7
      phpunit/code/re-assign-array-to-int.php
  4. 7
      phpunit/code/re-assign-array-to-obj.php
  5. 7
      phpunit/code/re-assign-bool-to-array.php
  6. 7
      phpunit/code/re-assign-bool-to-obj.php
  7. 7
      phpunit/code/re-assign-float-to-array.php
  8. 7
      phpunit/code/re-assign-float-to-obj.php
  9. 7
      phpunit/code/re-assign-int-to-array.php
  10. 7
      phpunit/code/re-assign-int-to-obj.php
  11. 3
      phpunit/code/re-assign-obj-to-array.php
  12. 7
      phpunit/code/re-assign-obj-to-bool.php
  13. 7
      phpunit/code/re-assign-obj-to-float.php
  14. 7
      phpunit/code/re-assign-obj-to-int.php
  15. 8
      phpunit/code/re-assign-obj-to-str.php
  16. 7
      phpunit/code/re-assign-str-to-array.php
  17. 7
      phpunit/code/re-assign-str-to-bool.php
  18. 7
      phpunit/code/re-assign-str-to-float.php
  19. 7
      phpunit/code/re-assign-str-to-int.php
  20. 15
      phpunit/code/re-assign-str-to-obj.php
  21. 107
      phpunit/src/AssignTest.php

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = true;
$x = [1, 2];
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 3.14;
$x = [1, 2];
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 42;
$x = [1, 2];
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$obj = new stdClass();
$obj = [1, 2];
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = [1, 2];
$x = true;
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$obj = new stdClass();
$obj = true;
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = [1, 2];
$x = 3.14;
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$obj = new stdClass();
$obj = 3.14;
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = [1, 2];
$x = 42;
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$obj = new stdClass();
$obj = 42;
}

@ -1,7 +1,8 @@
<?php
use native_types;
function main()
{
$obj = [];
$obj = new stdClass();
}
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = true;
$x = new stdClass();
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 3.14;
$x = new stdClass();
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 42;
$x = new stdClass();
}

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
use native_types;
function main()
{
$x = "hello";
$x = new stdClass();
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = [1, 2];
$x = "hello";
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = true;
$x = "hello";
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 3.14;
$x = "hello";
}

@ -0,0 +1,7 @@
<?php
use native_types;
function main()
{
$x = 42;
$x = "hello";
}

@ -1,15 +0,0 @@
<?php
function bar()
{
return 'string';
}
function foo(stdClass $o)
{
$o = bar();
}
function main()
{
$obj = new stdClass();
foo($obj);
}

@ -2,9 +2,11 @@
class AssignTest extends \BaseTest
{
// === Existing tests ===
public function testReAssign()
{
$this->exec('Cannot re-assign variable', 're-assign.php');
$this->exec('Cannot re-assign `$obj` from `php::Str` to `php::Object`', 're-assign.php');
}
public function testAssignClass()
@ -51,4 +53,107 @@ class AssignTest extends \BaseTest
'std-unsafe-ptr-argument-requires-container.php'
);
}
// === Object value assigned to non-object variable (right side is New_ expr) ===
public function testObjectToInt()
{
$this->exec("Cannot re-assign `\$x` from `php::Object` to `php::Int`", 're-assign-obj-to-int.php');
}
public function testObjectToFloat()
{
$this->exec("Cannot re-assign `\$x` from `php::Object` to `php::Float`", 're-assign-obj-to-float.php');
}
public function testObjectToBool()
{
$this->exec("Cannot re-assign `\$x` from `php::Object` to `php::Bool`", 're-assign-obj-to-bool.php');
}
public function testObjectToStr()
{
$this->exec("Cannot re-assign `\$x` from `php::Object` to `php::Str`", 're-assign-obj-to-str.php');
}
public function testObjectToArray()
{
$this->exec("Cannot re-assign `\$obj` from `php::Object` to `php::Array`", 're-assign-obj-to-array.php');
}
// === Non-object value assigned to Object variable ===
public function testIntToObject()
{
$this->exec("Cannot re-assign `\$obj` from `php::Int` to `php::Object`", 're-assign-int-to-obj.php');
}
public function testFloatToObject()
{
$this->exec("Cannot re-assign `\$obj` from `php::Float` to `php::Object`", 're-assign-float-to-obj.php');
}
public function testBoolToObject()
{
$this->exec("Cannot re-assign `\$obj` from `php::Bool` to `php::Object`", 're-assign-bool-to-obj.php');
}
public function testArrayToObject()
{
$this->exec("Cannot re-assign `\$obj` from `php::Array` to `php::Object`", 're-assign-array-to-obj.php');
}
// === Str / Array value assigned to non-object scalar variable ===
public function testStrToInt()
{
$this->exec("Cannot re-assign `\$x` from `php::Str` to `php::Int`", 're-assign-str-to-int.php');
}
public function testArrayToInt()
{
$this->exec("Cannot re-assign `\$x` from `php::Array` to `php::Int`", 're-assign-array-to-int.php');
}
public function testStrToFloat()
{
$this->exec("Cannot re-assign `\$x` from `php::Str` to `php::Float`", 're-assign-str-to-float.php');
}
public function testArrayToFloat()
{
$this->exec("Cannot re-assign `\$x` from `php::Array` to `php::Float`", 're-assign-array-to-float.php');
}
public function testStrToBool()
{
$this->exec("Cannot re-assign `\$x` from `php::Str` to `php::Bool`", 're-assign-str-to-bool.php');
}
public function testArrayToBool()
{
$this->exec("Cannot re-assign `\$x` from `php::Array` to `php::Bool`", 're-assign-array-to-bool.php');
}
// === Scalar value assigned to Array variable ===
public function testIntToArray()
{
$this->exec("Cannot re-assign `\$x` from `php::Int` to `php::Array`", 're-assign-int-to-array.php');
}
public function testFloatToArray()
{
$this->exec("Cannot re-assign `\$x` from `php::Float` to `php::Array`", 're-assign-float-to-array.php');
}
public function testBoolToArray()
{
$this->exec("Cannot re-assign `\$x` from `php::Bool` to `php::Array`", 're-assign-bool-to-array.php');
}
public function testStrToArray()
{
$this->exec("Cannot re-assign `\$x` from `php::Str` to `php::Array`", 're-assign-str-to-array.php');
}
}

Loading…
Cancel
Save