2323use ReflectionMethod ;
2424use ReflectionProperty ;
2525use Spiral \Attributes \AnnotationReader ;
26+ use Spiral \Attributes \FallbackAttributeReader ;
2627use Spiral \Attributes \NativeAttributeReader ;
2728
2829/**
@@ -54,51 +55,48 @@ public function testAttach(): void
5455
5556 $ this ->assertCount (1 , $ founds = \iterator_to_array ($ annotation ->load ()));
5657
58+ /** @var Fixtures\SampleCollector $found */
5759 foreach ($ founds as $ found ) {
5860 $ this ->assertInstanceOf (Fixtures \SampleCollector::class, $ found );
5961
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 ();
6464
65+ foreach ($ collected as $ name => $ sample ) {
6566 $ names [] = $ name ;
6667 $ result [] = $ sample ;
6768 }
6869 }
6970
70- asort ($ names );
71- asort ($ result );
72-
7371 $ this ->assertEquals ([
7472 'default ' ,
75- 'protected_property ' ,
76- 'private_property ' ,
77- 'mtp_start ' ,
73+ 'global_property ' ,
74+ 'global_specific_name ' ,
75+ 'global_specific_none ' ,
7876 'mtp_end ' ,
7977 'mtp_next ' ,
78+ 'mtp_start ' ,
8079 'priority ' ,
8180 'private ' ,
81+ 'private_property ' ,
8282 'protected ' ,
83+ 'protected_property ' ,
8384 'public_property ' ,
84- 'global_specific_name ' ,
85- 'global_specific_none ' ,
86- 'global_property ' ,
8785 ], $ names );
8886
8987 $ this ->assertEquals ([
9088 ['handler ' => ReflectionMethod::class, 'priority ' => 24 ],
9189 ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
92- ['handler ' => ReflectionProperty::class, 'priority ' => 4 ],
9390 ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
91+ ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
9492 ['handler ' => ReflectionMethod::class, 'priority ' => 1 ],
9593 ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
9694 ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
9795 ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
96+ ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
97+ ['handler ' => ReflectionProperty::class, 'priority ' => 4 ],
9898 ['handler ' => ReflectionMethod::class, 'priority ' => 323 ],
9999 ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
100- ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
101- ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
102100 ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
103101 ], $ result );
104102 }
@@ -117,22 +115,55 @@ public function testAttachAttribute(): void
117115
118116 $ this ->assertCount (1 , $ founds = \iterator_to_array ($ annotation ->load ()));
119117
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 */
120152 foreach ($ founds as $ found ) {
121153 $ this ->assertInstanceOf (Fixtures \SampleCollector::class, $ found );
122154
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 ();
127157
158+ foreach ($ collected as $ name => $ sample ) {
128159 $ result [$ name ] = $ sample ;
129160 }
130161 }
131162
132163 $ this ->assertEquals ([
133164 'attribute_specific_name ' => ['handler ' => ReflectionMethod::class, 'priority ' => 0 ],
134165 'attribute_specific_none ' => ['handler ' => ReflectionMethod::class, 'priority ' => 14 ],
135- 'attribite_property ' => ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
166+ 'attribute_property ' => ['handler ' => ReflectionProperty::class, 'priority ' => 0 ],
136167 ], $ result );
137168 }
138169}
0 commit comments