|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 12.05.16 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\Tests\Library\Type; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Type\Enum\EnumType; |
|
12
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
13
|
|
|
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator; |
|
14
|
|
|
use Youshido\Tests\DataProvider\TestEnumType; |
|
15
|
|
|
|
|
16
|
|
|
class EnumTypeTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testInvalidInlineCreation() |
|
23
|
|
|
{ |
|
24
|
|
|
new EnumType([]); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testInvalidEmptyParams() |
|
31
|
|
|
{ |
|
32
|
|
|
$enumField = new EnumType([ |
|
33
|
|
|
'values' => [] |
|
34
|
|
|
]); |
|
35
|
|
|
ConfigValidator::getInstance()->assertValidConfig($enumField->getConfig()); |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
public function testInvalidValueParams() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$enumField = new EnumType([ |
|
45
|
|
|
'values' => [ |
|
46
|
|
|
'test' => 'asd', |
|
47
|
|
|
'value' => 'asdasd' |
|
48
|
|
|
] |
|
49
|
|
|
]); |
|
50
|
|
|
ConfigValidator::getInstance()->assertValidConfig($enumField->getConfig()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
55
|
|
|
*/ |
|
56
|
|
View Code Duplication |
public function testExistingNameParams() |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$enumField = new EnumType([ |
|
59
|
|
|
'values' => [ |
|
60
|
|
|
[ |
|
61
|
|
|
'test' => 'asd', |
|
62
|
|
|
'value' => 'asdasd' |
|
63
|
|
|
] |
|
64
|
|
|
] |
|
65
|
|
|
]); |
|
66
|
|
|
ConfigValidator::getInstance()->assertValidConfig($enumField->getConfig()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function testInvalidNameParams() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$enumField = new EnumType([ |
|
75
|
|
|
'values' => [ |
|
76
|
|
|
[ |
|
77
|
|
|
'name' => false, |
|
78
|
|
|
'value' => 'asdasd' |
|
79
|
|
|
] |
|
80
|
|
|
] |
|
81
|
|
|
]); |
|
82
|
|
|
ConfigValidator::getInstance()->assertValidConfig($enumField->getConfig()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @expectedException Youshido\GraphQL\Exception\ConfigurationException |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function testWithoutValueParams() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$enumField = new EnumType([ |
|
91
|
|
|
'values' => [ |
|
92
|
|
|
[ |
|
93
|
|
|
'name' => 'TEST_ENUM', |
|
94
|
|
|
] |
|
95
|
|
|
] |
|
96
|
|
|
]); |
|
97
|
|
|
ConfigValidator::getInstance()->assertValidConfig($enumField->getConfig()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testNormalCreatingParams() |
|
101
|
|
|
{ |
|
102
|
|
|
$valuesData = [ |
|
103
|
|
|
[ |
|
104
|
|
|
'name' => 'ENABLE', |
|
105
|
|
|
'value' => true |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'name' => 'DISABLE', |
|
109
|
|
|
'value' => 'disable' |
|
110
|
|
|
] |
|
111
|
|
|
]; |
|
112
|
|
|
$enumType = new EnumType([ |
|
113
|
|
|
'name' => 'BoolEnum', |
|
114
|
|
|
'values' => $valuesData |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals($enumType->getKind(), TypeMap::KIND_ENUM); |
|
118
|
|
|
$this->assertEquals($enumType->getName(), 'BoolEnum'); |
|
119
|
|
|
$this->assertEquals($enumType->getType(), $enumType); |
|
120
|
|
|
$this->assertEquals($enumType->getNamedType(), $enumType); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertFalse($enumType->isValidValue($enumType)); |
|
123
|
|
|
$this->assertTrue($enumType->isValidValue(null)); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertTrue($enumType->isValidValue(true)); |
|
126
|
|
|
$this->assertTrue($enumType->isValidValue('disable')); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertNull($enumType->serialize('invalid value')); |
|
129
|
|
|
$this->assertNull($enumType->parseValue('invalid literal')); |
|
130
|
|
|
$this->assertTrue($enumType->parseValue('ENABLE')); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEquals($valuesData, $enumType->getValues()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testExtendedObject() |
|
136
|
|
|
{ |
|
137
|
|
|
$testEnumType = new TestEnumType(); |
|
138
|
|
|
$this->assertEquals('TestEnum', $testEnumType->getName()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.