diff options
| author | Martin Schröder <m.schroeder2007@gmail.com> | 2020-06-28 19:16:33 +0200 |
|---|---|---|
| committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-06-29 10:45:51 +0200 |
| commit | 053ef28b8d0c1e2af2cc5151cfe2bd4369ddde34 (patch) | |
| tree | 54cebb4dfb10fae058a89f086f914ba93e16d254 /Zend/tests/attributes | |
| parent | 46e38a192751be02ff95d56fb0c6c18ec4d7d6df (diff) | |
| download | php-git-053ef28b8d0c1e2af2cc5151cfe2bd4369ddde34.tar.gz | |
Implement Attribute Amendments.
RFC: https://wiki.php.net/rfc/attribute_amendments
Support for attribute grouping is left out, because the short
attribute syntax RFC will likely make it obsolete.
Closes GH-5751.
Diffstat (limited to 'Zend/tests/attributes')
| -rw-r--r-- | Zend/tests/attributes/002_rfcexample.phpt | 6 | ||||
| -rw-r--r-- | Zend/tests/attributes/003_ast_nodes.phpt | 2 | ||||
| -rw-r--r-- | Zend/tests/attributes/005_objects.phpt | 8 | ||||
| -rw-r--r-- | Zend/tests/attributes/007_self_reflect_attribute.phpt | 15 | ||||
| -rw-r--r-- | Zend/tests/attributes/008_wrong_attribution.phpt | 6 | ||||
| -rw-r--r-- | Zend/tests/attributes/019_variable_attribute_name.phpt (renamed from Zend/tests/attributes/018_variable_attribute_name.phpt) | 0 | ||||
| -rw-r--r-- | Zend/tests/attributes/020_userland_attribute_validation.phpt | 70 | ||||
| -rw-r--r-- | Zend/tests/attributes/021_attribute_flags_type_is_validated.phpt | 11 | ||||
| -rw-r--r-- | Zend/tests/attributes/022_attribute_flags_value_is_validated.phpt | 11 | ||||
| -rw-r--r-- | Zend/tests/attributes/023_ast_node_in_validation.phpt | 11 | ||||
| -rw-r--r-- | Zend/tests/attributes/024_internal_target_validation.phpt | 11 | ||||
| -rw-r--r-- | Zend/tests/attributes/025_internal_repeatable_validation.phpt | 12 |
12 files changed, 146 insertions, 17 deletions
diff --git a/Zend/tests/attributes/002_rfcexample.phpt b/Zend/tests/attributes/002_rfcexample.phpt index 0d3879877e..6d1028436d 100644 --- a/Zend/tests/attributes/002_rfcexample.phpt +++ b/Zend/tests/attributes/002_rfcexample.phpt @@ -4,9 +4,9 @@ Attributes: Example from Attributes RFC <?php // https://wiki.php.net/rfc/attributes_v2#attribute_syntax namespace My\Attributes { - use PhpAttribute; + use Attribute; - <<PhpAttribute>> + <<Attribute>> class SingleArgument { public $argumentValue; @@ -37,7 +37,7 @@ array(1) { [0]=> string(11) "Hello World" } -object(My\Attributes\SingleArgument)#3 (1) { +object(My\Attributes\SingleArgument)#%d (1) { ["argumentValue"]=> string(11) "Hello World" } diff --git a/Zend/tests/attributes/003_ast_nodes.phpt b/Zend/tests/attributes/003_ast_nodes.phpt index cf43e663d5..5bfffb8100 100644 --- a/Zend/tests/attributes/003_ast_nodes.phpt +++ b/Zend/tests/attributes/003_ast_nodes.phpt @@ -59,7 +59,7 @@ var_dump($ref->getAttributes()[0]->getArguments()); echo "\n"; -<<PhpAttribute>> +<<Attribute>> class C5 { public function __construct() { } diff --git a/Zend/tests/attributes/005_objects.phpt b/Zend/tests/attributes/005_objects.phpt index baf51af775..f213ed54b6 100644 --- a/Zend/tests/attributes/005_objects.phpt +++ b/Zend/tests/attributes/005_objects.phpt @@ -3,7 +3,7 @@ Attributes can be converted into objects. --FILE-- <?php -<<PhpAttribute>> +<<Attribute(Attribute::TARGET_FUNCTION)>> class A1 { public string $name; @@ -56,7 +56,7 @@ try { echo "\n"; -<<PhpAttribute>> +<<Attribute>> class A3 { private function __construct() { } @@ -72,7 +72,7 @@ try { echo "\n"; -<<PhpAttribute>> +<<Attribute>> class A4 { } $ref = new \ReflectionFunction(<<A4(1)>> function () { }); @@ -117,4 +117,4 @@ string(7) "ERROR 5" string(71) "Attribute class 'A4' does not have a constructor, cannot pass arguments" string(7) "ERROR 6" -string(78) "Attempting to use class 'A5' as attribute that does not have <<PhpAttribute>>." +string(55) "Attempting to use non-attribute class 'A5' as attribute" diff --git a/Zend/tests/attributes/007_self_reflect_attribute.phpt b/Zend/tests/attributes/007_self_reflect_attribute.phpt index ae19665dcb..ec9ee66f20 100644 --- a/Zend/tests/attributes/007_self_reflect_attribute.phpt +++ b/Zend/tests/attributes/007_self_reflect_attribute.phpt @@ -1,19 +1,22 @@ --TEST-- -Attributes: attributes on PhpAttribute return itself +Attributes: attributes on Attribute return itself --FILE-- <?php -$reflection = new \ReflectionClass(PhpAttribute::class); +$reflection = new \ReflectionClass(Attribute::class); $attributes = $reflection->getAttributes(); foreach ($attributes as $attribute) { var_dump($attribute->getName()); var_dump($attribute->getArguments()); - var_dump($attribute->newInstance()); + + $a = $attribute->newInstance(); + var_dump(get_class($a)); + var_dump($a->flags == Attribute::TARGET_ALL); } --EXPECTF-- -string(12) "PhpAttribute" +string(9) "Attribute" array(0) { } -object(PhpAttribute)#3 (0) { -} +string(9) "Attribute" +bool(true) diff --git a/Zend/tests/attributes/008_wrong_attribution.phpt b/Zend/tests/attributes/008_wrong_attribution.phpt index dcb0b6b51d..20a800b9a7 100644 --- a/Zend/tests/attributes/008_wrong_attribution.phpt +++ b/Zend/tests/attributes/008_wrong_attribution.phpt @@ -1,9 +1,9 @@ --TEST-- -Attributes: Prevent PhpAttribute on non classes +Attributes: Prevent Attribute on non classes --FILE-- <?php -<<PhpAttribute>> +<<Attribute>> function foo() {} --EXPECTF-- -Fatal error: Only classes can be marked with <<PhpAttribute>> in %s +Fatal error: Attribute "Attribute" cannot target function (allowed targets: class) in %s diff --git a/Zend/tests/attributes/018_variable_attribute_name.phpt b/Zend/tests/attributes/019_variable_attribute_name.phpt index 64fb69a4e0..64fb69a4e0 100644 --- a/Zend/tests/attributes/018_variable_attribute_name.phpt +++ b/Zend/tests/attributes/019_variable_attribute_name.phpt diff --git a/Zend/tests/attributes/020_userland_attribute_validation.phpt b/Zend/tests/attributes/020_userland_attribute_validation.phpt new file mode 100644 index 0000000000..48c5e2651b --- /dev/null +++ b/Zend/tests/attributes/020_userland_attribute_validation.phpt @@ -0,0 +1,70 @@ +--TEST-- +Attributes expose and verify target and repeatable data. +--FILE-- +<?php + +<<Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)>> +class A1 { } + +$ref = new \ReflectionFunction(<<A1>> function () { }); +$attr = $ref->getAttributes()[0]; +var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated()); +var_dump(get_class($attr->newInstance())); + +echo "\n"; + +$ref = new \ReflectionObject(new <<A1>> class() { }); +$attr = $ref->getAttributes()[0]; +var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated()); + +try { + $attr->newInstance(); +} catch (\Throwable $e) { + var_dump('ERROR 1', $e->getMessage()); +} + +echo "\n"; + +$ref = new \ReflectionFunction(<<A1>> <<A1>> function () { }); +$attr = $ref->getAttributes()[0]; +var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated()); + +try { + $attr->newInstance(); +} catch (\Throwable $e) { + var_dump('ERROR 2', $e->getMessage()); +} + +echo "\n"; + +<<Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)>> +class A2 { } + +$ref = new \ReflectionObject(new <<A2>> <<A2>> class() { }); +$attr = $ref->getAttributes()[0]; +var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated()); +var_dump(get_class($attr->newInstance())); + +?> +--EXPECT-- +string(2) "A1" +bool(true) +bool(false) +string(2) "A1" + +string(2) "A1" +bool(true) +bool(false) +string(7) "ERROR 1" +string(70) "Attribute "A1" cannot target class (allowed targets: function, method)" + +string(2) "A1" +bool(true) +bool(true) +string(7) "ERROR 2" +string(35) "Attribute "A1" must not be repeated" + +string(2) "A2" +bool(true) +bool(true) +string(2) "A2" diff --git a/Zend/tests/attributes/021_attribute_flags_type_is_validated.phpt b/Zend/tests/attributes/021_attribute_flags_type_is_validated.phpt new file mode 100644 index 0000000000..06ed4d08fd --- /dev/null +++ b/Zend/tests/attributes/021_attribute_flags_type_is_validated.phpt @@ -0,0 +1,11 @@ +--TEST-- +Attribute flags type is validated. +--FILE-- +<?php + +<<Attribute("foo")>> +class A1 { } + +?> +--EXPECTF-- +Fatal error: Attribute::__construct(): Argument #1 ($flags) must must be of type int, string given in %s diff --git a/Zend/tests/attributes/022_attribute_flags_value_is_validated.phpt b/Zend/tests/attributes/022_attribute_flags_value_is_validated.phpt new file mode 100644 index 0000000000..1deb81e4d5 --- /dev/null +++ b/Zend/tests/attributes/022_attribute_flags_value_is_validated.phpt @@ -0,0 +1,11 @@ +--TEST-- +Attribute flags value is validated. +--FILE-- +<?php + +<<Attribute(-1)>> +class A1 { } + +?> +--EXPECTF-- +Fatal error: Invalid attribute flags specified in %s diff --git a/Zend/tests/attributes/023_ast_node_in_validation.phpt b/Zend/tests/attributes/023_ast_node_in_validation.phpt new file mode 100644 index 0000000000..af0d0b767d --- /dev/null +++ b/Zend/tests/attributes/023_ast_node_in_validation.phpt @@ -0,0 +1,11 @@ +--TEST-- +Attribute flags value is validated. +--FILE-- +<?php + +<<Attribute(Foo::BAR)>> +class A1 { } + +?> +--EXPECTF-- +Fatal error: Class 'Foo' not found in %s diff --git a/Zend/tests/attributes/024_internal_target_validation.phpt b/Zend/tests/attributes/024_internal_target_validation.phpt new file mode 100644 index 0000000000..746ceb3c69 --- /dev/null +++ b/Zend/tests/attributes/024_internal_target_validation.phpt @@ -0,0 +1,11 @@ +--TEST-- +Internal attribute targets are validated. +--FILE-- +<?php + +<<Attribute>> +function a1() { } + +?> +--EXPECTF-- +Fatal error: Attribute "Attribute" cannot target function (allowed targets: class) in %s diff --git a/Zend/tests/attributes/025_internal_repeatable_validation.phpt b/Zend/tests/attributes/025_internal_repeatable_validation.phpt new file mode 100644 index 0000000000..631f0b5054 --- /dev/null +++ b/Zend/tests/attributes/025_internal_repeatable_validation.phpt @@ -0,0 +1,12 @@ +--TEST-- +Internal attribute targets are validated. +--FILE-- +<?php + +<<Attribute>> +<<Attribute>> +class A1 { } + +?> +--EXPECTF-- +Fatal error: Attribute "Attribute" must not be repeated in %s |
