Skip to content

Commit 1b5b182

Browse files
authored
Merge branch '0.14' into fix-missing-is-type-of
2 parents 39617b6 + cc3a0fe commit 1b5b182

File tree

2 files changed

+63
-46
lines changed

2 files changed

+63
-46
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"ext-json": "*",
3030
"murtukov/php-code-generator": "^0.1.5",
3131
"phpdocumentor/reflection-docblock": "^5.2",
32+
"phpdocumentor/type-resolver": ">1.4.0,<1.6.0",
3233
"psr/log": "^1.0 || ^2.0 || ^3.0",
3334
"symfony/config": "^4.4.30 || ^5.3.7",
3435
"symfony/dependency-injection": "^4.4.30 || ^5.3.7",
@@ -74,7 +75,6 @@
7475
"twig/twig": "^2.10|^3.0"
7576
},
7677
"conflict": {
77-
"phpdocumentor/type-resolver": "<1.4.0",
7878
"react/promise": "<2.8"
7979
},
8080
"extra": {

tests/Config/Parser/MetadataParser/TypeGuesser/DocBlockTypeGuesserTest.php

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,96 @@ class DocBlockTypeGuesserTest extends TestCase
2020
ReflectionMethod::class => 'return',
2121
];
2222

23-
public function testGuess(): void
23+
/**
24+
* @dataProvider guessSuccessDataProvider
25+
*/
26+
public function testGuessSuccess(string $docType, string $gqlType, ?ClassesTypesMap $map, ?string $reflectorClass): void
2427
{
25-
foreach ($this->reflectors as $reflectorClass => $tag) {
26-
$this->doTest('string', 'String!', null, $reflectorClass);
27-
$this->doTest('?string', 'String', null, $reflectorClass);
28-
$this->doTest('string|null', 'String', null, $reflectorClass);
29-
$this->doTest('string[]', '[String!]!', null, $reflectorClass);
30-
$this->doTest('array<string>', '[String!]!', null, $reflectorClass);
31-
$this->doTest('array<string>|null', '[String!]', null, $reflectorClass);
32-
$this->doTest('array<string|null>|null', '[String]', null, $reflectorClass);
33-
$this->doTest('int', 'Int!', null, $reflectorClass);
34-
$this->doTest('integer', 'Int!', null, $reflectorClass);
35-
$this->doTest('boolean', 'Boolean!', null, $reflectorClass);
36-
$this->doTest('bool', 'Boolean!', null, $reflectorClass);
37-
$this->doTest('float', 'Float!', null, $reflectorClass);
38-
$this->doTest('double', 'Float!', null, $reflectorClass);
39-
$this->doTest('iterable<string>', '[String!]!', null, $reflectorClass);
28+
$docBlockGuesser = new DocBlockTypeGuesser($map ?: new ClassesTypesMap());
29+
$this->assertEquals(
30+
$gqlType,
31+
$docBlockGuesser->guessType(
32+
new ReflectionClass(__CLASS__),
33+
$this->getMockedReflector($docType, $reflectorClass ?? ReflectionProperty::class)
34+
)
35+
);
36+
}
4037

41-
$this->doTestError('int|float', $reflectorClass, 'Tag @'.$tag.' found, but composite types are only allowed with null');
42-
$this->doTestError('array<int|float>', $reflectorClass, 'Tag @'.$tag.' found, but composite types in array or iterable are only allowed with null');
43-
$this->doTestError('UnknownClass', $reflectorClass, 'Tag @'.$tag.' found, but target object "Overblog\GraphQLBundle\Tests\Config\Parser\UnknownClass" is not a GraphQL Type class');
44-
$this->doTestError('object', $reflectorClass, 'Tag @'.$tag.' found, but type "object" is too generic');
45-
$this->doTestError('mixed[]', $reflectorClass, 'Tag @'.$tag.' found, but the array values cannot be mixed type');
46-
$this->doTestError('array<mixed>', $reflectorClass, 'Tag @'.$tag.' found, but the array values cannot be mixed type');
47-
$this->doTestError('', $reflectorClass, 'No @'.$tag.' tag found in doc block or tag has no type');
48-
$this->doTestError('[]', $reflectorClass, 'Doc Block parsing failed');
38+
public function guessSuccessDataProvider(): iterable
39+
{
40+
foreach ($this->reflectors as $reflectorClass => $tag) {
41+
yield ['string', 'String!', null, $reflectorClass];
42+
yield ['?string', 'String', null, $reflectorClass];
43+
yield ['string|null', 'String', null, $reflectorClass];
44+
yield ['string[]', '[String!]!', null, $reflectorClass];
45+
yield ['array<string>', '[String!]!', null, $reflectorClass];
46+
yield ['array<string>|null', '[String!]', null, $reflectorClass];
47+
yield ['array<string|null>|null', '[String]', null, $reflectorClass];
48+
yield ['int', 'Int!', null, $reflectorClass];
49+
yield ['integer', 'Int!', null, $reflectorClass];
50+
yield ['boolean', 'Boolean!', null, $reflectorClass];
51+
yield ['bool', 'Boolean!', null, $reflectorClass];
52+
yield ['float', 'Float!', null, $reflectorClass];
53+
yield ['double', 'Float!', null, $reflectorClass];
54+
yield ['iterable<string>', '[String!]!', null, $reflectorClass];
55+
}
4956

50-
$map = new ClassesTypesMap();
51-
$map->addClassType('GQLType1', 'Fake\Class1', 'object');
52-
$map->addClassType('GQLType2', 'Fake\Class2', 'object');
53-
$map->addClassType('Foo', ClassesTypesMap::class, 'object');
57+
$map = new ClassesTypesMap();
58+
$map->addClassType('GQLType1', 'Fake\Class1', 'object');
59+
$map->addClassType('GQLType2', 'Fake\Class2', 'object');
60+
$map->addClassType('Foo', ClassesTypesMap::class, 'object');
5461

55-
$this->doTest('\Fake\Class1[]', '[GQLType1!]!', $map);
56-
$this->doTest('ClassesTypesMap|null', 'Foo', $map);
57-
}
62+
yield ['\Fake\Class1[]', '[GQLType1!]!', $map, null];
63+
yield ['ClassesTypesMap|null', 'Foo', $map, null];
5864
}
5965

