This repository was archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathMutationTest.php
147 lines (129 loc) · 4.79 KB
/
MutationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use Illuminate\Validation\Validator;
class MutationTest extends FieldTest
{
protected function getFieldClass()
{
return UpdateExampleMutationWithInputType::class;
}
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set('graphql.types', [
'Example' => ExampleType::class,
'ExampleValidationInputObject' => ExampleValidationInputObject::class,
'ExampleNestedValidationInputObject' => ExampleNestedValidationInputObject::class,
]);
}
/**
* Test get rules.
*
* @test
*/
public function testGetRules()
{
$class = $this->getFieldClass();
$field = new $class();
$rules = $field->getRules();
$this->assertInternalType('array', $rules);
$this->assertArrayHasKey('test', $rules);
$this->assertArrayHasKey('test_with_rules', $rules);
$this->assertArrayHasKey('test_with_rules_closure', $rules);
$this->assertEquals($rules['test'], ['required']);
$this->assertEquals($rules['test_with_rules'], ['required']);
$this->assertEquals($rules['test_with_rules_closure'], ['required']);
$this->assertEquals($rules['test_with_rules_input_object'], ['required']);
$this->assertEquals(array_get($rules, 'test_with_rules_input_object.val'), ['required']);
$this->assertEquals(array_get($rules, 'test_with_rules_input_object.nest'), ['required']);
$this->assertEquals(array_get($rules, 'test_with_rules_input_object.nest.email'), ['email']);
$this->assertEquals(array_get($rules, 'test_with_rules_input_object.list'), ['required']);
$this->assertEquals(array_get($rules, 'test_with_rules_input_object.list.*.email'), ['email']);
}
/**
* Test resolve.
*
* @test
*/
public function testResolve()
{
$class = $this->getFieldClass();
$field = $this->getMockBuilder($class)
->setMethods(['resolve'])
->getMock();
$field->expects($this->once())
->method('resolve');
$attributes = $field->getAttributes();
$attributes['resolve'](null, [
'test' => 'test',
'test_with_rules' => 'test',
'test_with_rules_closure' => 'test',
'test_with_rules_input_object' => [
'val' => 'test',
'nest' => ['email' => 'test@test.com'],
'list' => [
['email' => 'test@test.com'],
],
],
], [], null);
}
/**
* Test resolve throw validation error.
*
* @test
* @expectedException \Folklore\GraphQL\Error\ValidationError
*/
public function testResolveThrowValidationError()
{
$class = $this->getFieldClass();
$field = new $class();
$attributes = $field->getAttributes();
$attributes['resolve'](null, [], [], null);
}
/**
* Test validation error.
*
* @test
*/
public function testValidationError()
{
$class = $this->getFieldClass();
$field = new $class();
$attributes = $field->getAttributes();
try {
$attributes['resolve'](null, [], [], null);
} catch (\Folklore\GraphQL\Error\ValidationError $e) {
$validator = $e->getValidator();
$this->assertInstanceOf(Validator::class, $validator);
$messages = $e->getValidatorMessages();
$this->assertTrue($messages->has('test'));
$this->assertTrue($messages->has('test_with_rules'));
$this->assertTrue($messages->has('test_with_rules_closure'));
$this->assertTrue($messages->has('test_with_rules_input_object.val'));
$this->assertTrue($messages->has('test_with_rules_input_object.nest'));
$this->assertTrue($messages->has('test_with_rules_input_object.list'));
}
}
/**
* Test custom validation error messages.
*
* @test
*/
public function testCustomValidationErrorMessages()
{
$class = $this->getFieldClass();
$field = new $class();
$rules = $field->getRules();
$attributes = $field->getAttributes();
try {
$attributes['resolve'](null, [
'test_with_rules_input_object' => [
'nest' => ['email' => 'invalidTestEmail.com'],
],
], [], null);
} catch (\Folklore\GraphQL\Error\ValidationError $e) {
$messages = $e->getValidatorMessages();
$this->assertEquals($messages->first('test'), 'A test is required.');
$this->assertEquals($messages->first('test_with_rules_input_object.nest.email'), 'Invalid your email : invalidTestEmail.com');
}
}
}