Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQLTests\Doctrine; |
||
| 6 | |||
| 7 | use GraphQL\Doctrine\DocBlockReader; |
||
| 8 | use PHPUnit\Framework\TestCase; |
||
| 9 | use ReflectionMethod; |
||
| 10 | use ReflectionParameter; |
||
| 11 | |||
| 12 | class DocBlockReaderTest extends TestCase |
||
| 13 | { |
||
| 14 | private const EMPTY_COMMENT = ' |
||
| 15 | /** |
||
| 16 | */'; |
||
| 17 | |||
| 18 | private const COMMENT = ' |
||
| 19 | /** |
||
| 20 | * Get interesting data |
||
| 21 | * |
||
| 22 | * Long description for the method |
||
| 23 | * spanning lines |
||
| 24 | * |
||
| 25 | * @param null|string $foo Some param description |
||
| 26 | * @param User $bar |
||
| 27 | * @param \DateTimeImmutable $bazAbsolute |
||
| 28 | * @param DateTimeImmutable $bazRelative |
||
| 29 | * @return bool |
||
| 30 | */'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @dataProvider providerGetMethodDescription |
||
| 34 | */ |
||
| 35 | public function testGetMethodDescription(string|false $comment, ?string $expected): void |
||
| 36 | { |
||
| 37 | $reader = $this->create($comment); |
||
| 38 | $actual = $reader->getMethodDescription(); |
||
| 39 | self::assertSame($expected, $actual); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function providerGetMethodDescription(): array |
||
| 43 | { |
||
| 44 | return [ |
||
| 45 | [false, null], |
||
| 46 | [self::EMPTY_COMMENT, null], |
||
| 47 | [self::COMMENT, 'Interesting data |
||
| 48 | |||
| 49 | Long description for the method |
||
| 50 | spanning lines'], |
||
| 51 | ]; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @dataProvider providerGetParameterDescription |
||
| 56 | */ |
||
| 57 | public function testGetParameterDescription(string|false $comment, string $parameterName, ?string $expected): void |
||
| 58 | { |
||
| 59 | $reader = $this->create($comment); |
||
| 60 | $parameter = $this->createParameter($parameterName); |
||
| 61 | $actual = $reader->getParameterDescription($parameter); |
||
| 62 | self::assertSame($expected, $actual); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function providerGetParameterDescription(): array |
||
| 66 | { |
||
| 67 | return [ |
||
| 68 | [false, 'foo', null], |
||
| 69 | [self::EMPTY_COMMENT, 'foo', null], |
||
| 70 | [self::COMMENT, 'foo', 'Some param description'], |
||
| 71 | 'non-existing param' => [self::COMMENT, 'fo', null], |
||
| 72 | [self::COMMENT, 'bar', null], |
||
| 73 | ]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @dataProvider providerGetParameterType |
||
| 78 | */ |
||
| 79 | public function testGetParameterType(string|false $comment, string $parameterName, ?string $expected): void |
||
| 80 | { |
||
| 81 | $reader = $this->create($comment); |
||
| 82 | $parameter = $this->createParameter($parameterName); |
||
| 83 | $actual = $reader->getParameterType($parameter); |
||
| 84 | self::assertSame($expected, $actual); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function providerGetParameterType(): array |
||
| 88 | { |
||
| 89 | return [ |
||
| 90 | [false, 'foo', null], |
||
| 91 | [self::EMPTY_COMMENT, 'foo', null], |
||
| 92 | [self::COMMENT, 'foo', 'null|string'], |
||
| 93 | 'non-existing param' => [self::COMMENT, 'fo', null], |
||
| 94 | [self::COMMENT, 'bar', 'User'], |
||
| 95 | 'do not make assumption on absolute types' => [self::COMMENT, 'bazAbsolute', '\DateTimeImmutable'], |
||
| 96 | 'do not make assumption on relative types' => [self::COMMENT, 'bazRelative', 'DateTimeImmutable'], |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @dataProvider providerGetReturnType |
||
| 102 | */ |
||
| 103 | public function testGetReturnType(string|false $comment, ?string $expected): void |
||
| 104 | { |
||
| 105 | $reader = $this->create($comment); |
||
| 106 | $actual = $reader->getReturnType(); |
||
| 107 | self::assertSame($expected, $actual); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function providerGetReturnType(): array |
||
| 111 | { |
||
| 112 | return [ |
||
| 113 | [false, null], |
||
| 114 | [self::EMPTY_COMMENT, null], |
||
| 115 | [self::COMMENT, 'bool'], |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | |||
| 119 | private function create(string|false $comment): DocBlockReader |
||
| 120 | { |
||
| 121 | $method = new class($comment) extends ReflectionMethod { |
||
| 122 | public function __construct(private readonly string|false $comment) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 123 | { |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getDocComment(): string|false |
||
| 127 | { |
||
| 128 | return $this->comment; |
||
| 129 | } |
||
| 130 | }; |
||
| 131 | |||
| 132 | return new DocBlockReader($method); |
||
| 133 | } |
||
| 134 | |||
| 135 | private function createParameter(string $name): ReflectionParameter |
||
| 136 | { |
||
| 137 | return new class($name) extends ReflectionParameter { |
||
| 138 | public function __construct(public string $mockedName) |
||
| 139 | { |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getName(): string |
||
| 143 | { |
||
| 144 | return $this->mockedName; |
||
| 145 | } |
||
| 146 | }; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |