Completed
Pull Request — master (#204)
by Ryan
11:34
created

UnionTypeTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 36.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 28
loc 77
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/**
14
 * Date: 13.05.16.
15
 */
16
17
namespace Youshido\tests\Library\Type;
18
19
use Youshido\GraphQL\Type\Object\ObjectType;
20
use Youshido\GraphQL\Type\Scalar\IntType;
21
use Youshido\GraphQL\Type\TypeMap;
22
use Youshido\GraphQL\Type\Union\UnionType;
23
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
24
use Youshido\Tests\DataProvider\TestObjectType;
25
use Youshido\Tests\DataProvider\TestUnionType;
26
27
class UnionTypeTest extends \PHPUnit_Framework_TestCase
28
{
29
    public function testInlineCreation(): void
30
    {
31
        $object = new ObjectType([
32
            'name'   => 'TestObject',
33
            'fields' => ['id' => ['type' => new IntType()]],
34
        ]);
35
36
        $type = new UnionType([
37
            'name'        => 'Car',
38
            'description' => 'Union collect cars types',
39
            'types'       => [
40
                new TestObjectType(),
41
                $object,
42
            ],
43
            'resolveType' => static function ($type) {
44
                return $type;
45
            },
46
        ]);
47
48
        $this->assertEquals('Car', $type->getName());
49
        $this->assertEquals('Union collect cars types', $type->getDescription());
50
        $this->assertEquals([new TestObjectType(), $object], $type->getTypes());
51
        $this->assertEquals('test', $type->resolveType('test'));
52
        $this->assertEquals(TypeMap::KIND_UNION, $type->getKind());
53
        $this->assertEquals($type, $type->getNamedType());
54
        $this->assertTrue($type->isValidValue(true));
55
    }
56
57
    public function testObjectCreation(): void
58
    {
59
        $type = new TestUnionType();
60
61
        $this->assertEquals('TestUnion', $type->getName());
62
        $this->assertEquals('Union collect cars types', $type->getDescription());
63
        $this->assertEquals([new TestObjectType()], $type->getTypes());
64
        $this->assertEquals('test', $type->resolveType('test'));
65
    }
66
67
68
    public function testInvalidTypesWithScalar(): void
69
    {
70
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
71
72
        $type = new UnionType([
73
            'name'        => 'Car',
74
            'description' => 'Union collect cars types',
75
            'types'       => [
76
                'test', new IntType(),
77
            ],
78
            'resolveType' => static function ($type) {
79
                return $type;
80
            },
81
        ]);
82
        ConfigValidator::getInstance()->assertValidConfig($type->getConfig());
83
    }
84
85
86
    public function testInvalidTypes(): void
87
    {
88
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
89
90
        $type = new UnionType([
91
            'name'        => 'Car',
92
            'description' => 'Union collect cars types',
93
            'types'       => [
94
                new IntType(),
95
            ],
96
            'resolveType' => static function ($type) {
97
                return $type;
98
            },
99
        ]);
100
        ConfigValidator::getInstance()->assertValidConfig($type->getConfig());
101
    }
102
}
103