graphql-php/tests/Utils/BreakingChangesFinderTest.php

1929 lines
64 KiB
PHP
Raw Normal View History

2017-11-17 19:21:05 +03:00
<?php
2018-08-22 02:20:49 +03:00
2018-09-02 13:44:21 +03:00
declare(strict_types=1);
2017-11-17 19:21:05 +03:00
namespace GraphQL\Tests\Utils;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive;
2017-11-18 00:24:04 +03:00
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldArgument;
2017-11-17 23:43:16 +03:00
use GraphQL\Type\Definition\InputObjectType;
2017-11-17 21:04:01 +03:00
use GraphQL\Type\Definition\InterfaceType;
2017-11-17 19:21:05 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2017-11-17 21:04:01 +03:00
use GraphQL\Type\Definition\UnionType;
2017-11-17 19:21:05 +03:00
use GraphQL\Type\Schema;
2018-08-22 02:20:49 +03:00
use GraphQL\Utils\BreakingChangesFinder;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-02 13:44:21 +03:00
use function sprintf;
2017-11-17 19:21:05 +03:00
2018-08-22 02:20:49 +03:00
class BreakingChangesFinderTest extends TestCase
2017-11-17 19:21:05 +03:00
{
2018-09-02 13:44:21 +03:00
/** @var ObjectType */
2017-11-21 19:33:45 +03:00
private $queryType;
2017-11-17 19:21:05 +03:00
public function setUp()
{
$this->queryType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Query',
2017-11-17 19:21:05 +03:00
'fields' => [
'field1' => [
2018-09-02 13:44:21 +03:00
'type' => Type::string(),
],
],
2017-11-17 19:21:05 +03:00
]);
}
//DESCRIBE: findBreakingChanges
/**
* @see it('should detect if a type was removed or not')
*/
public function testShouldDetectIfTypeWasRemovedOrNot() : void
2017-11-17 19:21:05 +03:00
{
2018-09-02 13:44:21 +03:00
$type1 = new ObjectType([
'name' => 'Type1',
2017-11-17 19:21:05 +03:00
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
2017-11-17 19:21:05 +03:00
]);
2018-09-02 13:44:21 +03:00
$type2 = new ObjectType([
'name' => 'Type2',
2017-11-17 19:21:05 +03:00
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
2017-11-17 19:21:05 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$type1, $type2],
2017-11-17 19:21:05 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$type2],
2017-11-17 19:21:05 +03:00
]);
$expected = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED,
'description' => 'Type1 was removed.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findRemovedTypes($oldSchema, $newSchema)
2017-11-17 19:21:05 +03:00
);
2018-08-22 02:20:49 +03:00
$this->assertEquals([], BreakingChangesFinder::findRemovedTypes($oldSchema, $oldSchema));
2017-11-17 19:21:05 +03:00
}
/**
* @see it('should detect if a type changed its type')
*/
public function testShouldDetectIfATypeChangedItsType() : void
2017-11-17 21:04:01 +03:00
{
$objectType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'ObjectType',
2017-11-17 21:04:01 +03:00
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
2017-11-17 21:04:01 +03:00
]);
$interfaceType = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-17 21:04:01 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => ['type' => Type::string()],
],
2017-11-17 21:04:01 +03:00
]);
$unionType = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'types' => [$objectType],
2017-11-17 21:04:01 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$interfaceType],
2017-11-17 21:04:01 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$unionType],
2017-11-17 21:04:01 +03:00
]);
$expected = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND,
'description' => 'Type1 changed from an Interface type to a Union type.',
],
];
2017-11-17 21:04:01 +03:00
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findTypesThatChangedKind($oldSchema, $newSchema)
2017-11-17 21:04:01 +03:00
);
}
/**
* @see it('should detect if a field on a type was deleted or changed type')
*/
public function testShouldDetectIfAFieldOnATypeWasDeletedOrChangedType() : void
{
$typeA = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeA',
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
]);
// logically equivalent to TypeA; findBreakingFieldChanges shouldn't
// treat this as different than TypeA
2018-09-02 13:44:21 +03:00
$typeA2 = new ObjectType([
'name' => 'TypeA',
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
]);
2018-09-02 13:44:21 +03:00
$typeB = new ObjectType([
'name' => 'TypeB',
'fields' => [
'field1' => ['type' => Type::string()],
2018-09-02 13:44:21 +03:00
],
]);
$oldType1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'fields' => [
'field1' => ['type' => $typeA],
'field2' => ['type' => Type::string()],
'field3' => ['type' => Type::string()],
'field4' => ['type' => $typeA],
'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(
2018-09-02 13:44:21 +03:00
Type::listOf(Type::nonNull(Type::int()))
)),
],
2018-09-02 13:44:21 +03:00
],
]);
$newType1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'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(
2018-09-02 13:44:21 +03:00
Type::listOf(Type::nonNull(Type::int()))
),
],
2018-09-02 13:44:21 +03:00
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType1],
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType1],
]);
$expectedFieldChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED,
'description' => 'Type1.field2 was removed.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field3 changed type from String to Boolean.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field4 changed type from TypeA to TypeB.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field6 changed type from String to [String].',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field7 changed type from [String] to String.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field9 changed type from Int! to Int.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field10 changed type from [Int]! to [Int].',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field11 changed type from Int to [Int]!.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field13 changed type from [Int!] to [Int].',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field14 changed type from [Int] to [[Int]].',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field15 changed type from [[Int]] to [Int].',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field16 changed type from Int! to [Int]!.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'Type1.field18 changed type from [[Int!]!] to [[Int!]].',
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedFieldChanges,
BreakingChangesFinder::findFieldsThatChangedTypeOnObjectOrInterfaceTypes($oldSchema, $newSchema)
);
}
/**
* @see it('should detect if fields on input types changed kind or were removed')
*/
public function testShouldDetectIfFieldsOnInputTypesChangedKindOrWereRemoved() : void
2017-11-17 23:43:16 +03:00
{
$oldInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
2017-11-17 23:43:16 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => [
2017-11-17 23:43:16 +03:00
'type' => Type::string(),
],
2018-09-02 13:44:21 +03:00
'field2' => [
2017-11-17 23:43:16 +03:00
'type' => Type::boolean(),
],
2018-09-02 13:44:21 +03:00
'field3' => [
'type' => Type::listOf(Type::string()),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field4' => [
2017-11-17 23:43:16 +03:00
'type' => Type::nonNull(Type::string()),
],
2018-09-02 13:44:21 +03:00
'field5' => [
2017-11-17 23:43:16 +03:00
'type' => Type::string(),
],
2018-09-02 13:44:21 +03:00
'field6' => [
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field7' => [
'type' => Type::nonNull(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field8' => [
2017-11-17 23:43:16 +03:00
'type' => Type::int(),
],
2018-09-02 13:44:21 +03:00
'field9' => [
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
'field10' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::nonNull(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field11' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
'field12' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field13' => [
2018-09-02 13:44:21 +03:00
'type' => Type::nonNull(Type::int()),
2017-11-17 23:43:16 +03:00
],
'field14' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))),
2017-11-17 23:43:16 +03:00
],
'field15' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))),
],
],
2017-11-17 23:43:16 +03:00
]);
$newInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
2017-11-17 23:43:16 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => [
2017-11-17 23:43:16 +03:00
'type' => Type::int(),
],
2018-09-02 13:44:21 +03:00
'field3' => [
'type' => Type::string(),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field4' => [
'type' => Type::string(),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field5' => [
'type' => Type::nonNull(Type::string()),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field6' => [
'type' => Type::nonNull(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field7' => [
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field8' => [
'type' => Type::nonNull(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
2018-09-02 13:44:21 +03:00
'field9' => [
'type' => Type::listOf(Type::nonNull(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field10' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
'field11' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field12' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::int()),
2017-11-17 23:43:16 +03:00
],
'field13' => [
2018-09-02 13:44:21 +03:00
'type' => Type::nonNull(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field14' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::listOf(Type::int())),
2017-11-17 23:43:16 +03:00
],
'field15' => [
2018-09-02 13:44:21 +03:00
'type' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))),
],
],
2017-11-17 23:43:16 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$oldInputType],
2017-11-17 23:43:16 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$newInputType],
2017-11-17 23:43:16 +03:00
]);
$expectedFieldChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field1 changed type from String to Int.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED,
'description' => 'InputType1.field2 was removed.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field3 changed type from [String] to String.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field5 changed type from String to String!.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field6 changed type from [Int] to [Int]!.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field8 changed type from Int to [Int]!.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field9 changed type from [Int] to [Int!].',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field11 changed type from [Int] to [[Int]].',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field12 changed type from [[Int]] to [Int].',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field13 changed type from Int! to [Int]!.',
2017-11-17 23:43:16 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'InputType1.field15 changed type from [[Int]!] to [[Int!]!].',
2017-11-17 23:43:16 +03:00
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedFieldChanges,
BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes(
$oldSchema,
$newSchema
)['breakingChanges']
);
2017-11-17 23:43:16 +03:00
}
/**
* @see it('should detect if a non-null field is added to an input type')
*/
public function testShouldDetectIfANonNullFieldIsAddedToAnInputType() : void
{
$oldInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
]);
$newInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
'requiredField' => Type::nonNull(Type::int()),
2018-09-02 13:44:21 +03:00
'optionalField' => Type::boolean(),
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldInputType],
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newInputType],
]);
$expected = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_INPUT_FIELD_ADDED,
'description' => 'A non-null field requiredField on input type InputType1 was added.',
],
];
$this->assertEquals(
$expected,
2018-09-02 13:44:21 +03:00
BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes(
$oldSchema,
$newSchema
)['breakingChanges']
);
}
2017-11-18 00:08:44 +03:00
/**
* @see it('should detect if a type was removed from a union type')
*/
public function testShouldRetectIfATypeWasRemovedFromAUnionType() : void
2017-11-18 00:08:44 +03:00
{
$type1 = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-18 00:08:44 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 00:08:44 +03:00
]);
// logially equivalent to type1; findTypesRemovedFromUnions should not
// treat this as different than type1
2017-11-18 00:08:44 +03:00
$type1a = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-18 00:08:44 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 00:08:44 +03:00
]);
2018-09-02 13:44:21 +03:00
$type2 = new ObjectType([
'name' => 'Type2',
2017-11-18 00:08:44 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 00:08:44 +03:00
]);
2018-09-02 13:44:21 +03:00
$type3 = new ObjectType([
'name' => 'Type3',
2017-11-18 00:08:44 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 00:08:44 +03:00
]);
$oldUnionType = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionType1',
2017-11-18 00:08:44 +03:00
'types' => [$type1, $type2],
]);
$newUnionType = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionType1',
2017-11-18 00:08:44 +03:00
'types' => [$type1a, $type3],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldUnionType],
2017-11-18 00:08:44 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newUnionType],
2017-11-18 00:08:44 +03:00
]);
$expected = [
2017-11-18 00:08:44 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION,
'description' => 'Type2 was removed from union type UnionType1.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findTypesRemovedFromUnions($oldSchema, $newSchema)
2017-11-18 00:08:44 +03:00
);
}
2017-11-18 00:24:04 +03:00
/**
* @see it('should detect if a value was removed from an enum type')
*/
public function testShouldDetectIfAValueWasRemovedFromAnEnumType() : void
2017-11-18 00:24:04 +03:00
{
$oldEnumType = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-18 00:24:04 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
'VALUE2' => 2,
],
2017-11-18 00:24:04 +03:00
]);
$newEnumType = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-18 00:24:04 +03:00
'values' => [
'VALUE0' => 0,
'VALUE2' => 1,
2018-09-02 13:44:21 +03:00
'VALUE3' => 2,
],
2017-11-18 00:24:04 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$oldEnumType],
2017-11-18 00:24:04 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$newEnumType],
2017-11-18 00:24:04 +03:00
]);
$expected = [
2017-11-18 00:24:04 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM,
'description' => 'VALUE1 was removed from enum type EnumType1.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findValuesRemovedFromEnums($oldSchema, $newSchema)
2017-11-18 00:24:04 +03:00
);
}
2017-11-18 01:18:26 +03:00
/**
* @see it('should detect if a field argument was removed')
*/
public function testShouldDetectIfAFieldArgumentWasRemoved() : void
2017-11-18 01:18:26 +03:00
{
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-18 01:18:26 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'name' => Type::string(),
],
],
],
2017-11-18 01:18:26 +03:00
]);
$inputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
2017-11-18 01:18:26 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 01:18:26 +03:00
]);
$oldInterfaceType = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
2017-11-18 01:18:26 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::boolean(),
'objectArg' => $inputType,
],
],
],
2017-11-18 01:18:26 +03:00
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-18 01:18:26 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
2018-09-02 13:44:21 +03:00
'args' => [],
],
],
2017-11-18 01:18:26 +03:00
]);
$newInterfaceType = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
2017-11-18 01:18:26 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-18 01:18:26 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType, $oldInterfaceType],
2017-11-18 01:18:26 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType, $newInterfaceType],
2017-11-18 01:18:26 +03:00
]);
$expectedChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED,
'description' => 'Type1.field1 arg name was removed',
2017-11-18 01:18:26 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED,
'description' => 'Interface1.field1 arg arg1 was removed',
2017-11-18 01:18:26 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED,
'description' => 'Interface1.field1 arg objectArg was removed',
2018-09-02 13:44:21 +03:00
],
2017-11-18 01:18:26 +03:00
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedChanges,
BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']
);
2017-11-18 01:18:26 +03:00
}
2017-11-18 01:35:33 +03:00
/**
* @see it('should detect if a field argument has changed type')
*/
public function testShouldDetectIfAFieldArgumentHasChangedType() : void
2017-11-20 20:52:49 +03:00
{
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-20 20:52:49 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::string(),
'arg2' => Type::string(),
'arg3' => Type::listOf(Type::string()),
'arg4' => Type::string(),
'arg5' => Type::nonNull(Type::string()),
'arg6' => Type::nonNull(Type::string()),
'arg7' => Type::nonNull(Type::listOf(Type::int())),
'arg8' => Type::int(),
'arg9' => Type::listOf(Type::int()),
2017-11-20 20:52:49 +03:00
'arg10' => Type::listOf(Type::nonNull(Type::int())),
'arg11' => Type::listOf(Type::int()),
'arg12' => Type::listOf(Type::listOf(Type::int())),
'arg13' => Type::nonNull(Type::int()),
'arg14' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))),
2018-09-02 13:44:21 +03:00
'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))),
],
],
],
2017-11-20 20:52:49 +03:00
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-20 20:52:49 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::int(),
'arg2' => Type::listOf(Type::string()),
'arg3' => Type::string(),
'arg4' => Type::nonNull(Type::string()),
'arg5' => Type::int(),
'arg6' => Type::nonNull(Type::int()),
'arg7' => Type::listOf(Type::int()),
'arg8' => Type::nonNull(Type::listOf(Type::int())),
'arg9' => Type::listOf(Type::nonNull(Type::int())),
2017-11-20 20:52:49 +03:00
'arg10' => Type::listOf(Type::int()),
'arg11' => Type::listOf(Type::listOf(Type::int())),
'arg12' => Type::listOf(Type::int()),
'arg13' => Type::nonNull(Type::listOf(Type::int())),
'arg14' => Type::listOf(Type::listOf(Type::int())),
2018-09-02 13:44:21 +03:00
'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))),
],
],
],
2017-11-20 20:52:49 +03:00
]);
2017-11-18 01:35:33 +03:00
2017-11-20 20:52:49 +03:00
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$oldType],
2017-11-20 20:52:49 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$newType],
2017-11-20 20:52:49 +03:00
]);
$expectedChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg1 has changed type from String to Int',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg2 has changed type from String to [String]',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg3 has changed type from [String] to String',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg4 has changed type from String to String!',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg5 has changed type from String! to Int',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg6 has changed type from String! to Int!',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg8 has changed type from Int to [Int]!',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg9 has changed type from [Int] to [Int!]',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg11 has changed type from [Int] to [[Int]]',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg12 has changed type from [[Int]] to [Int]',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg13 has changed type from Int! to [Int]!',
2017-11-20 20:52:49 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'Type1.field1 arg arg15 has changed type from [[Int]!] to [[Int!]!]',
2017-11-20 20:52:49 +03:00
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedChanges,
BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']
);
2017-11-18 01:35:33 +03:00
}
2017-11-20 21:48:21 +03:00
/**
* @see it('should detect if a non-null field argument was added')
*/
public function testShouldDetectIfANonNullFieldArgumentWasAdded() : void
2017-11-20 21:48:21 +03:00
{
2018-09-02 13:44:21 +03:00
$oldType = new ObjectType([
'name' => 'Type1',
2017-11-20 21:48:21 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::string(),
],
],
],
2017-11-20 21:48:21 +03:00
]);
2018-09-02 13:44:21 +03:00
$newType = new ObjectType([
'name' => 'Type1',
2017-11-20 21:48:21 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::string(),
2017-11-20 21:48:21 +03:00
'newRequiredArg' => Type::nonNull(Type::string()),
2018-09-02 13:44:21 +03:00
'newOptionalArg' => Type::int(),
],
],
],
2017-11-20 21:48:21 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$oldType],
2017-11-20 21:48:21 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$newType],
2017-11-20 21:48:21 +03:00
]);
$expected = [
2017-11-20 21:48:21 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_ARG_ADDED,
'description' => 'A non-null arg newRequiredArg on Type1.field1 was added',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']
);
2017-11-20 21:48:21 +03:00
}
/**
* @see it('should not flag args with the same type signature as breaking')
*/
public function testShouldNotFlagArgsWithTheSameTypeSignatureAsBreaking() : void
2017-11-20 22:39:06 +03:00
{
$inputType1a = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
]);
$inputType1b = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
]);
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'fields' => [
'field1' => [
'type' => Type::int(),
'args' => [
'arg1' => Type::nonNull(Type::int()),
2018-09-02 13:44:21 +03:00
'arg2' => $inputType1a,
],
],
],
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'fields' => [
'field1' => [
'type' => Type::int(),
'args' => [
'arg1' => Type::nonNull(Type::int()),
2018-09-02 13:44:21 +03:00
'arg2' => $inputType1b,
],
],
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
]);
2018-08-22 02:20:49 +03:00
$this->assertEquals([], BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']);
}
2017-11-20 22:12:26 +03:00
/**
* @see it('should consider args that move away from NonNull as non-breaking')
*/
public function testShouldConsiderArgsThatMoveAwayFromNonNullAsNonBreaking() : void
2017-11-20 22:39:06 +03:00
{
2017-11-20 22:12:26 +03:00
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-20 22:12:26 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'arg1' => Type::nonNull(Type::string()),
2018-09-02 13:44:21 +03:00
],
],
],
2017-11-20 22:12:26 +03:00
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-20 22:12:26 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'arg1' => Type::string(),
],
],
],
2017-11-20 22:12:26 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
2017-11-20 22:12:26 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
2017-11-20 22:12:26 +03:00
]);
2018-08-22 02:20:49 +03:00
$this->assertEquals([], BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']);
2017-11-20 22:12:26 +03:00
}
2017-11-20 22:39:06 +03:00
/**
* @see it('should detect interfaces removed from types')
*/
public function testShouldDetectInterfacesRemovedFromTypes() : void
2017-11-20 22:39:06 +03:00
{
$interface1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
2017-11-20 22:39:06 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
2017-11-20 22:39:06 +03:00
],
]);
2018-09-02 13:44:21 +03:00
$oldType = new ObjectType([
'name' => 'Type1',
2017-11-20 22:39:06 +03:00
'interfaces' => [$interface1],
2018-09-02 13:44:21 +03:00
'fields' => [
'field1' => Type::string(),
],
2017-11-20 22:39:06 +03:00
]);
2018-09-02 13:44:21 +03:00
$newType = new ObjectType([
'name' => 'Type1',
2017-11-20 22:39:06 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-20 22:39:06 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
2017-11-20 22:39:06 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
2017-11-20 22:39:06 +03:00
]);
$expected = [
2017-11-20 22:39:06 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT,
'description' => 'Type1 no longer implements interface Interface1.',
2017-11-20 22:39:06 +03:00
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findInterfacesRemovedFromObjectTypes($oldSchema, $newSchema)
);
2017-11-20 22:39:06 +03:00
}
2017-11-21 19:33:45 +03:00
/**
* @see it('should detect all breaking changes')
*/
public function testShouldDetectAllBreakingChanges() : void
2017-11-21 19:33:45 +03:00
{
$typeThatGetsRemoved = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatGetsRemoved',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$argThatChanges = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'ArgThatChanges',
2017-11-21 19:33:45 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'id' => Type::int(),
],
],
],
2017-11-21 19:33:45 +03:00
]);
$argChanged = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'ArgThatChanges',
2017-11-21 19:33:45 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
2018-09-02 13:44:21 +03:00
'id' => Type::string(),
],
],
],
2017-11-21 19:33:45 +03:00
]);
$typeThatChangesTypeOld = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatChangesType',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$typeThatChangesTypeNew = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatChangesType',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$typeThatHasBreakingFieldChangesOld = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatHasBreakingFieldChanges',
2017-11-21 19:33:45 +03:00
'fields' => [
'field1' => Type::string(),
2018-09-02 13:44:21 +03:00
'field2' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$typeThatHasBreakingFieldChangesNew = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatHasBreakingFieldChanges',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field2' => Type::boolean(),
],
2017-11-21 19:33:45 +03:00
]);
$typeInUnion1 = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeInUnion1',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$typeInUnion2 = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeInUnion2',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$unionTypeThatLosesATypeOld = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionTypeThatLosesAType',
2017-11-21 19:33:45 +03:00
'types' => [$typeInUnion1, $typeInUnion2],
]);
$unionTypeThatLosesATypeNew = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionTypeThatLosesAType',
2017-11-21 19:33:45 +03:00
'types' => [$typeInUnion1],
]);
$enumTypeThatLosesAValueOld = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumTypeThatLosesAValue',
2017-11-21 19:33:45 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
'VALUE2' => 2,
],
2017-11-21 19:33:45 +03:00
]);
$enumTypeThatLosesAValueNew = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumTypeThatLosesAValue',
2017-11-21 19:33:45 +03:00
'values' => [
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
'VALUE2' => 2,
],
2017-11-21 19:33:45 +03:00
]);
$interface1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
2017-11-21 19:33:45 +03:00
],
]);
$typeThatLosesInterfaceOld = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatLosesInterface1',
2017-11-21 19:33:45 +03:00
'interfaces' => [$interface1],
2018-09-02 13:44:21 +03:00
'fields' => [
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
$typeThatLosesInterfaceNew = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatLosesInterface1',
2017-11-21 19:33:45 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 19:33:45 +03:00
]);
2018-09-02 13:44:21 +03:00
$directiveThatIsRemoved = Directive::skipDirective();
$directiveThatRemovesArgOld = new Directive([
'name' => 'DirectiveThatRemovesArg',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
'args' => FieldArgument::createMap([
'arg1' => ['name' => 'arg1'],
]),
]);
2018-09-02 13:44:21 +03:00
$directiveThatRemovesArgNew = new Directive([
'name' => 'DirectiveThatRemovesArg',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
]);
2018-09-02 13:44:21 +03:00
$nonNullDirectiveAddedOld = new Directive([
'name' => 'NonNullDirectiveAdded',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
]);
2018-09-02 13:44:21 +03:00
$nonNullDirectiveAddedNew = new Directive([
'name' => 'NonNullDirectiveAdded',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
'args' => FieldArgument::createMap([
'arg1' => [
'name' => 'arg1',
'type' => Type::nonNull(Type::boolean()),
],
]),
]);
$directiveRemovedLocationOld = new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [DirectiveLocation::FIELD_DEFINITION, DirectiveLocation::QUERY],
]);
$directiveRemovedLocationNew = new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
]);
2017-11-21 19:33:45 +03:00
$oldSchema = new Schema([
2018-09-02 13:44:21 +03:00
'query' => $this->queryType,
'types' => [
$typeThatGetsRemoved,
$typeThatChangesTypeOld,
$typeThatHasBreakingFieldChangesOld,
$unionTypeThatLosesATypeOld,
$enumTypeThatLosesAValueOld,
$argThatChanges,
2018-09-02 13:44:21 +03:00
$typeThatLosesInterfaceOld,
],
'directives' => [
$directiveThatIsRemoved,
$directiveThatRemovesArgOld,
$nonNullDirectiveAddedOld,
$directiveRemovedLocationOld,
2018-09-02 13:44:21 +03:00
],
2017-11-21 19:33:45 +03:00
]);
$newSchema = new Schema([
2018-09-02 13:44:21 +03:00
'query' => $this->queryType,
'types' => [
$typeThatChangesTypeNew,
$typeThatHasBreakingFieldChangesNew,
$unionTypeThatLosesATypeNew,
$enumTypeThatLosesAValueNew,
$argChanged,
$typeThatLosesInterfaceNew,
2018-09-02 13:44:21 +03:00
$interface1,
],
'directives' => [
$directiveThatRemovesArgNew,
$nonNullDirectiveAddedNew,
$directiveRemovedLocationNew,
2018-09-02 13:44:21 +03:00
],
2017-11-21 19:33:45 +03:00
]);
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED,
2017-11-21 19:33:45 +03:00
'description' => 'TypeThatGetsRemoved was removed.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED,
2017-11-21 19:33:45 +03:00
'description' => 'TypeInUnion2 was removed.',
],
/* This is reported in the js version because builtin sclar types are added on demand
and not like here always
2017-11-21 19:33:45 +03:00
[
'type' => FindBreakingChanges::BREAKING_CHANGE_TYPE_REMOVED,
'description' => 'Int was removed.'
],*/
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND,
2017-11-21 19:33:45 +03:00
'description' => 'TypeThatChangesType changed from an Object type to an Interface type.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED,
'description' => 'TypeThatHasBreakingFieldChanges.field1 was removed.',
2017-11-21 19:33:45 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND,
'description' => 'TypeThatHasBreakingFieldChanges.field2 changed type from String to Boolean.',
2017-11-21 19:33:45 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION,
2017-11-21 19:33:45 +03:00
'description' => 'TypeInUnion2 was removed from union type UnionTypeThatLosesAType.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM,
2017-11-21 19:33:45 +03:00
'description' => 'VALUE0 was removed from enum type EnumTypeThatLosesAValue.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND,
'description' => 'ArgThatChanges.field1 arg id has changed type from Int to String',
2017-11-21 19:33:45 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT,
2017-11-21 19:33:45 +03:00
'description' => 'TypeThatLosesInterface1 no longer implements interface Interface1.',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED,
'description' => 'skip was removed',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED,
'description' => 'arg1 was removed from DirectiveThatRemovesArg',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED,
'description' => 'A non-null arg arg1 on directive NonNullDirectiveAdded was added',
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED,
'description' => 'QUERY was removed from Directive Name',
2018-09-02 13:44:21 +03:00
],
2017-11-21 19:33:45 +03:00
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findBreakingChanges($oldSchema, $newSchema)
);
2017-11-21 19:33:45 +03:00
}
2017-11-21 19:50:11 +03:00
/**
* @see it('should detect if a directive was explicitly removed')
*/
public function testShouldDetectIfADirectiveWasExplicitlyRemoved() : void
{
$oldSchema = new Schema([
'directives' => [Directive::skipDirective(), Directive::includeDirective()],
]);
$newSchema = new Schema([
'directives' => [Directive::skipDirective()],
]);
$includeDirective = Directive::includeDirective();
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED,
'description' => sprintf('%s was removed', $includeDirective->name),
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema)
);
}
/**
* @see it('should detect if a directive was implicitly removed')
*/
public function testShouldDetectIfADirectiveWasImplicitlyRemoved() : void
{
$oldSchema = new Schema([]);
$newSchema = new Schema([
'directives' => [Directive::skipDirective(), Directive::includeDirective()],
]);
$deprecatedDirective = Directive::deprecatedDirective();
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED,
'description' => sprintf('%s was removed', $deprecatedDirective->name),
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema)
);
}
/**
* @see it('should detect if a directive argument was removed')
*/
public function testShouldDetectIfADirectiveArgumentWasRemoved() : void
{
$oldSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'DirectiveWithArg',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
'args' => FieldArgument::createMap([
'arg1' => ['name' => 'arg1'],
]),
2018-09-02 13:44:21 +03:00
]),
],
]);
$newSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'DirectiveWithArg',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
]),
],
]);
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED,
'description' => 'arg1 was removed from DirectiveWithArg',
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findRemovedDirectiveArgs($oldSchema, $newSchema)
);
}
/**
* @see it('should detect if a non-nullable directive argument was added')
*/
public function testShouldDetectIfANonNullableDirectiveArgumentWasAdded() : void
{
$oldSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'DirectiveName',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
]),
],
]);
$newSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'DirectiveName',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
'args' => FieldArgument::createMap([
'arg1' => [
'name' => 'arg1',
'type' => Type::nonNull(Type::boolean()),
],
]),
2018-09-02 13:44:21 +03:00
]),
],
]);
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED,
'description' => 'A non-null arg arg1 on directive DirectiveName was added',
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findAddedNonNullDirectiveArgs($oldSchema, $newSchema)
);
}
/**
* @see it('should detect locations removed from a directive')
*/
public function testShouldDetectLocationsRemovedFromADirective() : void
{
$d1 = new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [DirectiveLocation::FIELD_DEFINITION, DirectiveLocation::QUERY],
]);
$d2 = new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
]);
2018-09-02 13:44:21 +03:00
$this->assertEquals(
[DirectiveLocation::QUERY],
BreakingChangesFinder::findRemovedLocationsForDirective($d1, $d2)
);
}
/**
* @see it('should detect locations removed directives within a schema')
*/
public function testShouldDetectLocationsRemovedDirectiveWithinASchema() : void
{
$oldSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [
DirectiveLocation::FIELD_DEFINITION,
2018-09-02 13:44:21 +03:00
DirectiveLocation::QUERY,
],
2018-09-02 13:44:21 +03:00
]),
],
]);
$newSchema = new Schema([
'directives' => [
new Directive([
2018-09-02 13:44:21 +03:00
'name' => 'Directive Name',
'locations' => [DirectiveLocation::FIELD_DEFINITION],
2018-09-02 13:44:21 +03:00
]),
],
]);
$expectedBreakingChanges = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED,
'description' => 'QUERY was removed from Directive Name',
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedBreakingChanges,
BreakingChangesFinder::findRemovedDirectiveLocations($oldSchema, $newSchema)
);
}
// DESCRIBE: findDangerousChanges
// DESCRIBE: findArgChanges
2017-11-21 19:50:11 +03:00
/**
* @see it('should detect if an argument's defaultValue has changed')
*/
public function testShouldDetectIfAnArgumentsDefaultValueHasChanged() : void
2017-11-21 19:50:11 +03:00
{
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 19:50:11 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'name' => [
2018-09-02 13:44:21 +03:00
'type' => Type::string(),
'defaultValue' => 'test',
],
],
],
],
2017-11-21 19:50:11 +03:00
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 19:50:11 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'name' => [
2018-09-02 13:44:21 +03:00
'type' => Type::string(),
'defaultValue' => 'Test',
],
],
],
],
2017-11-21 19:50:11 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
2017-11-21 19:50:11 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
2017-11-21 19:50:11 +03:00
]);
$expected = [
2017-11-21 19:50:11 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED,
'description' => 'Type1.field1 arg name has changed defaultValue',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['dangerousChanges']
2017-11-21 19:50:11 +03:00
);
}
2017-11-21 19:53:14 +03:00
/**
* @see it('should detect if a value was added to an enum type')
*/
public function testShouldDetectIfAValueWasAddedToAnEnumType() : void
2017-11-21 19:53:14 +03:00
{
$oldEnumType = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-21 19:53:14 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
],
2017-11-21 19:53:14 +03:00
]);
$newEnumType = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-21 19:53:14 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
'VALUE2' => 2,
],
2017-11-21 19:53:14 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldEnumType],
2017-11-21 19:53:14 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newEnumType],
2017-11-21 19:53:14 +03:00
]);
$expected = [
2017-11-21 19:53:14 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM,
'description' => 'VALUE2 was added to enum type EnumType1.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findValuesAddedToEnums($oldSchema, $newSchema)
);
}
/**
* @see it('should detect interfaces added to types')
*/
public function testShouldDetectInterfacesAddedToTypes() : void
{
$interface1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
'fields' => [
'field1' => Type::string(),
2017-11-21 19:53:14 +03:00
],
]);
2018-09-02 13:44:21 +03:00
$oldType = new ObjectType([
'name' => 'Type1',
'fields' => [
'field1' => Type::string(),
],
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'interfaces' => [$interface1],
2018-09-02 13:44:21 +03:00
'fields' => [
'field1' => Type::string(),
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
]);
$expected = [
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT,
'description' => 'Interface1 added to interfaces implemented by Type1.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findInterfacesAddedToObjectTypes($oldSchema, $newSchema)
2017-11-21 19:53:14 +03:00
);
}
2017-11-21 20:18:28 +03:00
/**
* @see it('should detect if a type was added to a union type')
*/
public function testShouldDetectIfATypeWasAddedToAUnionType() : void
2017-11-21 20:30:18 +03:00
{
2017-11-21 20:18:28 +03:00
$type1 = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 20:18:28 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 20:18:28 +03:00
]);
// logially equivalent to type1; findTypesRemovedFromUnions should not
//treat this as different than type1
2017-11-21 20:18:28 +03:00
$type1a = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 20:18:28 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 20:18:28 +03:00
]);
2018-09-02 13:44:21 +03:00
$type2 = new ObjectType([
'name' => 'Type2',
2017-11-21 20:18:28 +03:00
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
2017-11-21 20:18:28 +03:00
]);
$oldUnionType = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionType1',
2017-11-21 20:18:28 +03:00
'types' => [$type1],
]);
$newUnionType = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionType1',
2017-11-21 20:18:28 +03:00
'types' => [$type1a, $type2],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldUnionType],
2017-11-21 20:18:28 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newUnionType],
2017-11-21 20:18:28 +03:00
]);
$expected = [
2017-11-21 20:18:28 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION,
'description' => 'Type2 was added to union type UnionType1.',
],
];
$this->assertEquals(
$expected,
2018-08-22 02:20:49 +03:00
BreakingChangesFinder::findTypesAddedToUnions($oldSchema, $newSchema)
2017-11-21 20:18:28 +03:00
);
}
2017-11-21 20:30:18 +03:00
/**
* @see it('should detect if a nullable field was added to an input')
*/
public function testShouldDetectIfANullableFieldWasAddedToAnInput() : void
{
$oldInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
'field1' => [
'type' => Type::string(),
],
],
]);
$newInputType = new InputObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'InputType1',
'fields' => [
'field1' => [
'type' => Type::string(),
],
'field2' => [
'type' => Type::int(),
],
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$oldInputType],
]);
$newSchema = new Schema([
'query' => $this->queryType,
2018-09-02 13:44:21 +03:00
'types' => [$newInputType],
]);
$expectedFieldChanges = [
[
'description' => 'A nullable field field2 on input type InputType1 was added.',
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_INPUT_FIELD_ADDED,
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedFieldChanges,
BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes(
$oldSchema,
$newSchema
)['dangerousChanges']
);
}
/**
* @see it('should find all dangerous changes')
*/
public function testShouldFindAllDangerousChanges() : void
2017-11-21 20:30:18 +03:00
{
$enumThatGainsAValueOld = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-21 20:30:18 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
],
2017-11-21 20:30:18 +03:00
]);
$enumThatGainsAValueNew = new EnumType([
2018-09-02 13:44:21 +03:00
'name' => 'EnumType1',
2017-11-21 20:30:18 +03:00
'values' => [
'VALUE0' => 0,
'VALUE1' => 1,
2018-09-02 13:44:21 +03:00
'VALUE2' => 2,
],
2017-11-21 20:30:18 +03:00
]);
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 20:30:18 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'name' => [
2018-09-02 13:44:21 +03:00
'type' => Type::string(),
'defaultValue' => 'test',
],
],
],
],
2017-11-21 20:30:18 +03:00
]);
2018-09-02 13:44:21 +03:00
$typeInUnion1 = new ObjectType([
'name' => 'TypeInUnion1',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
]);
2018-09-02 13:44:21 +03:00
$typeInUnion2 = new ObjectType([
'name' => 'TypeInUnion2',
'fields' => [
2018-09-02 13:44:21 +03:00
'field1' => Type::string(),
],
]);
$unionTypeThatGainsATypeOld = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionTypeThatGainsAType',
'types' => [$typeInUnion1],
]);
$unionTypeThatGainsATypeNew = new UnionType([
2018-09-02 13:44:21 +03:00
'name' => 'UnionTypeThatGainsAType',
'types' => [$typeInUnion1, $typeInUnion2],
]);
2017-11-21 20:30:18 +03:00
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
2017-11-21 20:30:18 +03:00
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'name' => [
2018-09-02 13:44:21 +03:00
'type' => Type::string(),
'defaultValue' => 'Test',
],
],
],
],
2017-11-21 20:30:18 +03:00
]);
$interface1 = new InterfaceType([
2018-09-02 13:44:21 +03:00
'name' => 'Interface1',
2017-11-21 20:30:18 +03:00
'fields' => [
'field1' => Type::string(),
],
2017-11-21 20:30:18 +03:00
]);
$typeThatGainsInterfaceOld = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatGainsInterface1',
2017-11-21 20:30:18 +03:00
'fields' => [
'field1' => Type::string(),
],
2017-11-21 20:30:18 +03:00
]);
$typeThatGainsInterfaceNew = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'TypeThatGainsInterface1',
'interfaces' => [$interface1],
2018-09-02 13:44:21 +03:00
'fields' => [
'field1' => Type::string(),
],
2017-11-21 20:30:18 +03:00
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [
$oldType,
$enumThatGainsAValueOld,
$typeThatGainsInterfaceOld,
2018-09-02 13:44:21 +03:00
$unionTypeThatGainsATypeOld,
],
2017-11-21 20:30:18 +03:00
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [
$newType,
$enumThatGainsAValueNew,
$typeThatGainsInterfaceNew,
2018-09-02 13:44:21 +03:00
$unionTypeThatGainsATypeNew,
],
2017-11-21 20:30:18 +03:00
]);
$expectedDangerousChanges = [
[
'description' => 'Type1.field1 arg name has changed defaultValue',
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED,
2017-11-21 20:30:18 +03:00
],
[
'description' => 'VALUE2 was added to enum type EnumType1.',
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM,
2017-11-21 20:30:18 +03:00
],
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT,
'description' => 'Interface1 added to interfaces implemented by TypeThatGainsInterface1.',
],
2017-11-21 20:30:18 +03:00
[
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION,
'description' => 'TypeInUnion2 was added to union type UnionTypeThatGainsAType.',
2018-09-02 13:44:21 +03:00
],
2017-11-21 20:30:18 +03:00
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedDangerousChanges,
BreakingChangesFinder::findDangerousChanges($oldSchema, $newSchema)
);
2017-11-21 20:30:18 +03:00
}
/**
* @see it('should detect if a nullable field argument was added')
*/
public function testShouldDetectIfANullableFieldArgumentWasAdded() : void
{
$oldType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'arg1' => [
'type' => Type::string(),
],
],
],
],
]);
$newType = new ObjectType([
2018-09-02 13:44:21 +03:00
'name' => 'Type1',
'fields' => [
'field1' => [
'type' => Type::string(),
'args' => [
'arg1' => [
'type' => Type::string(),
],
'arg2' => [
'type' => Type::string(),
],
],
],
],
]);
$oldSchema = new Schema([
'query' => $this->queryType,
'types' => [$oldType],
]);
$newSchema = new Schema([
'query' => $this->queryType,
'types' => [$newType],
]);
$expectedFieldChanges = [
[
'description' => 'A nullable arg arg2 on Type1.field1 was added',
2018-09-02 13:44:21 +03:00
'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_ARG_ADDED,
],
];
2018-09-02 13:44:21 +03:00
$this->assertEquals(
$expectedFieldChanges,
BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['dangerousChanges']
);
}
}