1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EasyDictionary; |
6
|
|
|
|
7
|
|
|
use EasyDictionary\DataProvider\Simple; |
8
|
|
|
use EasyDictionary\Dictionary\Simple as SimpleDictionary; |
9
|
|
|
use EasyDictionary\Interfaces\ConfigInterface; |
10
|
|
|
use EasyDictionary\Interfaces\DataProviderInterface; |
11
|
|
|
use EasyDictionary\Interfaces\DictionaryInterface; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
14
|
|
|
|
15
|
|
|
/** |
|
|
|
|
16
|
|
|
* @coversDefaultClass \EasyDictionary\Manager |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
class ManagerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @covers \EasyDictionary\Manager |
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
public function testClassExistence() |
24
|
|
|
{ |
25
|
|
|
self::assertTrue(class_exists('\EasyDictionary\Manager')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @covers ::__construct |
30
|
|
|
* @covers ::setConfig |
31
|
|
|
* @covers ::getConfig |
32
|
|
|
*/ |
|
|
|
|
33
|
|
|
public function testSetConfigFromConstructor() |
34
|
|
|
{ |
35
|
|
|
$config = $this->createMock(ConfigInterface::class); |
36
|
|
|
$manager = new Manager($config); |
|
|
|
|
37
|
|
|
|
38
|
|
|
self::assertEquals($config, $manager->getConfig()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @covers ::__construct |
43
|
|
|
* @covers ::getConfig |
44
|
|
|
* @covers ::get |
45
|
|
|
* |
46
|
|
|
* @expectedException \EasyDictionary\Exception\RuntimeException |
|
|
|
|
47
|
|
|
* @expectedExceptionMessage Config not found |
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
public function testGetDictionaryWithoutConfig() |
50
|
|
|
{ |
51
|
|
|
$manager = new Manager(); |
52
|
|
|
$manager->get('test'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @covers ::__construct |
57
|
|
|
* @covers ::setConfig |
58
|
|
|
* @covers ::getConfig |
59
|
|
|
* @covers ::get |
60
|
|
|
* |
61
|
|
|
* @expectedException \EasyDictionary\Exception\RuntimeException |
|
|
|
|
62
|
|
|
* @expectedExceptionMessage Dictionary with key "test" not found |
|
|
|
|
63
|
|
|
*/ |
|
|
|
|
64
|
|
|
public function testGetANonExistentDictionary() |
65
|
|
|
{ |
66
|
|
|
$config = $this->createMock(ConfigInterface::class); |
67
|
|
|
$manager = new Manager($config); |
|
|
|
|
68
|
|
|
$manager->get('test'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @covers ::__construct |
73
|
|
|
* @covers ::add |
74
|
|
|
* @covers ::get |
75
|
|
|
*/ |
|
|
|
|
76
|
|
View Code Duplication |
public function testAddNewDictionary() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$dictionaryName = 'test'; |
79
|
|
|
$dictionary = $this->createMock(DictionaryInterface::class); |
80
|
|
|
$dictionary->method('getName')->willReturn($dictionaryName); |
81
|
|
|
|
82
|
|
|
$manager = new Manager(); |
83
|
|
|
$manager->add($dictionary); |
|
|
|
|
84
|
|
|
|
85
|
|
|
self::assertEquals($dictionary, $manager->get($dictionaryName)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* @covers ::__construct |
90
|
|
|
* @covers ::add |
91
|
|
|
* @covers ::get |
92
|
|
|
* |
93
|
|
|
* @expectedException \EasyDictionary\Exception\RuntimeException |
|
|
|
|
94
|
|
|
* @expectedExceptionMessage The dictionary with key "test" already exists |
95
|
|
|
*/ |
|
|
|
|
96
|
|
View Code Duplication |
public function testAddDuplicatedDictionary() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$dictionaryName = 'test'; |
99
|
|
|
$dictionary = $this->createMock(DictionaryInterface::class); |
100
|
|
|
$dictionary->method('getName')->willReturn($dictionaryName); |
101
|
|
|
|
102
|
|
|
$manager = new Manager(); |
103
|
|
|
$manager->add($dictionary); |
|
|
|
|
104
|
|
|
$manager->add($dictionary); |
|
|
|
|
105
|
|
|
|
106
|
|
|
self::assertEquals($dictionary, $manager->get($dictionaryName)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @covers ::get |
111
|
|
|
* @covers ::add |
112
|
|
|
* @covers ::create |
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
public function testCreateSimpleDictionary() |
115
|
|
|
{ |
116
|
|
|
$cacheMock = $this->createMock(CacheInterface::class); |
117
|
|
|
$dataProviderMock = $this->createMock(DataProviderInterface::class); |
118
|
|
|
|
119
|
|
|
$dictionaryMock = $this->createMock(DictionaryInterface::class); |
120
|
|
|
$dictionaryMock->method('getName')->willReturn('test'); |
121
|
|
|
$dictionaryMock->expects(self::once())->method('getName'); |
122
|
|
|
$dictionaryMock->expects(self::once())->method('setDataProvider')->with($dataProviderMock); |
123
|
|
|
$dictionaryMock->expects(self::once())->method('setName'); |
124
|
|
|
$dictionaryMock->expects(self::once())->method('setDefaultView')->with(null); |
125
|
|
|
$dictionaryMock->expects(self::once())->method('setSearchFields')->with([]); |
126
|
|
|
$dictionaryMock->expects(self::once())->method('setCache')->with($cacheMock, 1); |
127
|
|
|
|
128
|
|
|
$config = $this->createMock(ConfigInterface::class); |
129
|
|
|
$config->method('getCache')->with('testCache')->willReturn($cacheMock); |
130
|
|
|
$config->expects(self::once())->method('getDefaultDataProviderClass'); |
131
|
|
|
$config->expects(self::once())->method('getDefaultDictionaryClass'); |
132
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
133
|
|
|
'test' => [ |
134
|
|
|
'cache' => 'testCache', |
135
|
|
|
'cacheTTL' => 1, |
136
|
|
|
'data' => [] |
137
|
|
|
] |
138
|
|
|
]); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$manager = $this->createPartialMock(Manager::class, [ |
|
|
|
|
141
|
|
|
'createDictionary', |
142
|
|
|
'createDataProvider', |
143
|
|
|
'getConfig', |
144
|
|
|
]); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$manager->method('createDictionary')->willReturn($dictionaryMock); |
147
|
|
|
$manager->method('createDataProvider')->willReturn($dataProviderMock); |
148
|
|
|
$manager->method('getConfig')->willReturn($config); |
149
|
|
|
|
150
|
|
|
self::assertEquals($manager->get('test'), $manager->get('test')); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
|
|
|
|
154
|
|
|
* @covers \EasyDictionary\AbstractDictionary |
155
|
|
|
* @covers \EasyDictionary\DataProvider\Simple |
156
|
|
|
* @covers ::<public> |
157
|
|
|
* @covers ::create |
158
|
|
|
* @covers ::createDataProvider |
159
|
|
|
*/ |
|
|
|
|
160
|
|
|
public function testCreateRealDictionary() |
161
|
|
|
{ |
162
|
|
|
$config = $this->createMock(ConfigInterface::class); |
163
|
|
|
$config->expects(self::once())->method('getDefaultDataProviderClass') |
164
|
|
|
->willReturn(Simple::class); |
165
|
|
|
$config->expects(self::once())->method('getDefaultDictionaryClass') |
166
|
|
|
->willReturn(SimpleDictionary::class); |
167
|
|
|
|
168
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
169
|
|
|
'test' => [ |
170
|
|
|
'data' => [] |
171
|
|
|
] |
172
|
|
|
]); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$manager = new Manager(); |
175
|
|
|
$manager->setConfig($config); |
|
|
|
|
176
|
|
|
|
177
|
|
|
self::assertEquals($manager->get('test'), $manager->get('test')); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* @covers ::<public> |
|
|
|
|
182
|
|
|
* @covers ::create |
|
|
|
|
183
|
|
|
* @covers ::createDataProvider |
|
|
|
|
184
|
|
|
* @expectedException \EasyDictionary\Exception\InvalidConfigurationException |
|
|
|
|
185
|
|
|
* @expectedExceptionMessage Class "/bad/dataprovider/class" not found |
186
|
|
|
*/ |
|
|
|
|
187
|
|
|
public function testGetExceptionOfUndefinedDataProviderClass() |
188
|
|
|
{ |
189
|
|
|
$config = $this->createMock(ConfigInterface::class); |
190
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
191
|
|
|
'test' => [ |
192
|
|
|
'data' => [ |
193
|
|
|
'class' => '/bad/dataprovider/class', |
194
|
|
|
] |
195
|
|
|
] |
196
|
|
|
]); |
|
|
|
|
197
|
|
|
|
198
|
|
|
$manager = new Manager(); |
199
|
|
|
$manager->setConfig($config); |
|
|
|
|
200
|
|
|
$manager->get('test'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
|
|
|
|
204
|
|
|
* @covers ::<public> |
|
|
|
|
205
|
|
|
* @covers ::create |
|
|
|
|
206
|
|
|
* @covers ::createDataProvider |
|
|
|
|
207
|
|
|
* @expectedException \EasyDictionary\Exception\InvalidConfigurationException |
|
|
|
|
208
|
|
|
* @expectedExceptionMessage Class "stdClass" is not implement required interface |
209
|
|
|
*/ |
|
|
|
|
210
|
|
View Code Duplication |
public function testGetExceptionOfBadDataProviderClass() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$config = $this->createMock(ConfigInterface::class); |
213
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
214
|
|
|
'test' => [ |
215
|
|
|
'data' => [ |
216
|
|
|
'class' => \stdClass::class, |
217
|
|
|
] |
218
|
|
|
] |
219
|
|
|
]); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$manager = new Manager(); |
222
|
|
|
$manager->setConfig($config); |
|
|
|
|
223
|
|
|
$manager->get('test'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
|
|
|
|
227
|
|
|
* @covers \EasyDictionary\DataProvider\Simple |
|
|
|
|
228
|
|
|
* @covers ::<public> |
|
|
|
|
229
|
|
|
* @covers ::create |
|
|
|
|
230
|
|
|
* @covers ::createDataProvider |
|
|
|
|
231
|
|
|
* @expectedException \EasyDictionary\Exception\InvalidConfigurationException |
|
|
|
|
232
|
|
|
* @expectedExceptionMessage Class "/bad/dictionary/class" not found |
233
|
|
|
*/ |
|
|
|
|
234
|
|
View Code Duplication |
public function testGetExceptionOfUndefinedDictionaryClass() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$config = $this->createMock(ConfigInterface::class); |
237
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
238
|
|
|
'test' => [ |
239
|
|
|
'class' => '/bad/dictionary/class', |
240
|
|
|
'data' => [ |
241
|
|
|
'class' => Simple::class, |
242
|
|
|
] |
243
|
|
|
] |
244
|
|
|
]); |
|
|
|
|
245
|
|
|
|
246
|
|
|
$manager = new Manager(); |
247
|
|
|
$manager->setConfig($config); |
|
|
|
|
248
|
|
|
$manager->get('test'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
|
|
|
|
252
|
|
|
* @covers \EasyDictionary\DataProvider\Simple |
|
|
|
|
253
|
|
|
* @covers ::<public> |
|
|
|
|
254
|
|
|
* @covers ::create |
|
|
|
|
255
|
|
|
* @covers ::createDataProvider |
|
|
|
|
256
|
|
|
* @expectedException \EasyDictionary\Exception\InvalidConfigurationException |
|
|
|
|
257
|
|
|
* @expectedExceptionMessage Class "stdClass" not found |
258
|
|
|
*/ |
|
|
|
|
259
|
|
View Code Duplication |
public function testGetExceptionOfBadDictionaryClass() |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$config = $this->createMock(ConfigInterface::class); |
262
|
|
|
$config->method('getDictionaryConfig')->willReturn([ |
|
|
|
|
263
|
|
|
'test' => [ |
264
|
|
|
'class' => \stdClass::class, |
265
|
|
|
'data' => [ |
266
|
|
|
'class' => Simple::class, |
267
|
|
|
] |
268
|
|
|
] |
269
|
|
|
]); |
|
|
|
|
270
|
|
|
|
271
|
|
|
$manager = new Manager(); |
272
|
|
|
$manager->setConfig($config); |
|
|
|
|
273
|
|
|
$manager->get('test'); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|