1 | <?php |
||
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([ |
||
151 |