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