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

InterfaceTypeConfigTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 0
loc 62
rs 10
c 0
b 0
f 0
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/12/16 4:17 PM
18
 */
19
20
namespace Youshido\Tests\Library\Config;
21
22
use Youshido\GraphQL\Config\Object\InterfaceTypeConfig;
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\Validator\ConfigValidator\ConfigValidator;
27
use Youshido\Tests\DataProvider\TestInterfaceType;
28
29
class InterfaceTypeConfigTest extends \PHPUnit_Framework_TestCase
30
{
31
    public function testCreation(): void
32
    {
33
        $config = new InterfaceTypeConfig(['name' => 'Test'], null, false);
34
        $this->assertEquals($config->getName(), 'Test', 'Normal creation');
35
    }
36
37
38
    public function testConfigNoFields(): void
39
    {
40
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
41
42
        ConfigValidator::getInstance()->assertValidConfig(
43
            new InterfaceTypeConfig(['name' => 'Test', 'resolveType' => static function (): void {
44
            }], null, true)
45
        );
46
    }
47
48
49
    public function testConfigNoResolve(): void
50
    {
51
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
52
53
        ConfigValidator::getInstance()->assertValidConfig(
54
            new InterfaceTypeConfig(['name' => 'Test', 'fields' => ['id' => new IntType()]], null, true)
55
        );
56
    }
57
58
59
    public function testConfigInvalidResolve(): void
60
    {
61
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
62
63
        $config = new InterfaceTypeConfig(['name' => 'Test', 'fields' => ['id' => new IntType()]], null, false);
64
        $config->resolveType(['invalid object']);
65
    }
66
67
    public function testInterfaces(): void
68
    {
69
        $interfaceConfig = new InterfaceTypeConfig([
70
            'name'        => 'Test',
71
            'fields'      => ['id' => new IntType()],
72
            'resolveType' => static function ($object) {
73
                return $object->getType();
74
            },
75
        ], null, true);
76
        $object = new ObjectType(['name' => 'User', 'fields' => ['name' => new StringType()]]);
77
78
        $this->assertEquals($interfaceConfig->getName(), 'Test');
79
        $this->assertEquals($interfaceConfig->resolveType($object), $object->getType());
80
81
        $testInterface                = new TestInterfaceType();
82
        $interfaceConfigWithNoResolve = new InterfaceTypeConfig([
83
            'name'   => 'Test',
84
            'fields' => ['id' => new IntType()],
85
        ], $testInterface, false);
86
        $this->assertEquals($interfaceConfigWithNoResolve->resolveType($object), $object);
87
    }
88
}
89