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

ObjectTypeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 111
rs 10
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
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 5/11/16 9:43 PM
18
 */
19
20
namespace Youshido\Tests\Library\Type;
21
22
use Youshido\GraphQL\Field\Field;
23
use Youshido\GraphQL\Type\Object\ObjectType;
24
use Youshido\GraphQL\Type\Scalar\IntType;
25
use Youshido\GraphQL\Type\Scalar\StringType;
26
use Youshido\GraphQL\Type\TypeMap;
27
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
28
use Youshido\Tests\DataProvider\TestMutationObjectType;
29
use Youshido\Tests\DataProvider\TestObjectType;
30
31
class ObjectTypeTest extends \PHPUnit_Framework_TestCase
32
{
33
    public function testCreatingInvalidObject(): void
34
    {
35
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
36
37
        new ObjectType([]);
38
    }
39
40
41
    public function testInvalidNameParam(): void
42
    {
43
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
44
45
        $type = new ObjectType([
46
            'name' => null,
47
        ]);
48
        ConfigValidator::getInstance()->assertValidConfig($type->getConfig());
49
    }
50
51
52
    public function testInvalidFieldsParam(): void
53
    {
54
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
55
56
        $type = new ObjectType([
57
            'name'   => 'SomeName',
58
            'fields' => [],
59
        ]);
60
        ConfigValidator::getInstance()->assertValidConfig($type->getConfig());
61
    }
62
63
64
    public function testSerialize(): void
65
    {
66
        $this->expectException(\InvalidArgumentException::class);
67
68
        $object = new ObjectType([
69
            'name'   => 'SomeName',
70
            'fields' => [
71
                'name' => new StringType(),
72
            ],
73
        ]);
74
        $object->serialize([]);
75
    }
76
77
    public function testNormalCreatingParam(): void
78
    {
79
        $objectType = new ObjectType([
80
            'name'   => 'Post',
81
            'fields' => [
82
                'id' => new IntType(),
83
            ],
84
            'description' => 'Post type description',
85
        ]);
86
        $this->assertEquals($objectType->getKind(), TypeMap::KIND_OBJECT);
87
        $this->assertEquals($objectType->getName(), 'Post');
88
        $this->assertEquals($objectType->getType(), $objectType);
89
        $this->assertEquals($objectType->getType()->getName(), 'Post');
90
        $this->assertEquals($objectType->getNamedType(), $objectType);
91
92
        $this->assertEmpty($objectType->getInterfaces());
93
        $this->assertTrue($objectType->isValidValue($objectType));
94
        $this->assertTrue($objectType->isValidValue(null));
95
96
        $this->assertEquals('Post type description', $objectType->getDescription());
97
    }
98
99
    public function testFieldsTrait(): void
100
    {
101
        $idField   = new Field(['name' => 'id', 'type' => new IntType()]);
102
        $nameField = new Field(['name' => 'name', 'type' => new StringType()]);
103
104
        $objectType = new ObjectType([
105
            'name'   => 'Post',
106
            'fields' => [
107
                $idField,
108
            ],
109
            'description' => 'Post type description',
110
        ]);
111
        $this->assertTrue($objectType->hasFields());
112
        $this->assertEquals([
113
            'id' => $idField,
114
        ], $objectType->getFields());
115
116
        $objectType->addField($nameField);
117
        $this->assertEquals([
118
            'id'   => $idField,
119
            'name' => $nameField,
120
        ], $objectType->getFields());
121
    }
122
123
    public function testExtendedClass(): void
124
    {
125
        $objectType = new TestObjectType();
126
        $this->assertEquals($objectType->getName(), 'TestObject');
127
        $this->assertEquals($objectType->getType(), $objectType, 'test type of extended object');
128
129
        $this->assertNull($objectType->getDescription());
130
    }
131
132
    public function testMutationObjectClass(): void
133
    {
134
        $mutation = new TestMutationObjectType();
135
        $this->assertEquals(new StringType(), $mutation->getType());
136
    }
137
}
138