1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of GraphQL project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 5/11/16 10:41 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\Tests\Library\Config; |
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Type\Enum\EnumType; |
12
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
13
|
|
|
use Youshido\GraphQL\Type\Scalar\IdType; |
14
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
15
|
|
|
use Youshido\GraphQL\Type\TypeService; |
16
|
|
|
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator; |
17
|
|
|
use Youshido\Tests\DataProvider\TestConfig; |
18
|
|
|
use Youshido\Tests\DataProvider\TestConfigExtraFields; |
19
|
|
|
use Youshido\Tests\DataProvider\TestConfigInvalidRule; |
20
|
|
|
|
21
|
|
|
class ConfigTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
26
|
|
|
*/ |
27
|
|
|
public function testEmptyParams() |
28
|
|
|
{ |
29
|
|
|
new TestConfig([]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
34
|
|
|
*/ |
35
|
|
|
public function testInvalidParams() |
36
|
|
|
{ |
37
|
|
|
ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['id' => 1])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @expectedException \Exception |
42
|
|
|
*/ |
43
|
|
|
public function testInvalidMethod() |
44
|
|
|
{ |
45
|
|
|
$config = new TestConfig(['name' => 'test']); |
46
|
|
|
$config->doSomethingStrange(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testMethods() |
50
|
|
|
{ |
51
|
|
|
$name = 'Test'; |
52
|
|
|
$rules = [ |
53
|
|
|
'name' => ['type' => TypeService::TYPE_ANY, 'required' => true], |
54
|
|
|
'resolve' => ['type' => TypeService::TYPE_CALLABLE, 'final' => true], |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$config = new TestConfig(['name' => $name]); |
58
|
|
|
$this->assertEquals($config->getName(), $name); |
59
|
|
|
$this->assertEquals($config->get('name'), $name); |
60
|
|
|
$this->assertEquals($config->get('non existing key'), null); |
61
|
|
|
$this->assertEquals($config->set('name', 'StrangeName'), $config); |
62
|
|
|
$this->assertEquals($config->get('name'), 'StrangeName'); |
63
|
|
|
$this->assertEquals($config->get('non existing', 'default'), 'default'); |
64
|
|
|
$this->assertEquals($config->isName(), 'StrangeName'); |
|
|
|
|
65
|
|
|
$this->assertEquals($config->setName('StrangeName 2'), $config); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$config->set('var', 'value'); |
68
|
|
|
$this->assertEquals($config->getVar(), 'value'); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->assertEquals($config->getRules(), $rules); |
71
|
|
|
$this->assertEquals($config->getContextRules(), $rules); |
72
|
|
|
$this->assertNull($config->getResolveFunction()); |
73
|
|
|
|
74
|
|
|
$object = new ObjectType([ |
75
|
|
|
'name' => 'TestObject', |
76
|
|
|
'fields' => [ |
77
|
|
|
'id' => [ |
78
|
|
|
'type' => new IntType() |
79
|
|
|
] |
80
|
|
|
] |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$finalConfig = new TestConfig(['name' => $name . 'final', 'resolve' => function () { return []; }], $object, true); |
84
|
|
|
$this->assertEquals($finalConfig->getType(), null); |
85
|
|
|
|
86
|
|
|
$rules['resolve']['required'] = true; |
87
|
|
|
$this->assertEquals($finalConfig->getContextRules(), $rules); |
88
|
|
|
|
89
|
|
|
$this->assertNotNull($finalConfig->getResolveFunction()); |
90
|
|
|
|
91
|
|
|
$configExtraFields = new TestConfigExtraFields([ |
92
|
|
|
'name' => 'Test', |
93
|
|
|
'extraField' => 'extraValue' |
94
|
|
|
]); |
95
|
|
|
$this->assertEquals('extraValue', $configExtraFields->get('extraField')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
100
|
|
|
*/ |
101
|
|
|
public function testFinalRule() |
102
|
|
|
{ |
103
|
|
|
ConfigValidator::getInstance()->assertValidConfig(new TestConfig(['name' => 'Test' . 'final'], null, true)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
108
|
|
|
*/ |
109
|
|
|
public function testInvalidRule() |
110
|
|
|
{ |
111
|
|
|
ConfigValidator::getInstance()->assertValidConfig( |
112
|
|
|
new TestConfigInvalidRule(['name' => 'Test', 'invalidRuleField' => 'test'], null, null) |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
118
|
|
|
*/ |
119
|
|
|
public function testEnumConfig() |
120
|
|
|
{ |
121
|
|
|
$enumType = new EnumType([ |
122
|
|
|
'name' => 'Status', |
123
|
|
|
'values' => [ |
124
|
|
|
[ |
125
|
|
|
'name' => 'ACTIVE', |
126
|
|
|
'values' => 1 |
127
|
|
|
] |
128
|
|
|
] |
129
|
|
|
]); |
130
|
|
|
$object = new ObjectType([ |
131
|
|
|
'name' => 'Project', |
132
|
|
|
'fields' => [ |
133
|
|
|
'id' => new IdType(), |
134
|
|
|
'status' => $enumType |
135
|
|
|
] |
136
|
|
|
]); |
137
|
|
|
ConfigValidator::getInstance()->assertValidConfig($object->getConfig()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: