From 68dbcc9ca385b2aca83fd0cea435456a49e882e5 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Fri, 17 Nov 2017 14:29:47 -0500 Subject: [PATCH] testShouldDetectFieldChangesAndDeletions test --- src/Utils/FindBreakingChanges.php | 4 +- tests/Utils/FindBreakingChangesTest.php | 147 ++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/Utils/FindBreakingChanges.php b/src/Utils/FindBreakingChanges.php index 51134c4..4077b1d 100644 --- a/src/Utils/FindBreakingChanges.php +++ b/src/Utils/FindBreakingChanges.php @@ -175,7 +175,7 @@ class FindBreakingChanges $newArgType = $newArgDef->getType(); $breakingChanges[] = [ 'type' => self::BREAKING_CHANGE_ARG_CHANGED, - 'description' => "${oldTypeName}->${fieldName} arg ${oldArgName} has changed type from ${oldArgType} to ${newArgType}" + 'description' => "${oldTypeName}->${fieldName} arg ${oldArgName} has changed type from ${oldArgType} to ${newArgType}." ]; } elseif ($oldArgDef->defaultValueExists() && $oldArgDef->defaultValue !== $newArgDef->defaultValue) { $dangerousChanges[] = []; // TODO @@ -278,7 +278,7 @@ class FindBreakingChanges $oldFieldTypeString = self::isNamedType($oldFieldType) ? $oldFieldType->name : $oldFieldType; $newFieldTypeString = self::isNamedType($newfieldType) ? $newfieldType->name : $newfieldType; - $breakingFieldChanges[] = ['type' => self::BREAKING_CHANGE_FIELD_CHANGED, 'description' => "${typeName}->${fieldName} changed type from ${oldFieldTypeString} to ${newFieldTypeString}"]; + $breakingFieldChanges[] = ['type' => self::BREAKING_CHANGE_FIELD_CHANGED, 'description' => "${typeName}->${fieldName} changed type from ${oldFieldTypeString} to ${newFieldTypeString}."]; } } } diff --git a/tests/Utils/FindBreakingChangesTest.php b/tests/Utils/FindBreakingChangesTest.php index e2cc9b2..34334c9 100644 --- a/tests/Utils/FindBreakingChangesTest.php +++ b/tests/Utils/FindBreakingChangesTest.php @@ -111,4 +111,151 @@ class FindBreakingChangesTest extends \PHPUnit_Framework_TestCase ); } + public function testShouldDetectFieldChangesAndDeletions() + { + $typeA1 = new ObjectType([ + 'name' => 'TypeA', + 'fields' => [ + 'field1' => ['type' => Type::string()], + ] + ]); + $typeA2 = new ObjectType([ + 'name' => 'TypeA', + 'fields' => [ + 'field1' => ['type' => Type::string()], + ] + ]); + $typeB = new ObjectType([ + 'name' => 'TypeB', + 'fields' => [ + 'field1' => ['type' => Type::string()], + ] + ]); + $oldType1 = new InterfaceType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => ['type' => $typeA1], + 'field2' => ['type' => Type::string()], + 'field3' => ['type' => Type::string()], + 'field4' => ['type' => $typeA1], + 'field6' => ['type' => Type::string()], + 'field7' => ['type' => Type::listOf(Type::string())], + 'field8' => ['type' => Type::int()], + 'field9' => ['type' => Type::nonNull(Type::int())], + 'field10' => ['type' => Type::nonNull(Type::listOf(Type::int()))], + 'field11' => ['type' => Type::int()], + 'field12' => ['type' => Type::listOf(Type::int())], + 'field13' => ['type' => Type::listOf(Type::nonNull(Type::int()))], + 'field14' => ['type' => Type::listOf(Type::int())], + 'field15' => ['type' => Type::listOf(Type::listOf(Type::int()))], + 'field16' => ['type' => Type::nonNull(Type::int())], + 'field17' => ['type' => Type::listOf(Type::int())], + 'field18' => [ + 'type' => Type::listOf(Type::nonNull( + Type::listOf(Type::nonNull(Type::int())))), + ], + ] + ]); + $newType1 = new InterfaceType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => ['type' => $typeA2], + 'field3' => ['type' => Type::boolean()], + 'field4' => ['type' => $typeB], + 'field5' => ['type' => Type::string()], + 'field6' => ['type' => Type::listOf(Type::string())], + 'field7' => ['type' => Type::string()], + 'field8' => ['type' => Type::nonNull(Type::int())], + 'field9' => ['type' => Type::int()], + 'field10' => ['type' => Type::listOf(Type::int())], + 'field11' => ['type' => Type::nonNull(Type::listOf(Type::int()))], + 'field12' => ['type' => Type::listOf(Type::nonNull(Type::int()))], + 'field13' => ['type' => Type::listOf(Type::int())], + 'field14' => ['type' => Type::listOf(Type::listOf(Type::int()))], + 'field15' => ['type' => Type::listOf(Type::int())], + 'field16' => ['type' => Type::nonNull(Type::listOf(Type::int()))], + 'field17' => ['type' => Type::nonNull(Type::listOf(Type::int()))], + 'field18' => [ + 'type' => Type::listOf( + Type::listOf(Type::nonNull(Type::int()))), + ], + ] + ]); + + $expectedFieldChanges = [ + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_REMOVED, + 'description' => 'Type1->field2 was removed.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field3 changed type from String to Boolean.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field4 changed type from TypeA to TypeB.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field6 changed type from String to [String].', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field7 changed type from [String] to String.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field9 changed type from Int! to Int.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field10 changed type from [Int]! to [Int].', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field11 changed type from Int to [Int]!.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field13 changed type from [Int!] to [Int].', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field14 changed type from [Int] to [[Int]].', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field15 changed type from [[Int]] to [Int].', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field16 changed type from Int! to [Int]!.', + ], + [ + 'type' => FindBreakingChanges::BREAKING_CHANGE_FIELD_CHANGED, + 'description' => 'Type1->field18 changed type from [[Int!]!] to [[Int!]].', + ], + ]; + + $oldSchema = new Schema([ + 'query' => new ObjectType([ + 'name' => 'root', + 'fields' => [ + 'Type1' => $oldType1 + ] + ]) + ]); + + $newSchema = new Schema([ + 'query' => new ObjectType([ + 'name' => 'root', + 'fields' => [ + 'Type1' => $newType1 + ] + ]) + ]); + + $this->assertEquals($expectedFieldChanges, FindBreakingChanges::findFieldsThatChangedType($oldSchema, $newSchema)); + } + } \ No newline at end of file