-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from vossik/sorting
added TypeKindSorter
- Loading branch information
Showing
2 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Graphpinator\Utils\Sort; | ||
|
||
class TypeKindSorter implements PrintSorter | ||
{ | ||
public function sortTypes(array $types) : array | ||
{ | ||
$interface = $union = $input = $enum = $scalar = $object = []; | ||
|
||
foreach ($types as $name => $type) { | ||
switch ($type->getTypeKind()) { | ||
case \Graphpinator\Type\Introspection\TypeKind::INTERFACE: | ||
$interface[$name] = $type; | ||
break; | ||
case \Graphpinator\Type\Introspection\TypeKind::UNION: | ||
$union[$name] = $type; | ||
break; | ||
case \Graphpinator\Type\Introspection\TypeKind::INPUT_OBJECT: | ||
$input[$name] = $type; | ||
break; | ||
case \Graphpinator\Type\Introspection\TypeKind::ENUM: | ||
$enum[$name] = $type; | ||
break; | ||
case \Graphpinator\Type\Introspection\TypeKind::SCALAR: | ||
$scalar[$name] = $type; | ||
break; | ||
case \Graphpinator\Type\Introspection\TypeKind::OBJECT: | ||
$object[$name] = $type; | ||
break; | ||
} | ||
} | ||
|
||
\ksort($interface); | ||
\ksort($union); | ||
\ksort($input); | ||
\ksort($enum); | ||
\ksort($scalar); | ||
\ksort($object); | ||
|
||
return \array_merge($interface, $object, $union, $input, $scalar, $enum); | ||
} | ||
|
||
public function sortDirectives(array $directives) : array | ||
{ | ||
\ksort($directives); | ||
|
||
return $directives; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Graphpinator\Tests\Unit\Utils\Sort; | ||
|
||
final class TypeKindSorterTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function testPrintTestSchema() : void | ||
{ | ||
$expected = <<<EOL | ||
schema { | ||
query: Query | ||
mutation: null | ||
subscription: null | ||
} | ||
interface TestInterface { | ||
name: String! | ||
} | ||
type Abc { | ||
field1(arg1: Int = 123, arg2: TestInput): TestInterface @deprecated | ||
} | ||
type Query { | ||
field0: TestUnion | ||
fieldInvalidType: TestUnion | ||
fieldAbstract: TestUnion | ||
fieldThrow: TestUnion | ||
} | ||
type Xyz implements TestInterface { | ||
name: String! | ||
} | ||
type Zzz { | ||
enumList: [TestEnum] | ||
} | ||
union TestUnion = Abc | Xyz | ||
input TestInnerInput { | ||
name: String! | ||
number: [Int!]! | ||
bool: Boolean | ||
} | ||
input TestInput { | ||
name: String! | ||
inner: TestInnerInput | ||
innerList: [TestInnerInput!]! | ||
innerNotNull: TestInnerInput! | ||
} | ||
enum TestEnum { | ||
A | ||
B | ||
C | ||
D | ||
} | ||
enum TestExplicitEnum { | ||
A @deprecated | ||
B @deprecated | ||
C @deprecated | ||
D @deprecated | ||
} | ||
directive @invalidDirective repeatable on FIELD | ||
directive @testDirective repeatable on FIELD | ||
EOL; | ||
|
||
self::assertSame($expected, \Graphpinator\Tests\Spec\TestSchema::getSchema()->printSchema(new \Graphpinator\Utils\Sort\TypeKindSorter())); | ||
} | ||
|
||
public function testPrintFullTestSchema() : void | ||
{ | ||
$expected = <<<EOL | ||
schema { | ||
query: Query | ||
mutation: Query | ||
subscription: Query | ||
} | ||
interface TestInterface { | ||
name: String! | ||
} | ||
type Abc { | ||
field1(arg1: Int = 123, arg2: TestInput): TestInterface @deprecated | ||
} | ||
type Query { | ||
field0: TestUnion | ||
fieldInvalidType: TestUnion | ||
fieldAbstract: TestUnion | ||
fieldThrow: TestUnion | ||
} | ||
type Xyz implements TestInterface { | ||
name: String! | ||
} | ||
type Zzz { | ||
enumList: [TestEnum] | ||
} | ||
union TestUnion = Abc | Xyz | ||
input TestInnerInput { | ||
name: String! | ||
number: [Int!]! | ||
bool: Boolean | ||
} | ||
input TestInput { | ||
name: String! | ||
inner: TestInnerInput | ||
innerList: [TestInnerInput!]! | ||
innerNotNull: TestInnerInput! | ||
} | ||
enum TestEnum { | ||
A | ||
B | ||
C | ||
D | ||
} | ||
enum TestExplicitEnum { | ||
A @deprecated | ||
B @deprecated | ||
C @deprecated | ||
D @deprecated | ||
} | ||
directive @invalidDirective repeatable on FIELD | ||
directive @testDirective repeatable on FIELD | ||
EOL; | ||
|
||
self::assertSame($expected, \Graphpinator\Tests\Spec\TestSchema::getFullSchema()->printSchema(new \Graphpinator\Utils\Sort\TypeKindSorter())); | ||
} | ||
} |