Skip to content

Commit

Permalink
Test repeating attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielEScherzer committed Dec 10, 2024
1 parent 44aa58c commit e8f731b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Zend/tests/attributes/constants/not_repeatable-internal.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Validation of attribute repetition (not allowed; internal attribute)
--FILE--
<?php

#[Deprecated]
#[Deprecated]
const MY_CONST = true;

?>
--EXPECTF--
Fatal error: Attribute "Deprecated" must not be repeated in %s on line %d
36 changes: 36 additions & 0 deletions Zend/tests/attributes/constants/not_repeatable-userland.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Validation of attribute repetition (not allowed; userland attribute)
--FILE--
<?php

#[Attribute]
class MyAttribute {}

#[MyAttribute]
#[MyAttribute]
const MY_CONST = true;

$attributes = new ReflectionConstant( 'MY_CONST' )->getAttributes();
var_dump( $attributes );
$attributes[0]->newInstance();

?>
--EXPECTF--
array(2) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(11) "MyAttribute"
}
[1]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(11) "MyAttribute"
}
}

Fatal error: Uncaught Error: Attribute "MyAttribute" must not be repeated in %s:%d
Stack trace:
#0 %s(%d): ReflectionAttribute->newInstance()
#1 {main}
thrown in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/attributes/constants/yes_repeatable-internal.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Validation of attribute repetition (is allowed; internal attribute)
--EXTENSIONS--
zend_test
--FILE--
<?php

#[ZendTestRepeatableAttribute]
#[ZendTestRepeatableAttribute]
const MY_CONST = true;

echo "Done\n";

?>
--EXPECT--
Done
30 changes: 30 additions & 0 deletions Zend/tests/attributes/constants/yes_repeatable-userland.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Validation of attribute repetition (is allowed; userland attribute)
--FILE--
<?php

#[Attribute(Attribute::TARGET_ALL|Attribute::IS_REPEATABLE)]
class MyAttribute {}

#[MyAttribute]
#[MyAttribute]
const MY_CONST = true;

$attributes = new ReflectionConstant( 'MY_CONST' )->getAttributes();
var_dump( $attributes );
$attributes[0]->newInstance();

?>
--EXPECTF--
array(2) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(11) "MyAttribute"
}
[1]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(11) "MyAttribute"
}
}

0 comments on commit e8f731b

Please sign in to comment.