Skip to content

Commit

Permalink
100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed May 10, 2023
1 parent ca13db0 commit 21415a6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
18 changes: 1 addition & 17 deletions src/Attribute/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ public function getAttribute(ReflectionClass|ReflectionProperty|ReflectionMethod
{
$attributes = $this->getAttributeInstances($element, $attributeName);

return $this->onlyOne($attributes);
}

/**
* @template T of ApiAttribute
*
* @param T[] $attributes
*
* @return null|T
*/
private function onlyOne(array $attributes): ?ApiAttribute
{
if (count($attributes) > 1) {
throw new Exception('Expected to find single attribute, but found many');
}

return reset($attributes) ?: null;
}

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

$attributes = $element->getAttributes($attributeName);
Expand Down
35 changes: 35 additions & 0 deletions tests/Attribute/Reader/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace GraphQLTests\Doctrine\Attribute\Reader;

use GraphQL\Doctrine\Attribute\Argument;
use GraphQL\Doctrine\Attribute\Exclude;
use GraphQL\Doctrine\Attribute\Field;
use GraphQL\Doctrine\Attribute\Filter;
use GraphQL\Doctrine\Attribute\FilterGroupCondition;
Expand All @@ -17,6 +18,7 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use ReturnTypeWillChange;

class ReaderTest extends TestCase
{
Expand Down Expand Up @@ -80,4 +82,37 @@ public function testGetParameterAttribute(): void
$this->reader->getAttribute((new ReflectionMethod(User::class, 'getPosts'))->getParameters()[0], Argument::class)
);
}

public function testWillThrowIfUniqueAttributeIsUsedMultipleTimes(): void
{
$mock = new class() {
#[Exclude]
/** @phpstan-ignore-next-line */
#[Exclude]
/**
* @phpstan-ignore-next-line
*/
private $foo;
};

$this->expectExceptionMessage('Attribute "GraphQL\Doctrine\Attribute\Exclude" must not be repeated');
$this->reader->getAttribute(new ReflectionProperty($mock, 'foo'), Exclude::class);
}

public function testWillThrowIfReaderIsUsedWithOtherAttributes(): void
{
$mock = new class() {
/**
* @phpstan-ignore-next-line
*/
#[ReturnTypeWillChange]
private function foo(): void
{
}
};

$this->expectExceptionMessage('GraphQL\Doctrine\Attribute\Reader\Reader cannot be used for attribute than are not part of `ecodev/graphql-doctrine`.');
// @phpstan-ignore-next-line
$this->reader->getAttribute(new ReflectionMethod($mock, 'foo'), ReturnTypeWillChange::class);
}
}
18 changes: 18 additions & 0 deletions tests/Blog/Model/Special/ArgumentOverrideDefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace GraphQLTests\Doctrine\Blog\Model\Special;

use Doctrine\ORM\Mapping as ORM;
use GraphQL\Doctrine\Attribute\Argument;
use GraphQLTests\Doctrine\Blog\Model\AbstractModel;

#[ORM\Entity]
final class ArgumentOverrideDefaultValue extends AbstractModel
{
public function getWithParams(#[Argument(defaultValue: 2)] int $param = 1): string
{
return __FUNCTION__;
}
}
6 changes: 6 additions & 0 deletions tests/OutputTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ public function testFieldWithObjectTypeArgumentMustThrow(): void
$type = $this->types->getOutput(Blog\Model\Special\ObjectTypeArgument::class);
$type->getFields();
}

public function testCanOverrideArgumentDefaultValue(): void
{
$actual = $this->types->getOutput(Blog\Model\Special\ArgumentOverrideDefaultValue::class);
$this->assertType('tests/data/ArgumentOverrideDefaultValue.graphqls', $actual);
}
}
5 changes: 5 additions & 0 deletions tests/data/ArgumentOverrideDefaultValue.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type ArgumentOverrideDefaultValue {
withParams(param: Int = 2): String!
id: ID!
creationDate: DateTime!
}

0 comments on commit 21415a6

Please sign in to comment.