23
23
use ReflectionMethod ;
24
24
use ReflectionProperty ;
25
25
use Spiral \Attributes \AnnotationReader ;
26
+ use Spiral \Attributes \FallbackAttributeReader ;
26
27
use Spiral \Attributes \NativeAttributeReader ;
27
28
28
29
/**
@@ -54,51 +55,48 @@ public function testAttach(): void
54
55
55
56
$ this ->assertCount (1 , $ founds = \iterator_to_array ($ annotation ->load ()));
56
57
58
+ /** @var Fixtures\SampleCollector $found */
57
59
foreach ($ founds as $ found ) {
58
60
$ this ->assertInstanceOf (Fixtures \SampleCollector::class, $ found );
59
61
60
- foreach ($ found ->getCollected () as $ name => $ sample ) {
61
- if (\is_object ($ sample ['handler ' ])) {
62
- $ sample ['handler ' ] = \get_class ($ sample ['handler ' ]);
63
- }
62
+ $ collected = $ found ->getCollected ();
63
+ $ collected ->ksort ();
64
64
65
+ foreach ($ collected as $ name => $ sample ) {
65
66
$ names [] = $ name ;
66
67
$ result [] = $ sample ;
67
68
}
68
69
}
69
70
70
- asort ($ names );
71
- asort ($ result );
72
-
73
71
$ this ->assertEquals ([
74
72
'default ' ,
75
- 'protected_property ' ,
76
- 'private_property ' ,
77
- 'mtp_start ' ,
73
+ 'global_property ' ,
74
+ 'global_specific_name ' ,
75
+ 'global_specific_none ' ,
78
76
'mtp_end ' ,
79
77
'mtp_next ' ,
78
+ 'mtp_start ' ,
80
79
'priority ' ,
81
80
'private ' ,
81
+ 'private_property ' ,
82
82
'protected ' ,
83
+ 'protected_property ' ,
83
84
'public_property ' ,
84
- 'global_specific_name ' ,
85
- 'global_specific_none ' ,
86
- 'global_property ' ,
87
85
], $ names );
88
86
89
87
$ this ->assertEquals ([
90
88
['handler ' => ReflectionMethod::class, 'priority ' => 24 ],
91
89
['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
92
- ['handler ' => ReflectionProperty::class, 'priority ' => 4 ],
93
90
['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
91
+ ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
94
92
['handler ' => ReflectionMethod::class, 'priority ' => 1 ],
95
93
['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
96
94
['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
97
95
['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
96
+ ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
97
+ ['handler ' => ReflectionProperty::class, 'priority ' => 4 ],
98
98
['handler ' => ReflectionMethod::class, 'priority ' => 323 ],
99
99
['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
100
- ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
101
- ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
102
100
['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
103
101
], $ result );
104
102
}
@@ -117,22 +115,55 @@ public function testAttachAttribute(): void
117
115
118
116
$ this ->assertCount (1 , $ founds = \iterator_to_array ($ annotation ->load ()));
119
117
118
+ /** @var Fixtures\SampleCollector $found */
119
+ foreach ($ founds as $ found ) {
120
+ $ this ->assertInstanceOf (Fixtures \SampleCollector::class, $ found );
121
+
122
+ $ collected = $ found ->getCollected ();
123
+ $ collected ->ksort ();
124
+
125
+ foreach ($ collected as $ name => $ sample ) {
126
+ $ result [$ name ] = $ sample ;
127
+ }
128
+ }
129
+
130
+ $ this ->assertEquals ([
131
+ 'attribute_specific_name ' => ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
132
+ 'attribute_specific_none ' => ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
133
+ 'attribute_property ' => ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
134
+ ], $ result );
135
+ }
136
+
137
+ /**
138
+ * @requires PHP <= 8
139
+ * @runInSeparateProcess
140
+ */
141
+ public function testAttachAttributeFallback (): void
142
+ {
143
+ $ annotation = new AnnotationLoader (new FallbackAttributeReader ());
144
+ $ result = [];
145
+
146
+ $ annotation ->attachListener (new Fixtures \SampleListener ());
147
+ $ annotation ->attach (__DIR__ . '/Fixtures/Annotation/Attribute ' );
148
+
149
+ $ this ->assertCount (1 , $ founds = \iterator_to_array ($ annotation ->load ()));
150
+
151
+ /** @var Fixtures\SampleCollector $found */
120
152
foreach ($ founds as $ found ) {
121
153
$ this ->assertInstanceOf (Fixtures \SampleCollector::class, $ found );
122
154
123
- foreach ($ found ->getCollected () as $ name => $ sample ) {
124
- if (\is_object ($ sample ['handler ' ])) {
125
- $ sample ['handler ' ] = \get_class ($ sample ['handler ' ]);
126
- }
155
+ $ collected = $ found ->getCollected ();
156
+ $ collected ->ksort ();
127
157
158
+ foreach ($ collected as $ name => $ sample ) {
128
159
$ result [$ name ] = $ sample ;
129
160
}
130
161
}
131
162
132
163
$ this ->assertEquals ([
133
164
'attribute_specific_name ' => ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
134
165
'attribute_specific_none ' => ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
135
- 'attribite_property ' => ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
166
+ 'attribute_property ' => ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
136
167
], $ result );
137
168
}
138
169
}
0 commit comments