From 192bba345487269cb539f56e3598f3e944d52e14 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 25 May 2026 12:46:39 +0800 Subject: [PATCH] =?UTF-8?q?test(namespace):=20=E6=B7=BB=E5=8A=A0=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=9A=84=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 - 添加匿名类在命名空间中的测试 - 添加跨命名空间类常量引用的测试 - 添加跨命名空间继承的测试 - 添加完全限定名与相对名的测试 - 添加组合use导入的测试 - 添加跨命名空间接口实现的测试 - 添加混合use类和函数导入的测试 - 添加命名空间常量定义的测试 - 添加多层嵌套命名空间的测试 - 添加self/static跨命名空间解析的测试 - 添加跨命名空间trait使用的测试 - 添加use别名的测试 - 添加use const导入常量的测试 --- tests/aot/namespace/anonymous-class-ns.phpt | 35 ++++++++ tests/aot/namespace/class-const-ref.phpt | 61 ++++++++++++++ tests/aot/namespace/cross-ns-inheritance.phpt | 80 +++++++++++++++++++ tests/aot/namespace/fully-qualified.phpt | 41 ++++++++++ tests/aot/namespace/group-use.phpt | 46 +++++++++++ tests/aot/namespace/interface-across-ns.phpt | 58 ++++++++++++++ tests/aot/namespace/mixed-use-imports.phpt | 51 ++++++++++++ tests/aot/namespace/namespace-constants.phpt | 43 ++++++++++ tests/aot/namespace/nested-namespace.phpt | 38 +++++++++ tests/aot/namespace/self-static-cross-ns.phpt | 52 ++++++++++++ tests/aot/namespace/trait-across-ns.phpt | 67 ++++++++++++++++ tests/aot/namespace/use-alias.phpt | 35 ++++++++ tests/aot/namespace/use-const.phpt | 39 +++++++++ 13 files changed, 646 insertions(+) create mode 100644 tests/aot/namespace/anonymous-class-ns.phpt create mode 100644 tests/aot/namespace/class-const-ref.phpt create mode 100644 tests/aot/namespace/cross-ns-inheritance.phpt create mode 100644 tests/aot/namespace/fully-qualified.phpt create mode 100644 tests/aot/namespace/group-use.phpt create mode 100644 tests/aot/namespace/interface-across-ns.phpt create mode 100644 tests/aot/namespace/mixed-use-imports.phpt create mode 100644 tests/aot/namespace/namespace-constants.phpt create mode 100644 tests/aot/namespace/nested-namespace.phpt create mode 100644 tests/aot/namespace/self-static-cross-ns.phpt create mode 100644 tests/aot/namespace/trait-across-ns.phpt create mode 100644 tests/aot/namespace/use-alias.phpt create mode 100644 tests/aot/namespace/use-const.phpt diff --git a/tests/aot/namespace/anonymous-class-ns.phpt b/tests/aot/namespace/anonymous-class-ns.phpt new file mode 100644 index 00000000..3b188c8d --- /dev/null +++ b/tests/aot/namespace/anonymous-class-ns.phpt @@ -0,0 +1,35 @@ +--TEST-- +Anonymous class inside namespace +--FILE-- +prefix = $prefix; + } + public function greet(string $name): string { + return $this->prefix . " " . $name; + } + }; + } +} + +namespace { + use function App\Factory\createGreeter; + + function main() { + $g = createGreeter("Hello"); + var_dump($g->greet("World")); + echo "done\n"; + } +} +?> +--EXPECT-- +string(11) "Hello World" +done diff --git a/tests/aot/namespace/class-const-ref.phpt b/tests/aot/namespace/class-const-ref.phpt new file mode 100644 index 00000000..68cb2f95 --- /dev/null +++ b/tests/aot/namespace/class-const-ref.phpt @@ -0,0 +1,61 @@ +--TEST-- +Class constant references across namespaces +--FILE-- + +--EXPECT-- +string(7) "success" +string(9) "not found" +string(5) "error" +int(100) +int(30) +done diff --git a/tests/aot/namespace/cross-ns-inheritance.phpt b/tests/aot/namespace/cross-ns-inheritance.phpt new file mode 100644 index 00000000..a5d6e5cb --- /dev/null +++ b/tests/aot/namespace/cross-ns-inheritance.phpt @@ -0,0 +1,80 @@ +--TEST-- +Namespace with class inheritance across namespaces +--FILE-- +table = $table; + } + + public function getTable(): string { + return $this->table; + } + + abstract public function fields(): array; + } +} + +namespace App\Models { + use Base\Entity\Model; + + class User extends Model { + public function __construct() { + parent::__construct("users"); + } + + public function fields(): array { + return ["id", "name", "email"]; + } + } + + class Product extends Model { + public function __construct() { + parent::__construct("products"); + } + + public function fields(): array { + return ["id", "title", "price"]; + } + } +} + +namespace { + use App\Models\User; + use App\Models\Product; + + function main() { + $user = new User(); + $product = new Product(); + + var_dump($user->getTable()); + var_dump($user->fields()); + var_dump($product->getTable()); + var_dump($product->fields()); + echo "done\n"; + } +} +?> +--EXPECT-- +string(5) "users" +array(3) { + [0]=> + string(2) "id" + [1]=> + string(4) "name" + [2]=> + string(5) "email" +} +string(8) "products" +array(3) { + [0]=> + string(2) "id" + [1]=> + string(5) "title" + [2]=> + string(5) "price" +} +done diff --git a/tests/aot/namespace/fully-qualified.phpt b/tests/aot/namespace/fully-qualified.phpt new file mode 100644 index 00000000..6d83c83e --- /dev/null +++ b/tests/aot/namespace/fully-qualified.phpt @@ -0,0 +1,41 @@ +--TEST-- +Fully qualified names vs relative names in namespace +--FILE-- + +--EXPECT-- +string(5) "1.0.0" +string(14) "baz-in-foo-bar" +string(5) "1.0.0" +done diff --git a/tests/aot/namespace/group-use.phpt b/tests/aot/namespace/group-use.phpt new file mode 100644 index 00000000..5f4a0a1a --- /dev/null +++ b/tests/aot/namespace/group-use.phpt @@ -0,0 +1,46 @@ +--TEST-- +Group use imports for classes, functions, and constants +--FILE-- + +--EXPECT-- +string(6) "ClassA" +string(6) "ClassB" +string(6) "ClassC" +string(5) "funcA" +string(5) "funcB" +string(6) "constA" +string(6) "constB" +done diff --git a/tests/aot/namespace/interface-across-ns.phpt b/tests/aot/namespace/interface-across-ns.phpt new file mode 100644 index 00000000..b6217a68 --- /dev/null +++ b/tests/aot/namespace/interface-across-ns.phpt @@ -0,0 +1,58 @@ +--TEST-- +Namespace with interface and implementation across namespaces +--FILE-- +log[] = "paid:{$amount}"; + return true; + } + + public function refund(float $amount): bool { + $this->log[] = "refunded:{$amount}"; + return true; + } + + public function getLog(): array { + return $this->log; + } + } +} + +namespace { + use Services\Payment\StripeProcessor; + use Contracts\Payment\PaymentProcessor; + + function main() { + $p = new StripeProcessor(); + var_dump($p->pay(100.0)); + var_dump($p->refund(50.0)); + var_dump($p->getLog()); + var_dump($p instanceof PaymentProcessor); + echo "done\n"; + } +} +?> +--EXPECT-- +bool(true) +bool(true) +array(2) { + [0]=> + string(8) "paid:100" + [1]=> + string(11) "refunded:50" +} +bool(true) +done diff --git a/tests/aot/namespace/mixed-use-imports.phpt b/tests/aot/namespace/mixed-use-imports.phpt new file mode 100644 index 00000000..67775813 --- /dev/null +++ b/tests/aot/namespace/mixed-use-imports.phpt @@ -0,0 +1,51 @@ +--TEST-- +Mixed use class and use function imports +--FILE-- + +--EXPECT-- +string(15) "msg:hello-world" +int(42) +done diff --git a/tests/aot/namespace/namespace-constants.phpt b/tests/aot/namespace/namespace-constants.phpt new file mode 100644 index 00000000..20597662 --- /dev/null +++ b/tests/aot/namespace/namespace-constants.phpt @@ -0,0 +1,43 @@ +--TEST-- +Namespace with constants defined via const keyword +--FILE-- + APP_NAME, + "version" => VERSION, + "debug" => DEBUG, + ]; + } +} + +namespace { + use function Config\App\getConfig; + + function main() { + var_dump(\Config\App\APP_NAME); + var_dump(\Config\App\VERSION); + var_dump(\Config\App\DEBUG); + var_dump(getConfig()); + echo "done\n"; + } +} +?> +--EXPECT-- +string(5) "MyApp" +string(5) "2.0.0" +bool(true) +array(3) { + ["name"]=> + string(5) "MyApp" + ["version"]=> + string(5) "2.0.0" + ["debug"]=> + bool(true) +} +done diff --git a/tests/aot/namespace/nested-namespace.phpt b/tests/aot/namespace/nested-namespace.phpt new file mode 100644 index 00000000..6c7dc31f --- /dev/null +++ b/tests/aot/namespace/nested-namespace.phpt @@ -0,0 +1,38 @@ +--TEST-- +Multi-level nested namespaces with classes and functions +--FILE-- + +--EXPECT-- +string(14) "processed:sale" +int(5) +string(15) "processed:order" +done diff --git a/tests/aot/namespace/self-static-cross-ns.phpt b/tests/aot/namespace/self-static-cross-ns.phpt new file mode 100644 index 00000000..6706be8f --- /dev/null +++ b/tests/aot/namespace/self-static-cross-ns.phpt @@ -0,0 +1,52 @@ +--TEST-- +self/static resolution with inheritance across namespaces +--FILE-- + +--EXPECT-- +string(4) "user" +string(5) "admin" +string(4) "base" +string(4) "base" +done diff --git a/tests/aot/namespace/trait-across-ns.phpt b/tests/aot/namespace/trait-across-ns.phpt new file mode 100644 index 00000000..6a03538e --- /dev/null +++ b/tests/aot/namespace/trait-across-ns.phpt @@ -0,0 +1,67 @@ +--TEST-- +Trait used across namespaces +--FILE-- +log[] = $msg; + } + + public function getLog(): array { + return $this->log; + } + } +} + +namespace App\Services { + use Base\Traits\Logger; + + class OrderService { + use Logger; + + public function createOrder(int $id): string { + $this->log("order:{$id}"); + return "ok"; + } + } + + class UserService { + use Logger; + + public function createUser(string $name): string { + $this->log("user:{$name}"); + return "ok"; + } + } +} + +namespace { + use App\Services\OrderService; + use App\Services\UserService; + + function main() { + $os = new OrderService(); + $us = new UserService(); + + $os->createOrder(42); + $us->createUser("alice"); + + var_dump($os->getLog()); + var_dump($us->getLog()); + echo "done\n"; + } +} +?> +--EXPECT-- +array(1) { + [0]=> + string(8) "order:42" +} +array(1) { + [0]=> + string(10) "user:alice" +} +done diff --git a/tests/aot/namespace/use-alias.phpt b/tests/aot/namespace/use-alias.phpt new file mode 100644 index 00000000..af1b885d --- /dev/null +++ b/tests/aot/namespace/use-alias.phpt @@ -0,0 +1,35 @@ +--TEST-- +use alias for class names +--FILE-- +read()); + var_dump($mem->read()); + echo "done\n"; + } +} +?> +--EXPECT-- +string(9) "file:data" +string(11) "memory:data" +done diff --git a/tests/aot/namespace/use-const.phpt b/tests/aot/namespace/use-const.phpt new file mode 100644 index 00000000..be9fd972 --- /dev/null +++ b/tests/aot/namespace/use-const.phpt @@ -0,0 +1,39 @@ +--TEST-- +use const for importing namespace constants +--FILE-- + +--EXPECT-- +string(14) "localhost:8080" +bool(true) +done