Skip to content

Commit 6991df9

Browse files
authored
Fix MagicField & SourceField to include $annotations parameter (#564)
1 parent 3eb14df commit 6991df9

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

src/Annotations/MagicField.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ class MagicField implements SourceFieldInterface
4646
/** @var array<string, ParameterAnnotations> */
4747
private $parameterAnnotations;
4848

49-
/** @param mixed[] $attributes */
50-
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null, string|null $phpType = null, string|null $description = null, string|null $sourceName = null)
49+
/**
50+
* @param mixed[] $attributes
51+
* @param array<MiddlewareAnnotationInterface|ParameterAnnotationInterface> $annotations
52+
*/
53+
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null, string|null $phpType = null, string|null $description = null, string|null $sourceName = null, array $annotations = [])
5154
{
5255
$this->name = $attributes['name'] ?? $name;
5356
$this->outputType = $attributes['outputType'] ?? $outputType ?? null;
@@ -63,7 +66,7 @@ public function __construct(array $attributes = [], string|null $name = null, st
6366
}
6467
$middlewareAnnotations = [];
6568
$parameterAnnotations = [];
66-
$annotations = $attributes['annotations'] ?? [];
69+
$annotations = $annotations ?: $attributes['annotations'] ?? [];
6770
if (! is_array($annotations)) {
6871
$annotations = [$annotations];
6972
}

src/Annotations/SourceField.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ class SourceField implements SourceFieldInterface
4040
/** @var array<string, ParameterAnnotations> */
4141
private array $parameterAnnotations;
4242

43-
/** @param mixed[] $attributes */
44-
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null, string|null $phpType = null, string|null $description = null, string|null $sourceName = null)
43+
/**
44+
* @param mixed[] $attributes
45+
* @param array<MiddlewareAnnotationInterface|ParameterAnnotationInterface> $annotations
46+
*/
47+
public function __construct(array $attributes = [], string|null $name = null, string|null $outputType = null, string|null $phpType = null, string|null $description = null, string|null $sourceName = null, array $annotations = [])
4548
{
4649
$name = $name ?? $attributes['name'] ?? null;
4750
if ($name === null) {
@@ -59,7 +62,7 @@ public function __construct(array $attributes = [], string|null $name = null, st
5962
}
6063
$middlewareAnnotations = [];
6164
$parameterAnnotations = [];
62-
$annotations = $attributes['annotations'] ?? [];
65+
$annotations = $annotations ?: $attributes['annotations'] ?? [];
6366
if (! is_array($annotations)) {
6467
$annotations = [$annotations];
6568
}

tests/Annotations/MagicFieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ public function testExceptionInConstruct3(): void
2929
public function testAnnotations(): void
3030
{
3131
$magicField = new MagicField(['name'=>'test', 'outputType'=>'String!', 'annotations'=>[new Logged(), new Autowire(['for'=>'foo'])]]);
32+
3233
$this->assertNotEmpty($magicField->getMiddlewareAnnotations()->getAnnotationsByType(Logged::class));
3334
$this->assertNotEmpty($magicField->getParameterAnnotations()['foo']->getAnnotationsByType(Autowire::class));
3435
}
36+
37+
public function testAnnotationsFromParameter(): void
38+
{
39+
$magicField = new MagicField(name: 'test', outputType: 'String!', annotations: [new Logged(), new Autowire(['for'=>'foo'])]);
40+
41+
$this->assertNotEmpty($magicField->getMiddlewareAnnotations()->getAnnotationsByType(Logged::class));
42+
$this->assertNotEmpty($magicField->getParameterAnnotations()['foo']->getAnnotationsByType(Autowire::class));
43+
}
3544
}

tests/Annotations/SourceFieldTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,20 @@ public function testExceptionInConstruct3(): void
2525
$this->expectException(BadMethodCallException::class);
2626
new SourceField(['name'=>'test', 'phpType'=>'string', 'outputType'=>'String!']);
2727
}
28+
29+
public function testAnnotations(): void
30+
{
31+
$magicField = new SourceField(['name'=>'test', 'outputType'=>'String!', 'annotations'=>[new Logged(), new Autowire(['for'=>'foo'])]]);
32+
33+
$this->assertNotEmpty($magicField->getMiddlewareAnnotations()->getAnnotationsByType(Logged::class));
34+
$this->assertNotEmpty($magicField->getParameterAnnotations()['foo']->getAnnotationsByType(Autowire::class));
35+
}
36+
37+
public function testAnnotationsFromParameter(): void
38+
{
39+
$magicField = new SourceField(name: 'test', outputType: 'String!', annotations: [new Logged(), new Autowire(['for'=>'foo'])]);
40+
41+
$this->assertNotEmpty($magicField->getMiddlewareAnnotations()->getAnnotationsByType(Logged::class));
42+
$this->assertNotEmpty($magicField->getParameterAnnotations()['foo']->getAnnotationsByType(Autowire::class));
43+
}
2844
}

0 commit comments

Comments
 (0)