Skip to content

Commit 6eabb94

Browse files
Fixed phpunit tests failing with Github Actions
1 parent 09151be commit 6eabb94

File tree

3 files changed

+102
-51
lines changed

3 files changed

+102
-51
lines changed

src/AnnotationLoader.php

+42-26
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,7 @@ private function findAnnotations(string $resource, ListenerInterface $listener):
135135
$classReflection->getConstants()
136136
);
137137

138-
foreach ($reflections as $reflection) {
139-
if ($reflection instanceof ReflectionMethod && $reflection->isAbstract()) {
140-
continue;
141-
}
142-
143-
foreach ($this->getAnnotations($reflection, $listener) as $annotation) {
144-
if ($reflection instanceof ReflectionMethod) {
145-
$annotations[$className]['method'][] = [$reflection, $annotation];
146-
147-
foreach ($this->getMethodParameter($reflection->getParameters(), $listener) as $parameter) {
148-
$annotations[$className]['method_property'][] = $parameter;
149-
}
150-
151-
continue;
152-
}
153-
154-
if ($reflection instanceof ReflectionClassConstant) {
155-
$annotations[$className]['constant'][] = [$reflection, $annotation];
156-
157-
continue;
158-
}
159-
160-
$annotations[$className]['property'][] = [$reflection, $annotation];
161-
}
162-
}
138+
$this->fetchAnnotations($className, $reflections, $listener, $annotations);
163139
}
164140

165141
\gc_mem_caches();
@@ -168,7 +144,6 @@ private function findAnnotations(string $resource, ListenerInterface $listener):
168144
}
169145

170146
/**
171-
*
172147
* @param Reflector $reflection
173148
*
174149
* @return iterable<object>
@@ -229,6 +204,47 @@ private function getMethodParameter(array $parameters, ListenerInterface $listen
229204
}
230205
}
231206

207+
/**
208+
* Fetch annotations from methods, constant, property and methods parameter
209+
*
210+
* @param string $className
211+
* @param Reflector[] $reflections
212+
* @param ListenerInterface $listener
213+
* @param array<string,array<string,mixed>> $annotations
214+
*/
215+
private function fetchAnnotations(
216+
string $className,
217+
array $reflections,
218+
ListenerInterface $listener,
219+
array &$annotations
220+
): void {
221+
foreach ($reflections as $reflection) {
222+
if ($reflection instanceof ReflectionMethod && $reflection->isAbstract()) {
223+
continue;
224+
}
225+
226+
foreach ($this->getAnnotations($reflection, $listener) as $annotation) {
227+
if ($reflection instanceof ReflectionMethod) {
228+
$annotations[$className]['method'][] = [$reflection, $annotation];
229+
230+
foreach ($this->getMethodParameter($reflection->getParameters(), $listener) as $parameter) {
231+
$annotations[$className]['method_property'][] = $parameter;
232+
}
233+
234+
continue;
235+
}
236+
237+
if ($reflection instanceof ReflectionClassConstant) {
238+
$annotations[$className]['constant'][] = [$reflection, $annotation];
239+
240+
continue;
241+
}
242+
243+
$annotations[$className]['property'][] = [$reflection, $annotation];
244+
}
245+
}
246+
}
247+
232248
/**
233249
* Finds classes in the given resource directory
234250
*

tests/AnnotationLoaderTest.php

+52-21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ReflectionMethod;
2424
use ReflectionProperty;
2525
use Spiral\Attributes\AnnotationReader;
26+
use Spiral\Attributes\FallbackAttributeReader;
2627
use 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
}

tests/Fixtures/SampleCollector.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Biurad\Annotations\Tests\Fixtures;
1919

20+
use ArrayObject;
2021
use Reflector;
2122

2223
class SampleCollector
@@ -30,14 +31,17 @@ class SampleCollector
3031
*/
3132
public function add(string $name, int $priority, $handler): void
3233
{
33-
$this->collected[$name] = ['handler' => $handler, 'priority' => $priority];
34+
$this->collected[$name] = [
35+
'handler' => $handler instanceof Reflector ? \get_class($handler) : $handler,
36+
'priority' => $priority,
37+
];
3438
}
3539

3640
/**
37-
* @return array<int,array<string,Reflector|string>>
41+
* @return ArrayObject
3842
*/
39-
public function getCollected(): array
43+
public function getCollected(): ArrayObject
4044
{
41-
return $this->collected;
45+
return new ArrayObject($this->collected);
4246
}
4347
}

0 commit comments

Comments
 (0)