60-
public function testMissingDocBlock(): void
66+
/**
67+
* @dataProvider guessErrorDataProvider
68+
*/
69+
public function testGuessError(string $docType, string $reflectorClass, string $match): void
6170
{
6271
$docBlockGuesser = new DocBlockTypeGuesser(new ClassesTypesMap());
63-
$mock = $this->createMock(ReflectionProperty::class);
64-
$mock->method('getDocComment')->willReturn(false);
65-
6672
try {
67-
$docBlockGuesser->guessType(new ReflectionClass(__CLASS__), $mock);
73+
$docBlockGuesser->guessType(new ReflectionClass(__CLASS__), $this->getMockedReflector($docType, $reflectorClass));
74+
$this->fail(sprintf('The @var "%s" should resolve to GraphQL type "%s"', $docType, $match));
6875
} catch (Exception $e) {
6976
$this->assertInstanceOf(TypeGuessingException::class, $e);
70-
$this->assertEquals('Doc Block not found', $e->getMessage());
77+
$this->assertStringContainsString($match, $e->getMessage());
7178
}
7279
}
7380

74-
protected function doTest(string $docType, string $gqlType, ClassesTypesMap $map = null, string $reflectorClass = ReflectionProperty::class): void
81+
public function guessErrorDataProvider(): iterable
7582
{
76-
$docBlockGuesser = new DocBlockTypeGuesser($map ?: new ClassesTypesMap());
77-
$this->assertEquals($gqlType, $docBlockGuesser->guessType(new ReflectionClass(__CLASS__), $this->getMockedReflector($docType, $reflectorClass)));
83+
foreach ($this->reflectors as $reflectorClass => $tag) {
84+
yield ['int|float', $reflectorClass, 'Tag @'.$tag.' found, but composite types are only allowed with null'];
85+
yield ['array<int|float>', $reflectorClass, 'Tag @'.$tag.' found, but composite types in array or iterable are only allowed with null'];
86+
yield ['UnknownClass', $reflectorClass, 'Tag @'.$tag.' found, but target object "Overblog\GraphQLBundle\Tests\Config\Parser\UnknownClass" is not a GraphQL Type class'];
87+
yield ['object', $reflectorClass, 'Tag @'.$tag.' found, but type "object" is too generic'];
88+
yield ['mixed[]', $reflectorClass, 'Tag @'.$tag.' found, but the array values cannot be mixed type'];
89+
yield ['array<mixed>', $reflectorClass, 'Tag @'.$tag.' found, but the array values cannot be mixed type'];
90+
yield ['', $reflectorClass, 'No @'.$tag.' tag found in doc block or tag has no type'];
91+
yield ['[]', $reflectorClass, 'Doc Block parsing failed'];
92+
}
7893
}
7994

80-
protected function doTestError(string $docType, string $reflectorClass, string $match): void
95+
public function testMissingDocBlock(): void
8196
{
8297
$docBlockGuesser = new DocBlockTypeGuesser(new ClassesTypesMap());
98+
$mock = $this->createMock(ReflectionProperty::class);
99+
$mock->method('getDocComment')->willReturn(false);
100+
83101
try {
84-
$docBlockGuesser->guessType(new ReflectionClass(__CLASS__), $this->getMockedReflector($docType, $reflectorClass));
85-
$this->fail(sprintf('The @var "%s" should resolve to GraphQL type "%s"', $docType, $match));
102+
$docBlockGuesser->guessType(new ReflectionClass(__CLASS__), $mock);
86103
} catch (Exception $e) {
87104
$this->assertInstanceOf(TypeGuessingException::class, $e);
88-
$this->assertStringContainsString($match, $e->getMessage());
105+
$this->assertEquals('Doc Block not found', $e->getMessage());
89106
}
90107
}
91108

92109
/**
93110
* @return ReflectionProperty|ReflectionMethod
94111
*/
95-
protected function getMockedReflector(string $type, string $className = ReflectionProperty::class)
112+
private function getMockedReflector(string $type, string $className = ReflectionProperty::class)
96113
{
97114
// @phpstan-ignore-next-line
98115
$mock = $this->createMock($className);

0 commit comments

Comments
 (0)