Skip to content

Commit 53371a5

Browse files
committed
Add tests for attributes on all elements.
1 parent 603a4ac commit 53371a5

File tree

1 file changed

+83
-90
lines changed

1 file changed

+83
-90
lines changed

Zend/tests/attributes_001.phpt

Lines changed: 83 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,168 @@
11
--TEST--
2-
Basic attributes usage
2+
Basic attributes usage on all elements
33
--FILE--
44
<?php
5-
function dump_attributes($attributes) {
5+
class Attr {
6+
public $element;
7+
8+
public function __construct(string $element) {
9+
$this->element = $element;
10+
}
11+
}
12+
class OtherAttr {}
13+
14+
function dump_attributes($reflection) {
615
$arr = [];
7-
foreach ($attributes as $attribute) {
16+
foreach ($reflection->getAttributes() as $attribute) {
817
if (!isset($arr[$attribute->getName()])) {
918
$arr[$attribute->getName()] = [];
1019
}
1120
$arr[$attribute->getName()][] = $attribute->getArguments();
1221
}
1322
var_dump($arr);
1423
}
15-
// No attributes
16-
function f0() {
17-
}
18-
$r = new ReflectionFunction("f0");
19-
dump_attributes($r->getAttributes());
2024

21-
// Function attributes
22-
<<TestFunction>>
23-
function foo() {
24-
}
25-
$r = new ReflectionFunction("foo");
26-
dump_attributes($r->getAttributes());
25+
function f0() {}
26+
dump_attributes(new ReflectionFunction("f0"));
27+
28+
<<Attr("function")>>
29+
function foo() {}
30+
dump_attributes(new ReflectionFunction("foo"));
2731

2832
// Class attributes
29-
<<TestClass>>
33+
<<Attr("class")>>
3034
class Bar {
31-
<<TestConst>>
35+
<<Attr("const")>>
3236
const C = 2;
33-
<<TestProp>>
37+
<<Attr("property")>>
3438
public $x = 3;
3539

3640
}
37-
$r = new ReflectionClass("Bar");
38-
dump_attributes($r->getAttributes());
39-
$r1 = $r->getReflectionConstant("C");
40-
dump_attributes($r1->getAttributes());
41-
$r2 = $r->getProperty("x");
42-
dump_attributes($r2->getAttributes());
41+
$reflectionClass = new ReflectionClass("Bar");
42+
dump_attributes($reflectionClass);
43+
dump_attributes($reflectionClass->getReflectionConstant("C"));
44+
dump_attributes($reflectionClass->getProperty("x"));
45+
46+
$foo = <<Attr("closure")>>function () {};
47+
dump_attributes(new ReflectionFunction($foo));
48+
49+
$foo = <<Attr("short closure")>>fn ($x) => $x;
50+
dump_attributes(new ReflectionFunction($foo));
51+
52+
$anonClass = new <<Attr("anon class")>>class {};
53+
dump_attributes(new ReflectionClass($anonClass));
4354

4455
// Multiple attributes with multiple values
45-
<<a1>>
46-
<<a2(1)>>
47-
<<a3(1,2)>>
56+
<<Attr()>>
57+
<<OtherAttr()>>
4858
function f1() {}
49-
$r = new ReflectionFunction("f1");
50-
dump_attributes($r->getAttributes());
59+
dump_attributes(new ReflectionFunction("f1"));
5160

52-
// Attributes with AST
53-
<<a1>>
54-
<<a2(1+1)>>
55-
<<a3(1+3,2+2)>>
56-
<<a4(["a"=>1,"b"=>2])>>
61+
// multiple instances of same attribute
62+
<<Attr("foo")>>
63+
<<Attr("bar")>>
5764
function f2() {}
58-
$r = new ReflectionFunction("f2");
59-
dump_attributes($r->getAttributes());
60-
61-
// Attributes with namespaces
62-
<<Foo\Bar>>
63-
function f4() {
64-
}
65-
$r = new ReflectionFunction("f4");
66-
dump_attributes($r->getAttributes());
65+
dump_attributes(new ReflectionFunction("f2"));
6766
?>
6867
--EXPECT--
6968
array(0) {
7069
}
7170
array(1) {
72-
["TestFunction"]=>
71+
["Attr"]=>
7372
array(1) {
7473
[0]=>
75-
array(0) {
74+
array(1) {
75+
[0]=>
76+
string(8) "function"
7677
}
7778
}
7879
}
7980
array(1) {
80-
["TestClass"]=>
81+
["Attr"]=>
8182
array(1) {
8283
[0]=>
83-
array(0) {
84+
array(1) {
85+
[0]=>
86+
string(5) "class"
8487
}
8588
}
8689
}
8790
array(1) {
88-
["TestConst"]=>
91+
["Attr"]=>
8992
array(1) {
9093
[0]=>
91-
array(0) {
94+
array(1) {
95+
[0]=>
96+
string(5) "const"
9297
}
9398
}
9499
}
95100
array(1) {
96-
["TestProp"]=>
101+
["Attr"]=>
97102
array(1) {
98103
[0]=>
99-
array(0) {
104+
array(1) {
105+
[0]=>
106+
string(8) "property"
100107
}
101108
}
102109
}
103-
array(3) {
104-
["a1"]=>
110+
array(1) {
111+
["Attr"]=>
105112
array(1) {
106113
[0]=>
107-
array(0) {
114+
array(1) {
115+
[0]=>
116+
string(7) "closure"
108117
}
109118
}
110-
["a2"]=>
119+
}
120+
array(1) {
121+
["Attr"]=>
111122
array(1) {
112123
[0]=>
113124
array(1) {
114125
[0]=>
115-
int(1)
126+
string(13) "short closure"
116127
}
117128
}
118-
["a3"]=>
129+
}
130+
array(1) {
131+
["Attr"]=>
119132
array(1) {
120133
[0]=>
121-
array(2) {
134+
array(1) {
122135
[0]=>
123-
int(1)
124-
[1]=>
125-
int(2)
136+
string(10) "anon class"
126137
}
127138
}
128139
}
129-
array(4) {
130-
["a1"]=>
140+
array(2) {
141+
["Attr"]=>
131142
array(1) {
132143
[0]=>
133144
array(0) {
134145
}
135146
}
136-
["a2"]=>
147+
["OtherAttr"]=>
137148
array(1) {
138149
[0]=>
139-
array(1) {
140-
[0]=>
141-
int(2)
150+
array(0) {
142151
}
143152
}
144-
["a3"]=>
145-
array(1) {
153+
}
154+
array(1) {
155+
["Attr"]=>
156+
array(2) {
146157
[0]=>
147-
array(2) {
158+
array(1) {
148159
[0]=>
149-
int(4)
150-
[1]=>
151-
int(4)
160+
string(3) "foo"
152161
}
153-
}
154-
["a4"]=>
155-
array(1) {
156-
[0]=>
162+
[1]=>
157163
array(1) {
158164
[0]=>
159-
array(2) {
160-
["a"]=>
161-
int(1)
162-
["b"]=>
163-
int(2)
164-
}
165-
}
166-
}
167-
}
168-
array(1) {
169-
["Foo\Bar"]=>
170-
array(1) {
171-
[0]=>
172-
array(0) {
165+
string(3) "bar"
173166
}
174167
}
175168
}

0 commit comments

Comments
 (0)