Skip to content

Commit e46f32f

Browse files
Expand test cases
1 parent 6892c2a commit e46f32f

6 files changed

+124
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Constants listed in valid targets when used wrong (internal attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Deprecated]
7+
class Example {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Attribute "Deprecated" cannot target class (allowed targets: function, method, class constant, constant) in %s on line %d
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Constants listed in valid targets when used wrong (userland attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_CONSTANT)]
7+
class MyConstantAttribute {}
8+
9+
#[MyConstantAttribute]
10+
class Example {}
11+
12+
$ref = new ReflectionClass(Example::class);
13+
$attribs = $ref->getAttributes();
14+
var_dump($attribs);
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#2 (1) {
22+
["name"]=>
23+
string(19) "MyConstantAttribute"
24+
}
25+
}
26+
27+
Fatal error: Uncaught Error: Attribute "MyConstantAttribute" cannot target class (allowed targets: constant) in %s:%d
28+
Stack trace:
29+
#0 %s(%d): ReflectionAttribute->newInstance()
30+
#1 {main}
31+
thrown in %s on line %d
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Multiple attributes in a group are allowed
3+
--FILE--
4+
<?php
5+
6+
#[\Foo, \Bar]
7+
const CONSTANT = 1;
8+
9+
$ref = new ReflectionConstant('CONSTANT');
10+
var_dump($ref->getAttributes());
11+
12+
?>
13+
--EXPECTF--
14+
array(2) {
15+
[0]=>
16+
object(ReflectionAttribute)#%d (1) {
17+
["name"]=>
18+
string(3) "Foo"
19+
}
20+
[1]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(3) "Bar"
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Multiple attributes in separate groups are allowed
3+
--FILE--
4+
<?php
5+
6+
#[\Foo]
7+
#[\Bar]
8+
const CONSTANT = 1;
9+
10+
$ref = new ReflectionConstant('CONSTANT');
11+
var_dump($ref->getAttributes());
12+
13+
?>
14+
--EXPECTF--
15+
array(2) {
16+
[0]=>
17+
object(ReflectionAttribute)#%d (1) {
18+
["name"]=>
19+
string(3) "Foo"
20+
}
21+
[1]=>
22+
object(ReflectionAttribute)#%d (1) {
23+
["name"]=>
24+
string(3) "Bar"
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Error when attribute does not target constants
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_FUNCTION)]
7+
class MyFunctionAttribute {}
8+
9+
#[MyFunctionAttribute]
10+
const EXAMPLE = 'Foo';
11+
12+
$ref = new ReflectionConstant('EXAMPLE');
13+
$attribs = $ref->getAttributes();
14+
var_dump( $attribs );
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(19) "MyFunctionAttribute"
24+
}
25+
}
26+
27+
Fatal error: Uncaught Error: Attribute "MyFunctionAttribute" cannot target constant (allowed targets: function) in %s:%d
28+
Stack trace:
29+
#0 %s(%d): ReflectionAttribute->newInstance()
30+
#1 {main}
31+
thrown in %s on line %d

0 commit comments

Comments
 (0)