Skip to content

Commit 2af82bf

Browse files
committed
Add tests for attributes
1 parent 93c9527 commit 2af82bf

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
new in attribute arguments
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
class MyAttribute {
8+
public function __construct(public $x, public $y) {}
9+
}
10+
11+
#[MyAttribute(null, new stdClass)]
12+
class Test1 {
13+
}
14+
15+
$rc = new ReflectionClass(Test1::class);
16+
$ra = $rc->getAttributes()[0];
17+
$args1 = $ra->getArguments();
18+
$obj1 = $ra->newInstance();
19+
var_dump($args1, $obj1);
20+
21+
// Check that we get fresh instances each time:
22+
$args2 = $ra->getArguments();
23+
$obj2 = $ra->newInstance();
24+
var_dump($args1[1] !== $args2[1]);
25+
var_dump($obj1->y !== $obj2->y);
26+
27+
// Check that named args work:
28+
#[MyAttribute(y: new stdClass, x: null)]
29+
class Test2 {
30+
}
31+
32+
$rc = new ReflectionClass(Test2::class);
33+
$ra = $rc->getAttributes()[0];
34+
$args = $ra->getArguments();
35+
$obj = $ra->newInstance();
36+
var_dump($args, $obj);
37+
38+
?>
39+
--EXPECT--
40+
array(2) {
41+
[0]=>
42+
NULL
43+
[1]=>
44+
object(stdClass)#3 (0) {
45+
}
46+
}
47+
object(MyAttribute)#4 (2) {
48+
["x"]=>
49+
NULL
50+
["y"]=>
51+
object(stdClass)#5 (0) {
52+
}
53+
}
54+
bool(true)
55+
bool(true)
56+
array(2) {
57+
["y"]=>
58+
object(stdClass)#2 (0) {
59+
}
60+
["x"]=>
61+
NULL
62+
}
63+
object(MyAttribute)#10 (2) {
64+
["x"]=>
65+
NULL
66+
["y"]=>
67+
object(stdClass)#11 (0) {
68+
}
69+
}

0 commit comments

Comments
 (0)