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

ConfigTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 120
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 10:41 PM
18
 */
19
20
namespace Youshido\Tests\Library\Config;
21
22
use Youshido\GraphQL\Type\Enum\EnumType;
23
use Youshido\GraphQL\Type\Object\ObjectType;
24
use Youshido\GraphQL\Type\Scalar\IdType;
25
use Youshido\GraphQL\Type\Scalar\IntType;
26
use Youshido\GraphQL\Type\TypeService;
27
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
28
use Youshido\Tests\DataProvider\TestConfig;
29
use Youshido\Tests\DataProvider\TestConfigExtraFields;
30
use Youshido\Tests\DataProvider\TestConfigInvalidRule;
31
32
class ConfigTest extends \PHPUnit_Framework_TestCase
33
{
34
    public function testEmptyParams(): void
35
    {
36
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
37
38
        new TestConfig([]);
39
    }
40
41
42
    public function testInvalidParams(): void
43
    {
44
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
45
46
        ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['id' => 1]));
47
    }
48
49
50
    public function testInvalidMethod(): void
51
    {
52
        $this->expectException(\Exception::class);
53
54
        $config = new TestConfig(['name' => 'test']);
55
        $config->doSomethingStrange();
56
    }
57
58
    public function testMethods(): void
59
    {
60
        $name  = 'Test';
61
        $rules = [
62
            'name'    => ['type' => TypeService::TYPE_ANY, 'required' => true],
63
            'resolve' => ['type' => TypeService::TYPE_CALLABLE, 'final' => true],
64
        ];
65
66
        $config = new TestConfig(['name' => $name]);
67
        $this->assertEquals($config->getName(), $name);
68
        $this->assertEquals($config->get('name'), $name);
69
        $this->assertEquals($config->get('non existing key'), null);
70
        $this->assertEquals($config->set('name', 'StrangeName'), $config);
71
        $this->assertEquals($config->get('name'), 'StrangeName');
72
        $this->assertEquals($config->get('non existing', 'default'), 'default');
73
        $this->assertEquals($config->isName(), 'StrangeName');
74
        $this->assertEquals($config->setName('StrangeName 2'), $config);
75
76
        $config->set('var', 'value');
77
        $this->assertEquals($config->getVar(), 'value');
78
79
        $this->assertEquals($config->getRules(), $rules);
80
        $this->assertEquals($config->getContextRules(), $rules);
81
        $this->assertNull($config->getResolveFunction());
82
83
        $object = new ObjectType([
84
            'name'   => 'TestObject',
85
            'fields' => [
86
                'id' => [
87
                    'type' => new IntType(),
88
                ],
89
            ],
90
        ]);
91
92
        $finalConfig = new TestConfig(['name' => $name . 'final', 'resolve' => static function () {
93
            return [];
94
        }], $object, true);
95
        $this->assertEquals($finalConfig->getType(), null);
96
97
        $rules['resolve']['required'] = true;
98
        $this->assertEquals($finalConfig->getContextRules(), $rules);
99
100
        $this->assertNotNull($finalConfig->getResolveFunction());
101
102
        $configExtraFields = new TestConfigExtraFields([
103
            'name'       => 'Test',
104
            'extraField' => 'extraValue',
105
        ]);
106
        $this->assertEquals('extraValue', $configExtraFields->get('extraField'));
107
    }
108
109
110
    public function testFinalRule(): void
111
    {
112
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
113
114
        ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['name' => 'Test' . 'final'], null, true));
115
    }
116
117
118
    public function testInvalidRule(): void
119
    {
120
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
121
122
        ConfigValidator::getInstance()->assertValidConfig(
123
            new TestConfigInvalidRule(['name' => 'Test', 'invalidRuleField' => 'test'], null, null)
124
        );
125
    }
126
127
128
    public function testEnumConfig(): void
129
    {
130
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
131
132
        $enumType = new EnumType([
133
            'name'   => 'Status',
134
            'values' => [
135
                [
136
                    'name'   => 'ACTIVE',
137
                    'values' => 1,
138
                ],
139
            ],
140
        ]);
141
        $object = new ObjectType([
142
            'name'   => 'Project',
143
            'fields' => [
144
                'id'     => new IdType(),
145
                'status' => $enumType,
146
            ],
147
        ]);
148
        ConfigValidator::getInstance()->assertValidConfig($object->getConfig());
149
    }
150
}
151