Skip to content

Commit 50b61c1

Browse files
committed
Clean up some tests
1 parent bd9951d commit 50b61c1

File tree

5 files changed

+74
-106
lines changed

5 files changed

+74
-106
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ parameters:
1818
# allow partial parsing of language fragments.
1919
- "~Variable method call on GraphQL\\\\Language\\\\Parser\\.~"
2020

21-
# We utilize lazy initialization of non-nullable properties
22-
- "~Property .+? on left side of \\?\\?= is not nullable~"
23-
2421
# Useful/necessary in the default field resolver, deals with arbitrary user data
2522
-
2623
message: "~Variable property access on object~"

tests/Executor/ExecutorLazySchemaTest.php

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PHPUnit\Framework\Error\Error;
2424
use PHPUnit\Framework\TestCase;
2525

26-
class ExecutorLazySchemaTest extends TestCase
26+
final class ExecutorLazySchemaTest extends TestCase
2727
{
2828
public ScalarType $someScalarType;
2929

@@ -54,40 +54,32 @@ public function testWarnsAboutSlowIsTypeOfForLazySchema(): void
5454
// isTypeOf used to resolve runtime type for Interface
5555
$petType = new InterfaceType([
5656
'name' => 'Pet',
57-
'fields' => static function (): array {
58-
return [
59-
'name' => ['type' => Type::string()],
60-
];
61-
},
57+
'fields' => static fn (): array => [
58+
'name' => [
59+
'type' => Type::string(),
60+
],
61+
],
6262
]);
6363

6464
// Added to interface type when defined
6565
$dogType = new ObjectType([
6666
'name' => 'Dog',
6767
'interfaces' => [$petType],
68-
'isTypeOf' => static function ($obj): bool {
69-
return $obj instanceof Dog;
70-
},
71-
'fields' => static function (): array {
72-
return [
73-
'name' => ['type' => Type::string()],
74-
'woofs' => ['type' => Type::boolean()],
75-
];
76-
},
68+
'isTypeOf' => static fn ($obj): bool => $obj instanceof Dog,
69+
'fields' => static fn (): array => [
70+
'name' => ['type' => Type::string()],
71+
'woofs' => ['type' => Type::boolean()],
72+
],
7773
]);
7874

7975
$catType = new ObjectType([
8076
'name' => 'Cat',
8177
'interfaces' => [$petType],
82-
'isTypeOf' => static function ($obj): bool {
83-
return $obj instanceof Cat;
84-
},
85-
'fields' => static function (): array {
86-
return [
87-
'name' => ['type' => Type::string()],
88-
'meows' => ['type' => Type::boolean()],
89-
];
90-
},
78+
'isTypeOf' => static fn ($obj): bool => $obj instanceof Cat,
79+
'fields' => static fn (): array => [
80+
'name' => ['type' => Type::string()],
81+
'meows' => ['type' => Type::boolean()],
82+
],
9183
]);
9284

9385
$schema = new Schema([
@@ -96,23 +88,20 @@ public function testWarnsAboutSlowIsTypeOfForLazySchema(): void
9688
'fields' => [
9789
'pets' => [
9890
'type' => Type::listOf($petType),
99-
'resolve' => static function (): array {
100-
return [new Dog('Odie', true), new Cat('Garfield', false)];
101-
},
91+
'resolve' => static fn (): array => [
92+
new Dog('Odie', true),
93+
new Cat('Garfield', false),
94+
],
10295
],
10396
],
10497
]),
10598
'types' => [$catType, $dogType],
106-
'typeLoader' => static function ($name) use ($dogType, $petType, $catType) {
99+
'typeLoader' => static function ($name) use ($dogType, $petType, $catType): ?Type {
107100
switch ($name) {
108-
case 'Dog':
109-
return $dogType;
110-
111-
case 'Pet':
112-
return $petType;
113-
114-
case 'Cat':
115-
return $catType;
101+
case 'Dog': return $dogType;
102+
case 'Pet': return $petType;
103+
case 'Cat': return $catType;
104+
default: return null;
116105
}
117106
},
118107
]);
@@ -160,28 +149,21 @@ public function testHintsOnConflictingTypeInstancesInDefinitions(): void
160149
$typeLoader = static function ($name) use (&$calls): ?ObjectType {
161150
$calls[] = $name;
162151
switch ($name) {
163-
case 'Test':
164-
return new ObjectType([
165-
'name' => 'Test',
166-
'fields' => static function (): array {
167-
return [
168-
'test' => Type::string(),
169-
];
170-
},
171-
]);
172-
173-
default:
174-
return null;
152+
case 'Test': return new ObjectType([
153+
'name' => 'Test',
154+
'fields' => static fn (): array => [
155+
'test' => Type::string(),
156+
],
157+
]);
158+
default: return null;
175159
}
176160
};
177161

