summaryrefslogtreecommitdiff
path: root/Zend/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Zend/tests')
-rw-r--r--Zend/tests/ctor_promotion_abstract.phpt12
-rw-r--r--Zend/tests/ctor_promotion_additional_modifiers.phpt12
-rw-r--r--Zend/tests/ctor_promotion_attributes.phpt22
-rw-r--r--Zend/tests/ctor_promotion_basic.phpt21
-rw-r--r--Zend/tests/ctor_promotion_by_ref.phpt20
-rw-r--r--Zend/tests/ctor_promotion_callable_type.phpt12
-rw-r--r--Zend/tests/ctor_promotion_defaults.phpt43
-rw-r--r--Zend/tests/ctor_promotion_free_function.phpt10
-rw-r--r--Zend/tests/ctor_promotion_interface.phpt12
-rw-r--r--Zend/tests/ctor_promotion_mixing.phpt54
-rw-r--r--Zend/tests/ctor_promotion_not_a_ctor.phpt12
-rw-r--r--Zend/tests/ctor_promotion_null_default.phpt12
-rw-r--r--Zend/tests/ctor_promotion_repeated_prop.phpt14
-rw-r--r--Zend/tests/ctor_promotion_trait.phpt21
-rw-r--r--Zend/tests/ctor_promotion_variadic.phpt12
15 files changed, 289 insertions, 0 deletions
diff --git a/Zend/tests/ctor_promotion_abstract.phpt b/Zend/tests/ctor_promotion_abstract.phpt
new file mode 100644
index 0000000000..937e247acd
--- /dev/null
+++ b/Zend/tests/ctor_promotion_abstract.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Constructor promotion cannot be used inside an abstract constructor
+--FILE--
+<?php
+
+abstract class Test {
+ abstract public function __construct(public int $x);
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot declare promoted property in an abstract constructor in %s on line %d
diff --git a/Zend/tests/ctor_promotion_additional_modifiers.phpt b/Zend/tests/ctor_promotion_additional_modifiers.phpt
new file mode 100644
index 0000000000..fb3b092d66
--- /dev/null
+++ b/Zend/tests/ctor_promotion_additional_modifiers.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Constructor promotion only permits visibility modifiers
+--FILE--
+<?php
+
+class Test {
+ public function __construct(public static $x) {}
+}
+
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected 'static' (T_STATIC), expecting variable (T_VARIABLE) in %s on line %d
diff --git a/Zend/tests/ctor_promotion_attributes.phpt b/Zend/tests/ctor_promotion_attributes.phpt
new file mode 100644
index 0000000000..d85d8d3181
--- /dev/null
+++ b/Zend/tests/ctor_promotion_attributes.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Attributes on promoted properties are assigned to both the property and parameter
+--FILE--
+<?php
+
+class Test {
+ public function __construct(
+ <<NonNegative>>
+ public int $num,
+ ) {}
+}
+
+$prop = new ReflectionProperty(Test::class, 'num');
+var_dump($prop->getAttributes()[0]->getName());
+
+$param = new ReflectionParameter([Test::class, '__construct'], 'num');
+var_dump($param->getAttributes()[0]->getName());
+
+?>
+--EXPECT--
+string(11) "NonNegative"
+string(11) "NonNegative"
diff --git a/Zend/tests/ctor_promotion_basic.phpt b/Zend/tests/ctor_promotion_basic.phpt
new file mode 100644
index 0000000000..206f99fd40
--- /dev/null
+++ b/Zend/tests/ctor_promotion_basic.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Constructor promotion (basic example)
+--FILE--
+<?php
+
+class Point {
+ public function __construct(public int $x, public int $y, public int $z) {}
+}
+
+$point = new Point(1, 2, 3);
+
+// Check that properties really are typed.
+try {
+ $point->x = "foo";
+} catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+}
+
+?>
+--EXPECT--
+Cannot assign string to property Point::$x of type int
diff --git a/Zend/tests/ctor_promotion_by_ref.phpt b/Zend/tests/ctor_promotion_by_ref.phpt
new file mode 100644
index 0000000000..4b07149c57
--- /dev/null
+++ b/Zend/tests/ctor_promotion_by_ref.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Constructor promotion of by-ref parameter
+--FILE--
+<?php
+
+class Ary {
+ public function __construct(public array &$array) {}
+}
+
+$array = [];
+$ary = new Ary($array);
+$array[] = 42;
+var_dump($ary->array);
+
+?>
+--EXPECT--
+array(1) {
+ [0]=>
+ int(42)
+}
diff --git a/Zend/tests/ctor_promotion_callable_type.phpt b/Zend/tests/ctor_promotion_callable_type.phpt
new file mode 100644
index 0000000000..ae10512ad5
--- /dev/null
+++ b/Zend/tests/ctor_promotion_callable_type.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Type of promoted property may not be callable
+--FILE--
+<?php
+
+class Test {
+ public function __construct(public callable $callable) {}
+}
+
+?>
+--EXPECTF--
+Fatal error: Property Test::$callable cannot have type callable in %s on line %d
diff --git a/Zend/tests/ctor_promotion_defaults.phpt b/Zend/tests/ctor_promotion_defaults.phpt
new file mode 100644
index 0000000000..9999e8a539
--- /dev/null
+++ b/Zend/tests/ctor_promotion_defaults.phpt
@@ -0,0 +1,43 @@
+--TEST--
+Constructor promotion with default values
+--FILE--
+<?php
+
+class Point {
+ public function __construct(
+ public float $x = 0.0,
+ public float $y = 1.0,
+ public float $z = 2.0
+ ) {}
+}
+
+var_dump(new Point(10.0));
+var_dump(new Point(10.0, 11.0));
+var_dump(new Point(10.0, 11.0, 12.0));
+
+?>
+--EXPECT--
+object(Point)#1 (3) {
+ ["x"]=>
+ float(10)
+ ["y"]=>
+ float(1)
+ ["z"]=>
+ float(2)
+}
+object(Point)#1 (3) {
+ ["x"]=>
+ float(10)
+ ["y"]=>
+ float(11)
+ ["z"]=>
+ float(2)
+}
+object(Point)#1 (3) {
+ ["x"]=>
+ float(10)
+ ["y"]=>
+ float(11)
+ ["z"]=>
+ float(12)
+}
diff --git a/Zend/tests/ctor_promotion_free_function.phpt b/Zend/tests/ctor_promotion_free_function.phpt
new file mode 100644
index 0000000000..1eb84c5517
--- /dev/null
+++ b/Zend/tests/ctor_promotion_free_function.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Constructor promotion cannot be used in a free function
+--FILE--
+<?php
+
+function __construct(public $prop) {}
+
+?>
+--EXPECTF--
+Fatal error: Cannot declare promoted property outside a constructor in %s on line %d
diff --git a/Zend/tests/ctor_promotion_interface.phpt b/Zend/tests/ctor_promotion_interface.phpt
new file mode 100644
index 0000000000..7cc856b93a
--- /dev/null
+++ b/Zend/tests/ctor_promotion_interface.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Constructor promotion cannot be used inside an abstract constructor (interface variant)
+--FILE--
+<?php
+
+interface Test {
+ public function __construct(public int $x);
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot declare promoted property in an abstract constructor in %s on line %d
diff --git a/Zend/tests/ctor_promotion_mixing.phpt b/Zend/tests/ctor_promotion_mixing.phpt
new file mode 100644
index 0000000000..a527654e79
--- /dev/null
+++ b/Zend/tests/ctor_promotion_mixing.phpt
@@ -0,0 +1,54 @@
+--TEST--
+Constructor promotiong mixed with other properties, parameters and code
+--FILE--
+<?php
+
+class Test {
+ public string $prop2;
+
+ public function __construct(public string $prop1 = "", $param2 = "") {
+ $this->prop2 = $prop1 . $param2;
+ }
+}
+
+var_dump(new Test("Foo", "Bar"));
+echo "\n";
+echo new ReflectionClass(Test::class), "\n";
+
+?>
+--EXPECTF--
+object(Test)#1 (2) {
+ ["prop2"]=>
+ string(6) "FooBar"
+ ["prop1"]=>
+ string(3) "Foo"
+}
+
+Class [ <user> class Test ] {
+ @@ %s
+
+ - Constants [0] {
+ }
+
+ - Static properties [0] {
+ }
+
+ - Static methods [0] {
+ }
+
+ - Properties [2] {
+ Property [ public string $prop2 ]
+ Property [ public string $prop1 ]
+ }
+
+ - Methods [1] {
+ Method [ <user, ctor> public method __construct ] {
+ @@ %s
+
+ - Parameters [2] {
+ Parameter #0 [ <optional> string $prop1 = '' ]
+ Parameter #1 [ <optional> $param2 = '' ]
+ }
+ }
+ }
+}
diff --git a/Zend/tests/ctor_promotion_not_a_ctor.phpt b/Zend/tests/ctor_promotion_not_a_ctor.phpt
new file mode 100644
index 0000000000..110ee8a5ce
--- /dev/null
+++ b/Zend/tests/ctor_promotion_not_a_ctor.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Constructor promotion can only be used in constructors ... duh
+--FILE--
+<?php
+
+class Test {
+ public function foobar(public int $x, public int $y) {}
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot declare promoted property outside a constructor in %s on line %d
diff --git a/Zend/tests/ctor_promotion_null_default.phpt b/Zend/tests/ctor_promotion_null_default.phpt
new file mode 100644
index 0000000000..61c7b2d10c
--- /dev/null
+++ b/Zend/tests/ctor_promotion_null_default.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Constructor promotion with null default, requires an explicitly nullable type
+--FILE--
+<?php
+
+class Test {
+ public function __construct(public int $x = null) {}
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot use null as default value for parameter $x of type int in %s on line %d
diff --git a/Zend/tests/ctor_promotion_repeated_prop.phpt b/Zend/tests/ctor_promotion_repeated_prop.phpt
new file mode 100644
index 0000000000..6f4a9ffd02
--- /dev/null
+++ b/Zend/tests/ctor_promotion_repeated_prop.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Clash between promoted and explicit property
+--FILE--
+<?php
+
+class Test {
+ public $prop;
+
+ public function __construct(public $prop) {}
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot redeclare Test::$prop in %s on line %d
diff --git a/Zend/tests/ctor_promotion_trait.phpt b/Zend/tests/ctor_promotion_trait.phpt
new file mode 100644
index 0000000000..4c109157ae
--- /dev/null
+++ b/Zend/tests/ctor_promotion_trait.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Constructor promotion can be used inside a trait
+--FILE--
+<?php
+
+trait Test {
+ public function __construct(public $prop) {}
+}
+
+class Test2 {
+ use Test;
+}
+
+var_dump(new Test2(42));
+
+?>
+--EXPECT--
+object(Test2)#1 (1) {
+ ["prop"]=>
+ int(42)
+}
diff --git a/Zend/tests/ctor_promotion_variadic.phpt b/Zend/tests/ctor_promotion_variadic.phpt
new file mode 100644
index 0000000000..7e1e81cad8
--- /dev/null
+++ b/Zend/tests/ctor_promotion_variadic.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Cannot use constructor promotion with variadic parameter
+--FILE--
+<?php
+
+class Test {
+ public function __construct(public string ...$strings) {}
+}
+
+?>
+--EXPECTF--
+Fatal error: Cannot declare variadic promoted property in %s on line %d