Skip to content

Commit ffaacd3

Browse files
committed
Move tests from ext/reflection to Zend/tests/attributes* because its a language feature
1 parent 53371a5 commit ffaacd3

File tree

3 files changed

+79
-247
lines changed

3 files changed

+79
-247
lines changed

Zend/tests/attributes_001.phpt

Lines changed: 79 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,112 @@
11
--TEST--
2-
Basic attributes usage on all elements
2+
Attributes can be placed on all supported elements.
33
--FILE--
44
<?php
5-
class Attr {
6-
public $element;
75

8-
public function __construct(string $element) {
9-
$this->element = $element;
10-
}
6+
<<A1(1)>>
7+
class Foo
8+
{
9+
<<A1(2)>>
10+
public const FOO = 'foo', BAR = 'bar';
11+
12+
<<A1(3)>>
13+
public $x, $y;
14+
15+
<<A1(4)>>
16+
public function foo(<<A1(5)>> $a, <<A1(6)>> $b) { }
1117
}
12-
class OtherAttr {}
1318

14-
function dump_attributes($reflection) {
15-
$arr = [];
16-
foreach ($reflection->getAttributes() as $attribute) {
17-
if (!isset($arr[$attribute->getName()])) {
18-
$arr[$attribute->getName()] = [];
19-
}
20-
$arr[$attribute->getName()][] = $attribute->getArguments();
21-
}
22-
var_dump($arr);
23-
}
19+
$object = new <<A1(7)>> class () { };
2420

25-
function f0() {}
26-
dump_attributes(new ReflectionFunction("f0"));
21+
<<A1(8)>>
22+
function f1() { }
2723

28-
<<Attr("function")>>
29-
function foo() {}
30-
dump_attributes(new ReflectionFunction("foo"));
24+
$f2 = <<A1(9)>> function () { };
3125

32-
// Class attributes
33-
<<Attr("class")>>
34-
class Bar {
35-
<<Attr("const")>>
36-
const C = 2;
37-
<<Attr("property")>>
38-
public $x = 3;
26+
$f3 = <<A1(10)>> fn () => 1;
3927

40-
}
41-
$reflectionClass = new ReflectionClass("Bar");
42-
dump_attributes($reflectionClass);
43-
dump_attributes($reflectionClass->getReflectionConstant("C"));
44-
dump_attributes($reflectionClass->getProperty("x"));
28+
$ref = new \ReflectionClass(Foo::class);
4529

46-
$foo = <<Attr("closure")>>function () {};
47-
dump_attributes(new ReflectionFunction($foo));
30+
$sources = [
31+
$ref,
32+
$ref->getReflectionConstant('FOO'),
33+
$ref->getReflectionConstant('BAR'),
34+
$ref->getProperty('x'),
35+
$ref->getProperty('y'),
36+
$ref->getMethod('foo'),
37+
$ref->getMethod('foo')->getParameters()[0],
38+
$ref->getMethod('foo')->getParameters()[1],
39+
new \ReflectionObject($object),
40+
new \ReflectionFunction('f1'),
41+
new \ReflectionFunction($f2),
42+
new \ReflectionFunction($f3)
43+
];
4844

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));
54-
55-
// Multiple attributes with multiple values
56-
<<Attr()>>
57-
<<OtherAttr()>>
58-
function f1() {}
59-
dump_attributes(new ReflectionFunction("f1"));
45+
foreach ($sources as $r) {
46+
foreach ($r->getAttributes() as $attr) {
47+
var_dump($attr->getName(), $attr->getArguments());
48+
}
49+
}
6050

61-
// multiple instances of same attribute
62-
<<Attr("foo")>>
63-
<<Attr("bar")>>
64-
function f2() {}
65-
dump_attributes(new ReflectionFunction("f2"));
6651
?>
6752
--EXPECT--
68-
array(0) {
53+
string(2) "A1"
54+
array(1) {
55+
[0]=>
56+
int(1)
6957
}
58+
string(2) "A1"
7059
array(1) {
71-
["Attr"]=>
72-
array(1) {
73-
[0]=>
74-
array(1) {
75-
[0]=>
76-
string(8) "function"
77-
}
78-
}
60+
[0]=>
61+
int(2)
7962
}
63+
string(2) "A1"
8064
array(1) {
81-
["Attr"]=>
82-
array(1) {
83-
[0]=>
84-
array(1) {
85-
[0]=>
86-
string(5) "class"
87-
}
88-
}
65+
[0]=>
66+
int(2)
8967
}
68+
string(2) "A1"
9069
array(1) {
91-
["Attr"]=>
92-
array(1) {
93-
[0]=>
94-
array(1) {
95-
[0]=>
96-
string(5) "const"
97-
}
98-
}
70+
[0]=>
71+
int(3)
9972
}
73+
string(2) "A1"
10074
array(1) {
101-
["Attr"]=>
102-
array(1) {
103-
[0]=>
104-
array(1) {
105-
[0]=>
106-
string(8) "property"
107-
}
108-
}
75+
[0]=>
76+
int(3)
10977
}
78+
string(2) "A1"
11079
array(1) {
111-
["Attr"]=>
112-
array(1) {
113-
[0]=>
114-
array(1) {
115-
[0]=>
116-
string(7) "closure"
117-
}
118-
}
80+
[0]=>
81+
int(4)
11982
}
83+
string(2) "A1"
12084
array(1) {
121-
["Attr"]=>
122-
array(1) {
123-
[0]=>
124-
array(1) {
125-
[0]=>
126-
string(13) "short closure"
127-
}
128-
}
85+
[0]=>
86+
int(5)
12987
}
88+
string(2) "A1"
13089
array(1) {
131-
["Attr"]=>
132-
array(1) {
133-
[0]=>
134-
array(1) {
135-
[0]=>
136-
string(10) "anon class"
137-
}
138-
}
90+
[0]=>
91+
int(6)
13992
}
140-
array(2) {
141-
["Attr"]=>
142-
array(1) {
143-
[0]=>
144-
array(0) {
145-
}
146-
}
147-
["OtherAttr"]=>
148-
array(1) {
149-
[0]=>
150-
array(0) {
151-
}
152-
}
93+
string(2) "A1"
94+
array(1) {
95+
[0]=>
96+
int(7)
15397
}
98+
string(2) "A1"
15499
array(1) {
155-
["Attr"]=>
156-
array(2) {
157-
[0]=>
158-
array(1) {
159-
[0]=>
160-
string(3) "foo"
161-
}
162-
[1]=>
163-
array(1) {
164-
[0]=>
165-
string(3) "bar"
166-
}
167-
}
100+
[0]=>
101+
int(8)
102+
}
103+
string(2) "A1"
104+
array(1) {
105+
[0]=>
106+
int(9)
107+
}
108+
string(2) "A1"
109+
array(1) {
110+
[0]=>
111+
int(10)
168112
}

ext/reflection/tests/attributes/placement.phpt

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)