Skip to content

Commit 21415a6

Browse files
committedMay 10, 2023
100% code coverage
1 parent ca13db0 commit 21415a6

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed
 

‎src/Attribute/Reader/Reader.php

+1-17
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,6 @@ public function getAttribute(ReflectionClass|ReflectionProperty|ReflectionMethod
5757
{
5858
$attributes = $this->getAttributeInstances($element, $attributeName);
5959

60-
return $this->onlyOne($attributes);
61-
}
62-
63-
/**
64-
* @template T of ApiAttribute
65-
*
66-
* @param T[] $attributes
67-
*
68-
* @return null|T
69-
*/
70-
private function onlyOne(array $attributes): ?ApiAttribute
71-
{
72-
if (count($attributes) > 1) {
73-
throw new Exception('Expected to find single attribute, but found many');
74-
}
75-
7660
return reset($attributes) ?: null;
7761
}
7862

@@ -86,7 +70,7 @@ private function onlyOne(array $attributes): ?ApiAttribute
8670
private function getAttributeInstances(ReflectionClass|ReflectionMethod|ReflectionParameter|ReflectionProperty $element, string $attributeName): array
8771
{
8872
if (!is_subclass_of($attributeName, ApiAttribute::class)) {
89-
throw new Exception('This should not be used for attribute than are not ours');
73+
throw new Exception(self::class . ' cannot be used for attribute than are not part of `ecodev/graphql-doctrine`.');
9074
}
9175

9276
$attributes = $element->getAttributes($attributeName);

‎tests/Attribute/Reader/ReaderTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace GraphQLTests\Doctrine\Attribute\Reader;
66

77
use GraphQL\Doctrine\Attribute\Argument;
8+
use GraphQL\Doctrine\Attribute\Exclude;
89
use GraphQL\Doctrine\Attribute\Field;
910
use GraphQL\Doctrine\Attribute\Filter;
1011
use GraphQL\Doctrine\Attribute\FilterGroupCondition;
@@ -17,6 +18,7 @@
1718
use ReflectionClass;
1819
use ReflectionMethod;
1920
use ReflectionProperty;
21+
use ReturnTypeWillChange;
2022

2123
class ReaderTest extends TestCase
2224
{
@@ -80,4 +82,37 @@ public function testGetParameterAttribute(): void
8082
$this->reader->getAttribute((new ReflectionMethod(User::class, 'getPosts'))->getParameters()[0], Argument::class)
8183
);
8284
}
85+
86+
public function testWillThrowIfUniqueAttributeIsUsedMultipleTimes(): void
87+
{
88+
$mock = new class() {
89+
#[Exclude]
90+
/** @phpstan-ignore-next-line */
91+
#[Exclude]
92+
/**
93+
* @phpstan-ignore-next-line
94+
*/
95+
private $foo;
96+
};
97+
98+
$this->expectExceptionMessage('Attribute "GraphQL\Doctrine\Attribute\Exclude" must not be repeated');
99+
$this->reader->getAttribute(new ReflectionProperty($mock, 'foo'), Exclude::class);
100+
}
101+
102+
public function testWillThrowIfReaderIsUsedWithOtherAttributes(): void
103+
{
104+
$mock = new class() {
105+
/**
106+
* @phpstan-ignore-next-line
107+
*/
108+
#[ReturnTypeWillChange]
109+
private function foo(): void
110+
{
111+
}
112+
};
113+
114+
$this->expectExceptionMessage('GraphQL\Doctrine\Attribute\Reader\Reader cannot be used for attribute than are not part of `ecodev/graphql-doctrine`.');
115+
// @phpstan-ignore-next-line
116+
$this->reader->getAttribute(new ReflectionMethod($mock, 'foo'), ReturnTypeWillChange::class);
117+
}
83118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Blog\Model\Special;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use GraphQL\Doctrine\Attribute\Argument;
9+
use GraphQLTests\Doctrine\Blog\Model\AbstractModel;
10+
11+
#[ORM\Entity]
12+
final class ArgumentOverrideDefaultValue extends AbstractModel
13+
{
14+
public function getWithParams(#[Argument(defaultValue: 2)] int $param = 1): string
15+
{
16+
return __FUNCTION__;
17+
}
18+
}

‎tests/OutputTypesTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,10 @@ public function testFieldWithObjectTypeArgumentMustThrow(): void
101101
$type = $this->types->getOutput(Blog\Model\Special\ObjectTypeArgument::class);
102102
$type->getFields();
103103
}
104+
105+
public function testCanOverrideArgumentDefaultValue(): void
106+
{
107+
$actual = $this->types->getOutput(Blog\Model\Special\ArgumentOverrideDefaultValue::class);
108+
$this->assertType('tests/data/ArgumentOverrideDefaultValue.graphqls', $actual);
109+
}
104110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type ArgumentOverrideDefaultValue {
2+
withParams(param: Int = 2): String!
3+
id: ID!
4+
creationDate: DateTime!
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.