From d97fac6ab0811a2923766cf1fbf2aa2fe42204bf Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 10 Jan 2019 14:27:04 +0100 Subject: [PATCH] Invert instance of in BCFinder --- src/Utils/BreakingChangesFinder.php | 30 ++++++++++------- tests/Utils/BreakingChangesFinderTest.php | 41 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/Utils/BreakingChangesFinder.php b/src/Utils/BreakingChangesFinder.php index 597cd16..93736b6 100644 --- a/src/Utils/BreakingChangesFinder.php +++ b/src/Utils/BreakingChangesFinder.php @@ -112,27 +112,31 @@ class BreakingChangesFinder * @return string[][] */ public static function findTypesThatChangedKind( - Schema $oldSchema, - Schema $newSchema - ) { - $oldTypeMap = $oldSchema->getTypeMap(); - $newTypeMap = $newSchema->getTypeMap(); + Schema $schemaA, + Schema $schemaB + ) : iterable { + $schemaATypeMap = $schemaA->getTypeMap(); + $schemaBTypeMap = $schemaB->getTypeMap(); $breakingChanges = []; - foreach ($oldTypeMap as $typeName => $oldType) { - if (! isset($newTypeMap[$typeName])) { + foreach ($schemaATypeMap as $typeName => $schemaAType) { + if (! isset($schemaBTypeMap[$typeName])) { continue; } - $newType = $newTypeMap[$typeName]; - if ($oldType instanceof $newType) { + $schemaBType = $schemaBTypeMap[$typeName]; + if ($schemaAType instanceof $schemaBType) { continue; } - $oldTypeKindName = self::typeKindName($oldType); - $newTypeKindName = self::typeKindName($newType); - $breakingChanges[] = [ + if ($schemaBType instanceof $schemaAType) { + continue; + } + + $schemaATypeKindName = self::typeKindName($schemaAType); + $schemaBTypeKindName = self::typeKindName($schemaBType); + $breakingChanges[] = [ 'type' => self::BREAKING_CHANGE_TYPE_CHANGED_KIND, - 'description' => "${typeName} changed from ${oldTypeKindName} to ${newTypeKindName}.", + 'description' => "${typeName} changed from ${schemaATypeKindName} to ${schemaBTypeKindName}.", ]; } diff --git a/tests/Utils/BreakingChangesFinderTest.php b/tests/Utils/BreakingChangesFinderTest.php index 91eb677..898bc29 100644 --- a/tests/Utils/BreakingChangesFinderTest.php +++ b/tests/Utils/BreakingChangesFinderTest.php @@ -125,6 +125,47 @@ class BreakingChangesFinderTest extends TestCase ); } + /** + * We need to compare type of class A (old type) and type of class B (new type) + * Class B extends A but are evaluated as same types (if all properties match). + * The reason is that when constructing schema from remote schema, + * we have no certain way to get information about our classes. + * Thus object types from remote schema are constructed as Object Type + * while their local counterparts are usually a subclass of Object Type. + * + * @see https://github.com/webonyx/graphql-php/pull/431 + */ + public function testShouldNotMarkTypesWithInheritedClassesAsChanged() : void + { + $objectTypeConstructedFromRemoteSchema = new ObjectType([ + 'name' => 'ObjectType', + 'fields' => [ + 'field1' => ['type' => Type::string()], + ], + ]); + + $localObjectType = new class([ + 'name' => 'ObjectType', + 'fields' => [ + 'field1' => ['type' => Type::string()], + ], + ]) extends ObjectType{ + }; + + $schemaA = new Schema([ + 'query' => $this->queryType, + 'types' => [$objectTypeConstructedFromRemoteSchema], + ]); + + $schemaB = new Schema([ + 'query' => $this->queryType, + 'types' => [$localObjectType], + ]); + + self::assertEmpty(BreakingChangesFinder::findTypesThatChangedKind($schemaA, $schemaB)); + self::assertEmpty(BreakingChangesFinder::findTypesThatChangedKind($schemaB, $schemaA)); + } + /** * @see it('should detect if a field on a type was deleted or changed type') */