178162
$query = new ObjectType([
179163
'name' => 'Query',
180-
'fields' => static function () use ($typeLoader): array {
181-
return [
182-
'test' => $typeLoader('Test'),
183-
];
184-
},
164+
'fields' => static fn (): array => [
165+
'test' => $typeLoader('Test'),
166+
],
185167
]);
186168

187169
$schema = new Schema([
@@ -215,9 +197,7 @@ public function testSimpleQuery(): void
215197
{
216198
$schema = new Schema([
217199
'query' => $this->loadType('Query'),
218-
'typeLoader' => function ($name) {
219-
return $this->loadType($name, true);
220-
},
200+
'typeLoader' => fn (string $name): ?Type => $this->loadType($name, true),
221201
]);
222202

223203
$query = '{ object { string } }';
@@ -275,8 +255,8 @@ public function loadType(string $name, bool $isExecutorCall = false): ?Type
275255
'interfaces' => function (): array {
276256
$this->calls[] = 'SomeObject.interfaces';
277257

278-
/** @var InterfaceType $someInterface */
279258
$someInterface = $this->loadType('SomeInterface');
259+
assert($someInterface instanceof InterfaceType);
280260

281261
return [
282262
$someInterface,
@@ -290,10 +270,11 @@ public function loadType(string $name, bool $isExecutorCall = false): ?Type
290270
'fields' => function (): array {
291271
$this->calls[] = 'OtherObject.fields';
292272

293-
/** @var UnionType $someUnion */
294273
$someUnion = $this->loadType('SomeUnion');
295-
/** @var InterfaceType $someInterface */
274+
assert($someUnion instanceof UnionType);
275+
296276
$someInterface = $this->loadType('SomeInterface');
277+
assert($someInterface instanceof InterfaceType);
297278

298279
return [
299280
'union' => ['type' => $someUnion],
@@ -324,16 +305,16 @@ public function loadType(string $name, bool $isExecutorCall = false): ?Type
324305
'resolveType' => function () {
325306
$this->calls[] = 'SomeUnion.resolveType';
326307

327-
/** @var ObjectType $deeperObject */
328308
$deeperObject = $this->loadType('DeeperObject');
309+
assert($deeperObject instanceof ObjectType);
329310

330311
return $deeperObject;
331312
},
332313
'types' => function (): array {
333314
$this->calls[] = 'SomeUnion.types';
334315

335-
/** @var ObjectType $deeperObject */
336316
$deeperObject = $this->loadType('DeeperObject');
317+
assert($deeperObject instanceof ObjectType);
337318

338319
return [$deeperObject];
339320
},
@@ -345,16 +326,18 @@ public function loadType(string $name, bool $isExecutorCall = false): ?Type
345326
'resolveType' => function () {
346327
$this->calls[] = 'SomeInterface.resolveType';
347328

348-
/** @var ObjectType $someObject */
349329
$someObject = $this->loadType('SomeObject');
330+
assert($someObject instanceof ObjectType);
350331

351332
return $someObject;
352333
},
353334
'fields' => function (): array {
354335
$this->calls[] = 'SomeInterface.fields';
355336

356337
return [
357-
'string' => ['type' => Type::string()],
338+
'string' => [
339+
'type' => Type::string(),
340+
],
358341
];
359342
},
360343
]);
@@ -406,9 +389,7 @@ public function testResolveUnion(): void
406389
{
407390
$schema = new Schema([
408391
'query' => $this->loadType('Query'),
409-
'typeLoader' => function ($name) {
410-
return $this->loadType($name, true);
411-
},
392+
'typeLoader' => fn (string $name): ?Type => $this->loadType($name, true),
412393
]);
413394

414395
$query = '

tests/Executor/LazyInterfaceTest.php

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@
1010
use GraphQL\Type\Schema;
1111
use PHPUnit\Framework\TestCase;
1212

13-
class LazyInterfaceTest extends TestCase
13+
final class LazyInterfaceTest extends TestCase
1414
{
15-
/** @var Schema */
16-
protected $schema;
15+
protected Schema $schema;
1716

18-
/** @var InterfaceType */
19-
protected $lazyInterface;
17+
protected InterfaceType $lazyInterface;
2018

21-
/** @var ObjectType */
22-
protected $testObject;
19+
protected ObjectType $testObject;
2320

24-
/**
25-
* Handles execution of a lazily created interface.
26-
*/
2721
public function testReturnsFragmentsWithLazyCreatedInterface(): void
2822
{
2923
$request = '
@@ -38,67 +32,61 @@ public function testReturnsFragmentsWithLazyCreatedInterface(): void
3832

3933
$expected = [
4034
'data' => [
41-
'lazyInterface' => ['name' => 'testname'],
35+
'lazyInterface' => [
36+
'name' => 'testname',
37+
],
4238
],
4339
];
4440

45-
self::assertEquals($expected, Executor::execute($this->schema, Parser::parse($request))->toArray());
41+
self::assertEquals(
42+
$expected,
43+
Executor::execute($this->schema, Parser::parse($request))->toArray()
44+
);
4645
}
4746

48-
/**
49-
* Setup schema.
50-
*/
5147
protected function setUp(): void
5248
{
5349
$query = new ObjectType([
5450
'name' => 'query',
5551
'fields' => function (): array {
5652
return [
5753
'lazyInterface' => [
58-
'type' => $this->getLazyInterfaceType(),
59-
'resolve' => static function (): array {
60-
return [];
61-
},
54+
'type' => $this->makeLazyInterfaceType(),
55+
'resolve' => static fn (): array => [],
6256
],
6357
];
6458
},
6559
]);
6660

67-
$this->schema = new Schema(['query' => $query, 'types' => [$this->getTestObjectType()]]);
61+
$this->schema = new Schema(
62+
[
63+
'query' => $query,
64+
'types' => [$this->makeTestObjectType()], ]
65+
);
6866
}
6967

70-
/**
71-
* Returns the LazyInterface.
72-
*/
73-
protected function getLazyInterfaceType(): InterfaceType
68+
protected function makeLazyInterfaceType(): InterfaceType
7469
{
7570
return $this->lazyInterface ??= new InterfaceType([
7671
'name' => 'LazyInterface',
7772
'fields' => [
7873
'a' => Type::string(),
7974
],
80-
'resolveType' => function (): ObjectType {
81-
return $this->getTestObjectType();
82-
},
75+
'resolveType' => fn (): ObjectType => $this->makeTestObjectType(),
8376
]);
8477
}
8578

86-
/**
87-
* Returns the test ObjectType.
88-
*/
89-
protected function getTestObjectType(): ObjectType
79+
protected function makeTestObjectType(): ObjectType
9080
{
9181
return $this->testObject ??= new ObjectType([
9282
'name' => 'TestObject',
9383
'fields' => [
9484
'name' => [
9585
'type' => Type::string(),
96-
'resolve' => static function (): string {
97-
return 'testname';
98-
},
86+
'resolve' => static fn (): string => 'testname',
9987
],
10088
],
101-
'interfaces' => [$this->getLazyInterfaceType()],
89+
'interfaces' => [$this->makeLazyInterfaceType()],
10290
]);
10391
}
10492
}

tests/Executor/Promise/AmpPromiseAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @group AmpPromise
2121
*/
22-
class AmpPromiseAdapterTest extends TestCase
22+
final class AmpPromiseAdapterTest extends TestCase
2323
{
2424
public function testIsThenableReturnsTrueWhenAnAmpPromiseIsGiven(): void
2525
{

tests/Type/DefinitionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,9 @@ public function testThrowsWhenLazyLoadedFieldDefinitionHasNoKeysForFieldNames():
19881988
$objType = new ObjectType([
19891989
'name' => 'SomeObject',
19901990
'fields' => [
1991-
static fn (): array => ['type' => Type::string()],
1991+
static fn (): array => [
1992+
'type' => Type::string(),
1993+
],
19921994
],
19931995
]);
19941996

0 commit comments

Comments
 (0)