From 533b8b8b5f499ac06a54baf48be55b72ee3bef0f Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Tue, 21 Nov 2017 12:18:28 -0500 Subject: [PATCH] testDetectsAdditionsToUnionType --- src/Utils/FindBreakingChanges.php | 7 +-- tests/Utils/FindBreakingChangesTest.php | 59 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Utils/FindBreakingChanges.php b/src/Utils/FindBreakingChanges.php index 3880dcb..c747a71 100644 --- a/src/Utils/FindBreakingChanges.php +++ b/src/Utils/FindBreakingChanges.php @@ -433,12 +433,13 @@ class FindBreakingChanges Schema $oldSchema, Schema $newSchema ) { + $oldTypeMap = $oldSchema->getTypeMap(); $newTypeMap = $newSchema->getTypeMap(); $typesAddedToUnion = []; - foreach ($newTypeMap as $typeName => $oldType) { - $newType = isset($newTypeMap[$typeName]) ? $newTypeMap[$typeName] : null; + foreach ($newTypeMap as $typeName => $newType) { + $oldType = isset($oldTypeMap[$typeName]) ? $oldTypeMap[$typeName] : null; if (!($oldType instanceof UnionType) || !($newType instanceof UnionType)) { continue; } @@ -450,7 +451,7 @@ class FindBreakingChanges foreach ($newType->getTypes() as $type) { if (!isset($typeNamesInOldUnion[$type->name])) { $addedTypeName = $type->name; - $typesRemovedFromUnion[] = ['type' => self::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, 'description' => "${addedTypeName} was removed to union type ${typeName}"]; + $typesAddedToUnion[] = ['type' => self::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, 'description' => "${addedTypeName} was added to union type ${typeName}"]; } } } diff --git a/tests/Utils/FindBreakingChangesTest.php b/tests/Utils/FindBreakingChangesTest.php index a15b093..0d461f4 100644 --- a/tests/Utils/FindBreakingChangesTest.php +++ b/tests/Utils/FindBreakingChangesTest.php @@ -1326,4 +1326,63 @@ class FindBreakingChangesTest extends \PHPUnit_Framework_TestCase FindBreakingChanges::findValuesAddedToEnums($oldSchema, $newSchema)[0] ); } + + public function testDetectsAdditionsToUnionType() { + $type1 = new ObjectType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => Type::string() + ] + ]); + + $type1a = new ObjectType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => Type::string() + ] + ]); + + $type2 = new ObjectType([ + 'name' => 'Type2', + 'fields' => [ + 'field1' => Type::string() + ] + ]); + + $oldUnionType = new UnionType([ + 'name' => 'UnionType1', + 'types' => [$type1], + 'resolveType' => function () { + } + ]); + + $newUnionType = new UnionType([ + 'name' => 'UnionType1', + 'types' => [$type1a, $type2], + 'resolveType' => function () { + } + ]); + + $oldSchema = new Schema([ + 'query' => $this->queryType, + 'types' => [ + $oldUnionType + ] + ]); + + $newSchema = new Schema([ + 'query' => $this->queryType, + 'types' => [ + $newUnionType + ] + ]); + + $this->assertEquals( + [ + 'type' => FindBreakingChanges::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, + 'description' => 'Type2 was added to union type UnionType1' + ], + FindBreakingChanges::findTypesAddedToUnions($oldSchema, $newSchema)[0] + ); + } } \ No newline at end of file