1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DP\Core\CoreBundle\Behat; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\TableNode; |
6
|
|
|
use DP\GameServer\MinecraftServerBundle\Entity\MinecraftServer; |
7
|
|
|
use DP\GameServer\SteamServerBundle\Entity\SteamServer; |
8
|
|
|
use DP\VoipServer\TeamspeakServerBundle\Entity\TeamspeakServer; |
9
|
|
|
use DP\VoipServer\TeamspeakServerBundle\Entity\TeamspeakServerInstance; |
10
|
|
|
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext as SyliusDefaultContext; |
11
|
|
|
|
12
|
|
|
class ResourceContext extends SyliusDefaultContext |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $users = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @Given /^there are following users:$/ |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function thereAreFollowingUsers(TableNode $table) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
foreach ($table->getHash() as $data) { |
25
|
|
|
$this->thereIsUser( |
26
|
|
|
$data['username'], |
27
|
|
|
$data['email'], |
28
|
|
|
$data['password'], |
29
|
|
|
isset($data['role']) ? $data['role'] : 'ROLE_USER', |
30
|
|
|
isset($data['enabled']) ? $data['enabled'] : true, |
31
|
|
|
isset($data['group']) && !empty($data['group']) ? $data['group'] : null, |
32
|
|
|
false |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->getEntityManager()->flush(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function thereIsUser($username, $email, $password, $role = null, $enabled = true, $group = null, $flush = true) |
40
|
|
|
{ |
41
|
|
|
if (null === $user = $this->getRepository('user')->findOneBy(array('username' => $username))) { |
42
|
|
|
/* @var $user \DP\Core\UserBundle\Entity\User */ |
43
|
|
|
$user = $this->getRepository('user')->createNew(); |
44
|
|
|
$user->setUsername($username); |
45
|
|
|
$user->setEmail($email); |
46
|
|
|
$user->setEnabled($enabled); |
47
|
|
|
$user->setPlainPassword($password); |
48
|
|
|
|
49
|
|
|
if (null !== $role) { |
50
|
|
|
$user->addRole($role); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($group !== null) { |
54
|
|
|
$group = $this->thereIsGroup($group); |
55
|
|
|
$user->setGroup($group); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->validate($user); |
59
|
|
|
$this->getEntityManager()->persist($user); |
60
|
|
|
|
61
|
|
|
if ($flush) { |
62
|
|
|
$this->getEntityManager()->flush(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->users[$username] = $password; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $user; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @Given /^there are following groups:$/ |
73
|
|
|
*/ |
74
|
|
|
public function thereAreFollowingGroups(TableNode $table) |
75
|
|
|
{ |
76
|
|
|
foreach ($table->getHash() as $data) { |
77
|
|
|
$this->thereIsGroup( |
78
|
|
|
$data['name'], |
79
|
|
|
isset($data['roles']) ? array_map('trim', explode(',', $data['roles'])) : array(), |
80
|
|
|
!empty($data['parent']) ? $data['parent'] : null |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->getEntityManager()->flush(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function thereIsGroup($name, array $roles = array(), $parent = null, $flush = true) |
88
|
|
|
{ |
89
|
|
|
if (null === $group = $this->getRepository('group')->findOneBy(array('name' => $name))) { |
90
|
|
|
/* @var $group \DP\Core\UserBundle\Entity\Group */ |
91
|
|
|
$group = $this->getRepository('group')->createNew(); |
92
|
|
|
$group->setName($name); |
93
|
|
|
$group->setRoles($roles); |
94
|
|
|
|
95
|
|
|
if ($parent !== null) { |
96
|
|
|
$parent = $this->thereIsGroup($parent); |
97
|
|
|
$group->setParent($parent); |
98
|
|
|
$parent->addChildren($group); |
99
|
|
|
|
100
|
|
|
$this->getEntityManager()->persist($parent); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->validate($group); |
104
|
|
|
|
105
|
|
|
$this->getEntityManager()->persist($group); |
106
|
|
|
|
107
|
|
|
if ($flush) { |
108
|
|
|
$this->getEntityManager()->flush(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $group; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @Given /^there are following machines:$/ |
117
|
|
|
*/ |
118
|
|
|
public function thereAreFollowingMachines(TableNode $table) |
119
|
|
|
{ |
120
|
|
|
foreach ($table->getHash() as $data) { |
121
|
|
|
$groups = isset($data['groups']) ? $data['groups'] : $data['group']; |
122
|
|
|
|
123
|
|
|
if (!empty($groups)) { |
124
|
|
|
$groups = array_map('trim', explode(',', $groups)); |
125
|
|
|
} else { |
126
|
|
|
$groups = []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->thereIsMachine( |
130
|
|
|
$data['username'], |
131
|
|
|
$data['privateIp'], |
132
|
|
|
$data['key'], |
133
|
|
|
$groups, |
134
|
|
|
(isset($data['is64Bit']) ? $data['is64Bit'] == 'yes' : false), |
135
|
|
|
false |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->getEntityManager()->flush(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function thereIsMachine($username, $privateIp = null, $privateKey = null, $groups = array(), $is64Bit = false, $flush = true) |
143
|
|
|
{ |
144
|
|
|
if (null === $machine = $this->getRepository('machine')->findOneBy(array('username' => $username))) { |
145
|
|
|
$machine = $this->getRepository('machine')->createNew(); |
146
|
|
|
$machine->setIp($privateIp); |
147
|
|
|
$machine->setUsername($username); |
148
|
|
|
$machine->setPrivateKeyName($privateKey); |
149
|
|
|
$machine->setHome('/home/' . $username); |
150
|
|
|
$machine->setIs64Bit($is64Bit); |
151
|
|
|
|
152
|
|
|
foreach ($groups AS $group) { |
153
|
|
|
$group = $this->thereIsGroup($group); |
154
|
|
|
$machine->addGroup($group); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->getEntityManager()->persist($machine); |
158
|
|
|
|
159
|
|
|
if ($flush) { |
160
|
|
|
$this->getEntityManager()->flush(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $machine; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @Given /^there are following games:$/ |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function thereAreFollowingGames(TableNode $table) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
foreach ($table->getHash() as $data) { |
173
|
|
|
$this->thereIsGame( |
174
|
|
|
$data['name'], |
175
|
|
|
$data['launchName'], |
176
|
|
|
!empty($data['appId']) ? $data['appId'] : null, |
177
|
|
|
!empty($data['appMod']) ? $data['appMod'] : null, |
178
|
|
|
$data['bin'], |
179
|
|
|
$data['type'], |
180
|
|
|
(isset($data['available']) && $data['available'] == 'yes'), |
181
|
|
|
false |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->getEntityManager()->flush(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function thereIsGame( |
189
|
|
|
$name, |
190
|
|
|
$launchName = null, |
191
|
|
|
$appId = null, |
192
|
|
|
$appMod = null, |
193
|
|
|
$bin = null, |
194
|
|
|
$type = null, |
195
|
|
|
$available = true, |
196
|
|
|
$flush = true |
197
|
|
|
) { |
198
|
|
|
/** @var \DP\Core\GameBundle\Entity\Game $game */ |
199
|
|
|
$game = $this->getRepository('game')->findOneBy(array('name' => $name)); |
200
|
|
|
|
201
|
|
|
if (null === $game) { |
202
|
|
|
$game = $this->getRepository('game')->createNew(); |
203
|
|
|
$game->setName($name); |
204
|
|
|
$game->setLaunchName($launchName); |
205
|
|
|
$game->setAppId($appId); |
206
|
|
|
$game->setAppMod($appMod); |
207
|
|
|
$game->setBin($bin); |
208
|
|
|
$game->setType($type); |
209
|
|
|
$game->setAvailable($available); |
210
|
|
|
|
211
|
|
|
$this->validate($game); |
212
|
|
|
|
213
|
|
|
$this->getEntityManager()->persist($game); |
214
|
|
|
|
215
|
|
|
if ($flush) { |
216
|
|
|
$this->getEntityManager()->flush(); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $game; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @Given /^there are following plugins:$/ |
225
|
|
|
*/ |
226
|
|
|
public function thereAreFollowingPlugins(TableNode $table) |
227
|
|
|
{ |
228
|
|
|
foreach ($table->getHash() as $data) { |
229
|
|
|
$this->thereIsPlugin( |
230
|
|
|
$data['name'], |
231
|
|
|
$data['version'], |
232
|
|
|
$data['scriptName'], |
233
|
|
|
'http://' . $data['downloadUrl'], |
234
|
|
|
false |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$this->getEntityManager()->flush(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function thereIsPlugin($name, $version, $scriptName, $downloadUrl, $flush = true) |
242
|
|
|
{ |
243
|
|
|
if (null === $plugin = $this->getRepository('plugin')->findOneBy(array('name' => $name))) { |
244
|
|
|
$plugin = $this->getRepository('plugin')->createNew(); |
245
|
|
|
$plugin->setName($name); |
246
|
|
|
$plugin->setVersion($version); |
247
|
|
|
$plugin->setScriptName($scriptName); |
248
|
|
|
$plugin->setDownloadUrl($downloadUrl); |
249
|
|
|
|
250
|
|
|
$this->validate($plugin); |
251
|
|
|
|
252
|
|
|
$this->getEntityManager()->persist($plugin); |
253
|
|
|
|
254
|
|
|
if ($flush) { |
255
|
|
|
$this->getEntityManager()->flush(); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $plugin; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @Given /^there are following minecraft servers:$/ |
264
|
|
|
*/ |
265
|
|
|
public function thereAreMinecraftServers(TableNode $table) |
266
|
|
|
{ |
267
|
|
|
foreach ($table->getHash() as $data) { |
268
|
|
|
$this->thereIsMinecraftServer( |
269
|
|
|
$data['name'], |
270
|
|
|
$data['machine'], |
271
|
|
|
$data['port'], |
272
|
|
|
$data['queryPort'], |
273
|
|
|
$data['rconPort'], |
274
|
|
|
$data['rconPassword'], |
275
|
|
|
$data['game'], |
276
|
|
|
$data['installDir'], |
277
|
|
|
$data['maxplayers'], |
278
|
|
|
$data['minHeap'], |
279
|
|
|
$data['maxHeap'], |
280
|
|
|
(isset($data['installed']) && $data['installed'] == 'yes'), |
281
|
|
|
false |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$this->getEntityManager()->flush(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function thereIsMinecraftServer($name, $machine = null, $port = 25565, $queryPort = 25565, $rconPort = 25575, $rconPassword = 'test', $game = 'minecraft', $installDir = 'test', $maxplayers = 2, $minHeap = 128, $maxHeap = 256, $installed = true, $flush = true) |
289
|
|
|
{ |
290
|
|
|
if (null === $server = $this->getRepository('minecraft')->findOneBy(array('name' => $name))) { |
291
|
|
|
$game = $this->thereIsGame($game); |
292
|
|
|
$machine = $this->thereIsMachine($machine); |
293
|
|
|
|
294
|
|
|
$server = new MinecraftServer(); |
295
|
|
|
$server->setName($name); |
296
|
|
|
$server->setMachine($machine); |
297
|
|
|
$server->setPort($port); |
298
|
|
|
$server->setQueryPort($queryPort); |
299
|
|
|
$server->setRconPort($rconPort); |
300
|
|
|
$server->setRconPassword($rconPassword); |
301
|
|
|
$server->setGame($game); |
302
|
|
|
$server->setDir($installDir); |
303
|
|
|
$server->setMaxplayers($maxplayers); |
304
|
|
|
$server->setMinHeap($minHeap); |
305
|
|
|
$server->setMaxHeap($maxHeap); |
306
|
|
|
|
307
|
|
|
if ($installed) { |
308
|
|
|
$server->setInstallationStatus(101); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$this->validate($server); |
312
|
|
|
|
313
|
|
|
$this->getEntityManager()->persist($server); |
314
|
|
|
|
315
|
|
|
if ($flush) { |
316
|
|
|
$this->getEntityManager()->flush(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $server; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @Given /^there are following steam servers:$/ |
325
|
|
|
*/ |
326
|
|
|
public function thereAreSteamServers(TableNode $table) |
327
|
|
|
{ |
328
|
|
|
foreach ($table->getHash() as $data) { |
329
|
|
|
$this->thereIsSteamServer( |
330
|
|
|
$data['name'], |
331
|
|
|
$data['machine'], |
332
|
|
|
$data['port'], |
333
|
|
|
$data['rconPassword'], |
334
|
|
|
$data['game'], |
335
|
|
|
$data['installDir'], |
336
|
|
|
$data['maxplayers'], |
337
|
|
|
(isset($data['installed']) && $data['installed'] == 'yes'), |
338
|
|
|
false |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$this->getEntityManager()->flush(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function thereIsSteamServer($name, $machine = null, $port = 27025, $rconPassword = 'test', $game = 'Counter-Strike', $installDir = 'test', $maxplayers = 2, $installed = true, $flush = true) |
346
|
|
|
{ |
347
|
|
|
if (null === $server = $this->getRepository('steam')->findOneBy(array('name' => $name))) { |
348
|
|
|
$game = $this->thereIsGame($game); |
349
|
|
|
$machine = $this->thereIsMachine($machine); |
350
|
|
|
|
351
|
|
|
$server = new SteamServer(); |
352
|
|
|
$server->setName($name); |
353
|
|
|
$server->setMachine($machine); |
354
|
|
|
$server->setPort($port); |
355
|
|
|
$server->setRconPassword($rconPassword); |
356
|
|
|
$server->setGame($game); |
357
|
|
|
$server->setDir($installDir); |
358
|
|
|
$server->setMaxplayers($maxplayers); |
359
|
|
|
|
360
|
|
|
if ($installed) { |
361
|
|
|
$server->setInstallationStatus(101); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$this->validate($server); |
365
|
|
|
|
366
|
|
|
$this->getEntityManager()->persist($server); |
367
|
|
|
|
368
|
|
|
if ($flush) { |
369
|
|
|
$this->getEntityManager()->flush(); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $server; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @Given /^there are following teamspeak servers:$/ |
378
|
|
|
*/ |
379
|
|
|
public function thereAreTeamspeakServers(TableNode $table) |
380
|
|
|
{ |
381
|
|
|
foreach ($table->getHash() as $data) { |
382
|
|
|
$this->thereIsTeamspeakServer( |
383
|
|
|
$data['machine'], |
384
|
|
|
$data['queryPassword'], |
385
|
|
|
$data['installDir'], |
386
|
|
|
(isset($data['installed']) && $data['installed'] == 'yes'), |
387
|
|
|
false |
388
|
|
|
); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$this->getEntityManager()->flush(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function thereIsTeamspeakServer($machine, $queryPassword = 'test', $installDir = 'test', $installed = true, $flush = true) |
395
|
|
|
{ |
396
|
|
|
$machine = $this->thereIsMachine($machine); |
397
|
|
|
|
398
|
|
|
if (null === $server = $this->getRepository('teamspeak')->findOneBy(array('machine' => $machine))) { |
399
|
|
|
$server = new TeamspeakServer(); |
400
|
|
|
$server->setMachine($machine); |
401
|
|
|
$server->setQueryPassword($queryPassword); |
402
|
|
|
$server->setDir($installDir); |
403
|
|
|
|
404
|
|
|
if ($installed) { |
405
|
|
|
$server->setInstallationStatus(101); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$this->validate($server); |
409
|
|
|
|
410
|
|
|
$this->getEntityManager()->persist($server); |
411
|
|
|
|
412
|
|
|
if ($flush) { |
413
|
|
|
$this->getEntityManager()->flush(); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $server; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @Given /^there are following teamspeak instances:$/ |
422
|
|
|
*/ |
423
|
|
|
public function thereAreTeamspeakInstances(TableNode $table) |
424
|
|
|
{ |
425
|
|
|
foreach ($table->getHash() as $data) { |
426
|
|
|
$this->thereIsTeamspeakInstance( |
427
|
|
|
$data['instanceId'], |
428
|
|
|
$data['name'], |
429
|
|
|
$data['server'], |
430
|
|
|
$data['port'], |
431
|
|
|
$data['slots'], |
432
|
|
|
false |
433
|
|
|
); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
$this->getEntityManager()->flush(); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
public function thereIsTeamspeakInstance($instanceId, $name = 'Test', $server = '[email protected]', $port = 9887, $slots = 2, $flush = true) |
440
|
|
|
{ |
441
|
|
|
$parts = explode('@', $server); |
442
|
|
|
$machine = $this->findOneBy('machine', array('username' => $parts[0])); |
443
|
|
|
$server = $this->findOneBy('teamspeak', array('machine' => $machine->getId())); |
444
|
|
|
|
445
|
|
|
if (null === $instance = $this->getRepository('instance', 'teamspeak')->findOneBy(array('server' => $server->getId()))) { |
446
|
|
|
$instance = new TeamspeakServerInstance($server); |
447
|
|
|
$instance->setInstanceId($instanceId); |
448
|
|
|
$instance->setName($name); |
449
|
|
|
$instance->setPort($port); |
450
|
|
|
$instance->setMaxClients($slots); |
451
|
|
|
$instance->setAdminToken('test'); |
452
|
|
|
$instance->setAutostart(true); |
453
|
|
|
|
454
|
|
|
$this->validate($server); |
455
|
|
|
|
456
|
|
|
$this->getEntityManager()->persist($server); |
457
|
|
|
|
458
|
|
|
if ($flush) { |
459
|
|
|
$this->getEntityManager()->flush(); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $instance; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @param string $baseName |
468
|
|
|
*/ |
469
|
|
View Code Duplication |
protected function getRepository($resource, $baseName = null) |
|
|
|
|
470
|
|
|
{ |
471
|
|
|
$service = 'dedipanel.'; |
472
|
|
|
|
473
|
|
|
if (!empty($baseName)) { |
474
|
|
|
$service .= $baseName . '.'; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return $this->getService($service . 'repository.'.$resource); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
View Code Duplication |
protected function findOneBy($type, array $criteria, $repoPrefix = '') |
|
|
|
|
481
|
|
|
{ |
482
|
|
|
$resource = $this |
483
|
|
|
->getRepository($type, $repoPrefix) |
484
|
|
|
->findOneBy($criteria); |
485
|
|
|
|
486
|
|
|
if (null === $resource) { |
487
|
|
|
throw new \InvalidArgumentException( |
488
|
|
|
sprintf('%s for criteria "%s" was not found.', str_replace('_', ' ', ucfirst($type)), serialize($criteria)) |
489
|
|
|
); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
return $resource; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
protected function validate($data) |
496
|
|
|
{ |
497
|
|
|
$violationList = $this->getService('validator')->validate($data); |
498
|
|
|
|
499
|
|
|
if ($violationList->count() != 0) { |
500
|
|
|
throw new \RuntimeException(sprintf('Data not valid (%s).', $violationList)); |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
|
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.