Completed
Push — master ( fdbeef...265a2d )
by jerome
21:34
created

DefaultContext   D

Complexity

Total Complexity 92

Size/Duplication

Total Lines 621
Duplicated Lines 6.92 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 92
c 7
b 0
f 0
lcom 1
cbo 6
dl 43
loc 621
rs 4.7875

43 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePageUrl() 0 18 3
A getRepository() 0 4 1
B thereIsUser() 0 31 5
A thereIsGame() 0 22 3
A iAmOnThePage() 0 4 1
A iShouldBeOnThePage() 0 5 1
A accessShouldBeForbidden() 0 6 1
A iShouldBeUnauthorizedOnThePage() 0 5 1
A iLeaveFieldEmpty() 0 4 1
A iShouldSeeFieldsOnError() 0 4 1
A iShouldSeeAlertMessage() 0 6 2
B thereAreFollowingUsers() 0 16 6
A iAmLoggedInWithAccount() 0 14 2
A thereAreFollowingGames() 0 16 4
C whenIFillInFormWith() 0 29 8
A fillCheckbox() 0 14 3
A fillRadio() 0 11 3
A iShouldSeeResourceWithValueInThatList() 0 4 1
A iShouldNotSeeResourceWithValueInThatList() 0 4 1
A iShouldSeeThatMuchResourcesInTheList() 8 8 2
A iShouldBeOnTheResourcePage() 8 8 1
A iAmOnTheResourcePage() 0 8 1
A iAmOnTheResourcePageByName() 0 4 1
A iAmDoingSomethingWithResource() 9 9 1
A iAmDoingSomethingWithResourceByName() 0 4 1
A iShouldBeDoingSomethingWithResource() 10 10 1
A iShouldBeDoingSomethingWithResourceByName() 0 4 1
A assertStatusCodeEquals() 0 4 1
A thereAreFollowingPlugins() 0 14 2
A thereIsPlugin() 0 20 3
A iShouldSeeAssociatedGames() 0 4 1
A thereAreFollowingGroups() 0 12 4
B thereIsGroup() 0 27 4
A validate() 0 8 2
A iShouldSeeButton() 0 6 1
A iShouldNotSeeButton() 0 6 1
A iShouldBeOn403() 0 7 1
A iShouldSeeCheckboxes() 0 6 1
A iShouldSeeOptionsInSelect() 0 7 1
A thereAreFollowingMachines() 0 18 4
B thereIsMachine() 0 24 4
A findField() 0 11 3
A iShouldSeeThatMuchServersInTheList() 8 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DefaultContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DefaultContext, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DP\Core\CoreBundle\Behat;
13
14
use FOS\UserBundle\Doctrine\UserManager;
15
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext as SyliusDefaultContext;
16
use Behat\Gherkin\Node\TableNode;
17
18
class DefaultContext extends SyliusDefaultContext
19
{
20
    protected $users = [];
21
22
    /**
23
     * Actions.
24
     *
25
     * @var array
26
     */
27
    protected $actions = array(
28
        'viewing'  => 'show',
29
        'creation' => 'create',
30
        'editing'  => 'update',
31
        'building' => 'build',
32
        'testing'  => 'connection_test',
33
    );
34
35
    /** {@inheritdoc} */
36
    protected function generatePageUrl($page, array $parameters = array())
37
    {
38
        if (is_object($page)) {
39
            return $this->locatePath($this->generateUrl($page, $parameters));
0 ignored issues
show
Documentation introduced by
$page is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
        }
41
42
        $route  = str_replace(' ', '_', trim($page));
43
        $routes = $this->getContainer()->get('router')->getRouteCollection();
44
45
        if (null === $routes->get($route)) {
46
            $route = 'dedipanel_'.$route;
47
        }
48
49
        $route = str_replace(array_keys($this->actions), array_values($this->actions), $route);
50
        $route = str_replace(' ', '_', $route);
51
52
        return $this->locatePath($this->generateUrl($route, $parameters));
53
    }
54
55
    protected function getRepository($resource)
56
    {
57
        return $this->getService('dedipanel.repository.'.$resource);
58
    }
59
60
    public function thereIsUser($username, $email, $password, $role = null, $enabled = true, $group = null, $flush = true)
61
    {
62
        if (null === $user = $this->getRepository('user')->findOneBy(array('username' => $username))) {
63
            /* @var $user FOS\UserBundle\Model\UserInterface */
64
            $user = $this->getRepository('user')->createNew();
65
            $user->setUsername($username);
66
            $user->setEmail($email);
67
            $user->setEnabled($enabled);
68
            $user->setPlainPassword($password);
69
70
            if (null !== $role) {
71
                $user->addRole($role);
72
            }
73
74
            if ($group !== null) {
75
                $group = $this->thereIsGroup($group);
76
                $user->setGroup($group);
77
            }
78
79
            $this->validate($user);
80
            $this->getEntityManager()->persist($user);
81
82
            if ($flush) {
83
                $this->getEntityManager()->flush();
84
            }
85
86
            $this->users[$username] = $password;
87
        }
88
89
        return $user;
90
    }
91
92
    public function thereIsGame($name, $installName = null, $launchName = null, $bin = null, $type = null, $available = true, $flush = true)
93
    {
94
        if (null === $game = $this->getRepository('game')->findOneBy(array('name' => $name))) {
95
            $game = $this->getRepository('game')->createNew();
96
            $game->setName($name);
97
            $game->setInstallName($installName);
98
            $game->setLaunchName($launchName);
99
            $game->setBin($bin);
100
            $game->setType($type);
101
            $game->setAvailable($available);
102
103
            $this->validate($game);
104
105
            $this->getEntityManager()->persist($game);
106
107
            if ($flush) {
108
                $this->getEntityManager()->flush();
109
            }
110
        }
111
112
        return $game;
113
    }
114
115
    /**
116
     * @Given /^I am on the (.+) (page)?$/
117
     * @When /^I go to the (.+) (page)?$/
118
     */
119
    public function iAmOnThePage($page)
120
    {
121
        $this->getSession()->visit($this->generatePageUrl($page));
122
    }
123
124
    /**
125
     * @Then /^I should be on the (.+) (page)$/
126
     * @Then /^I should be redirected to the (.+) (page)$/
127
     * @Then /^I should still be on the (.+) (page)$/
128
     */
129
    public function iShouldBeOnThePage($page)
130
    {
131
        $this->assertSession()->addressEquals($this->generatePageUrl($page));
132
        $this->assertStatusCodeEquals(200);
133
    }
134
135
    /**
136
     * @Then /^access should be forbidden$/
137
     */
138
    public function accessShouldBeForbidden()
139
    {
140
        $this->assertStatusCodeEquals(403);
141
        $this->iShouldSeeAlertMessage(1, 'error');
142
        $this->assertSession()->pageTextContains("Vous n'avez pas accès à cette page.");
143
    }
144
145
    /**
146
     * @Then /^I should be unauthorized on the (.+) (page)$/
147
     */
148
    public function iShouldBeUnauthorizedOnThePage($page)
149
    {
150
        $this->assertSession()->addressEquals($this->generatePageUrl($page));
151
        $this->assertStatusCodeEquals(403);
152
    }
153
154
    /**
155
     * @Given /^I leave "([^"]*)" empty$/
156
     * @Given /^I leave "([^"]*)" field blank/
157
     */
158
    public function iLeaveFieldEmpty($field)
159
    {
160
        $this->fillField($field, '');
161
    }
162
163
    /**
164
     * @Then /^I should see (\d+) validation errors?$/
165
     */
166
    public function iShouldSeeFieldsOnError($amount)
167
    {
168
        $this->iShouldSeeAlertMessage($amount, 'error');
169
    }
170
171
    /**
172
     * @Then /^I should see (\d+) (?:alert )?((error|success) )?message$/
173
     */
174
    public function iShouldSeeAlertMessage($amount, $type = '')
175
    {
176
        $class = '.alert' . (!empty($type) ? '-' . $type : '');
177
178
        $this->assertSession()->elementsCount('css', $class . ' > ul > li', $amount);
179
    }
180
181
    /**
182
     * @Given /^there are following users:$/
183
     */
184
    public function thereAreFollowingUsers(TableNode $table)
185
    {
186
        foreach ($table->getHash() as $data) {
187
            $this->thereIsUser(
188
                $data['username'],
189
                $data['email'],
190
                $data['password'],
191
                isset($data['role']) ? $data['role'] : 'ROLE_USER',
192
                isset($data['enabled']) ? $data['enabled'] : true,
193
                isset($data['group']) && !empty($data['group']) ? $data['group'] : null,
194
                false
195
            );
196
        }
197
198
        $this->getEntityManager()->flush();
199
    }
200
201
    /**
202
     * @Given /^I am logged in with (.*) account$/
203
     */
204
    public function iAmLoggedInWithAccount($username)
205
    {
206
        $password = 'test1234';
207
208
        if (isset($this->users[$username])) {
209
            $password = $this->users[$username];
210
        }
211
212
        $this->getSession()->visit($this->generatePageUrl('fos_user_security_login'));
213
214
        $this->fillField("Nom d'utilisateur", $username);
215
        $this->fillField('Mot de passe', $password);
216
        $this->pressButton('Connexion');
217
    }
218
219
    /**
220
     * @Given /^there are following games:$/
221
     */
222
    public function thereAreFollowingGames(TableNode $table)
223
    {
224
        foreach ($table->getHash() as $data) {
225
            $this->thereIsGame(
226
                $data['name'],
227
                $data['installName'],
228
                isset($data['launchName']) ? $data['launchName'] : $data['installName'],
229
                $data['bin'],
230
                $data['type'],
231
                (isset($data['available']) && $data['available'] == 'yes'),
232
                false
233
            );
234
        }
235
236
        $this->getEntityManager()->flush();
237
    }
238
239
    /**
240
     * @When /^I fill in (.+) form with:$/
241
     */
242
    public function whenIFillInFormWith($base, TableNode $table)
243
    {
244
        $page = $this->getSession()->getPage();
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
245
246
        foreach ($table->getTable() AS $item) {
247
            list($name,$value) = $item;
248
            $field     = $this->findField($base, $name);
249
            $fieldName = $field->getAttribute('name');
250
251
            if ($name == 'machine' || $name == 'game') {
252
                $key = ($name == 'machine' ? 'username' : 'name');
253
                $entity = $this->findOneBy($name, array($key => $value));
254
                $value = $entity->getId();
255
            }
256
257
            if ($field->getTagName() == 'select') {
258
                $this->selectOption($fieldName, $value);
259
            }
260
            elseif ($field->getAttribute('type') == 'checkbox') {
261
                $this->fillCheckbox($fieldName, $value);
262
            }
263
            elseif ($field->getAttribute('type') == 'radio') {
264
                $this->fillRadio($fieldName, $value);
265
            }
266
            else {
267
                $this->fillField($fieldName, $value);
268
            }
269
        }
270
    }
271
272
    public function fillCheckbox($fieldName, $value)
273
    {
274
        $page = $this->getSession()->getPage();
275
276
        if ($value == 'yes') {
277
            $page->checkField($fieldName);
278
        }
279
        elseif ($value == 'no') {
280
            $page->uncheckField($fieldName);
281
        }
282
        else {
283
            throw new \RuntimeException(sprintf('Unsupported value "%s" for the checkbox field "%s"', $value, $fieldName));
284
        }
285
    }
286
287
    public function fillRadio($fieldName, $value)
288
    {
289
        if ($value == 'yes') {
290
            $value = 1;
291
        }
292
        elseif ($value == 'no') {
293
            $value = 0;
294
        }
295
296
        $this->fillField($fieldName, $value);
297
    }
298
299
    /**
300
     * @Then /^I should see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
301
     */
302
    public function iShouldSeeResourceWithValueInThatList($value)
303
    {
304
        $this->assertSession()->elementTextContains('css', 'table', $value);
305
    }
306
307
    /**
308
     * @Then /^I should not see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/
309
     */
310
    public function iShouldNotSeeResourceWithValueInThatList($value)
311
    {
312
        $this->assertSession()->elementTextNotContains('css', 'table', $value);
313
    }
314
315
    /**
316
     * @Then /^I should see (\d+) ([^"" ]*) in (that|the) list$/
317
     */
318 View Code Duplication
    public function iShouldSeeThatMuchResourcesInTheList($amount, $type)
319
    {
320
        if (1 === count($this->getSession()->getPage()->findAll('css', 'table'))) {
321
            $this->assertSession()->elementsCount('css', 'table tbody > tr', $amount);
322
        } else {
323
            $this->assertSession()->elementsCount('css', sprintf('table#%s tbody > tr', str_replace(' ', '-', $type)), $amount);
324
        }
325
    }
326
327
    /**
328
     * @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
329
     * @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
330
     */
331 View Code Duplication
    public function iShouldBeOnTheResourcePage($type, $property, $value)
332
    {
333
        $type = str_replace(' ', '_', $type);
334
        $resource = $this->findOneBy($type, array($property => $value));
335
336
        $this->assertSession()->addressEquals($this->generatePageUrl(sprintf('%s_show', $type), array('id' => $resource->getId())));
337
        $this->assertStatusCodeEquals(200);
338
    }
339
340
    /**
341
     * @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/
342
     * @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/
343
     */
344
    public function iAmOnTheResourcePage($type, $property, $value)
345
    {
346
        $type = str_replace(' ', '_', $type);
347
348
        $resource = $this->findOneBy($type, array($property => $value));
349
350
        $this->getSession()->visit($this->generatePageUrl(sprintf('%s_show', $type), array('id' => $resource->getId())));
351
    }
352
353
    /**
354
     * @Given /^I am on the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/
355
     * @Given /^I go to the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/
356
     */
357
    public function iAmOnTheResourcePageByName($type, $name)
358
    {
359
        $this->iAmOnTheResourcePage($type, 'name', $name);
360
    }
361
362
    /**
363
     * @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/
364
     */
365 View Code Duplication
    public function iAmDoingSomethingWithResource($action, $type, $property, $value)
366
    {
367
        $type = str_replace(' ', '_', $type);
368
369
        $action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
370
        $resource = $this->findOneBy($type, array($property => $value));
371
372
        $this->getSession()->visit($this->generatePageUrl(sprintf('%s_%s', $type, $action), array('id' => $resource->getId())));
373
    }
374
375
    /**
376
     * @Given /^I am (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/
377
     */
378
    public function iAmDoingSomethingWithResourceByName($action, $type, $name)
379
    {
380
        $this->iAmDoingSomethingWithResource($action, $type, 'name', $name);
381
    }
382
383
    /**
384
     * @Then /^I should be (building|viewing|editing|testing) ([^"]*) with ([^"]*) "([^""]*)"$/
385
     */
386 View Code Duplication
    public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value)
387
    {
388
        $type = str_replace(' ', '_', $type);
389
390
        $action = str_replace(array_keys($this->actions), array_values($this->actions), $action);
391
        $resource = $this->findOneBy($type, array($property => $value));
392
393
        $this->assertSession()->addressEquals($this->generatePageUrl(sprintf('dedipanel_%s_%s', $type, $action), array('id' => $resource->getId())));
394
        $this->assertStatusCodeEquals(200);
395
    }
396
397
    /**
398
     * @Then /^I should be (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/
399
     */
400
    public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name)
401
    {
402
        $this->iShouldBeDoingSomethingWithResource($action, $type, 'name', $name);
403
    }
404
405
    /**
406
     * Assert that given code equals the current one.
407
     *
408
     * @param integer $code
409
     */
410
    protected function assertStatusCodeEquals($code)
411
    {
412
        $this->assertSession()->statusCodeEquals($code);
413
    }
414
415
    /**
416
     * @Given /^there are following plugins:$/
417
     */
418
    public function thereAreFollowingPlugins(TableNode $table)
419
    {
420
        foreach ($table->getHash() as $data) {
421
            $this->thereIsPlugin(
422
                $data['name'],
423
                $data['version'],
424
                $data['scriptName'],
425
                'http://' . $data['downloadUrl'],
426
                false
427
            );
428
        }
429
430
        $this->getEntityManager()->flush();
431
    }
432
433
    public function thereIsPlugin($name, $version, $scriptName, $downloadUrl, $flush = true)
434
    {
435
        if (null === $plugin = $this->getRepository('plugin')->findOneBy(array('name' => $name))) {
436
            $plugin = $this->getRepository('plugin')->createNew();
437
            $plugin->setName($name);
438
            $plugin->setVersion($version);
439
            $plugin->setScriptName($scriptName);
440
            $plugin->setDownloadUrl($downloadUrl);
441
442
            $this->validate($plugin);
443
444
            $this->getEntityManager()->persist($plugin);
445
446
            if ($flush) {
447
                $this->getEntityManager()->flush();
448
            }
449
        }
450
451
        return $plugin;
452
    }
453
454
    /**
455
     * @Then /^I should see (\d+) associated games?$/
456
     */
457
    public function iShouldSeeAssociatedGames($amount)
458
    {
459
        $this->assertSession()->elementsCount('css', 'ul.associated-games > li', $amount);
460
    }
461
462
    /**
463
     * @Given /^there are following groups:$/
464
     */
465
    public function thereAreFollowingGroups(TableNode $table)
466
    {
467
        foreach ($table->getHash() as $data) {
468
            $this->thereIsGroup(
469
                $data['name'],
470
                isset($data['roles']) ? array_map('trim', explode(',', $data['roles'])) : array(),
471
                !empty($data['parent']) ? $data['parent'] : null
472
            );
473
        }
474
475
        $this->getEntityManager()->flush();
476
    }
477
478
    public function thereIsGroup($name, array $roles = array(), $parent = null, $flush = true)
479
    {
480
        if (null === $group = $this->getRepository('group')->findOneBy(array('name' => $name))) {
481
            /* @var $group UserInterface */
482
            $group = $this->getRepository('group')->createNew();
483
            $group->setName($name);
484
            $group->setRoles($roles);
485
486
            if ($parent !== null) {
487
                $parent = $this->thereIsGroup($parent);
488
                $group->setParent($parent);
489
                $parent->addChildren($group);
490
491
                $this->getEntityManager()->persist($parent);
492
            }
493
494
            $this->validate($group);
495
496
            $this->getEntityManager()->persist($group);
497
498
            if ($flush) {
499
                $this->getEntityManager()->flush();
500
            }
501
        }
502
503
        return $group;
504
    }
505
506
    protected function validate($data)
507
    {
508
        $violationList = $this->getService('validator')->validate($data);
509
510
        if ($violationList->count() != 0) {
511
            throw new \RuntimeException(sprintf('Data not valid (%s).', $violationList));
512
        }
513
    }
514
515
    /**
516
     * @Then /^I should see (\d+) buttons? "([^"]+)"$/
517
     */
518
    public function iShouldSeeButton($count, $value)
519
    {
520
        $locator = sprintf('a:contains("%s"), button:contains("%s")', $value, $value);
521
522
        $this->assertSession()->elementsCount('css', $locator, $count);
523
    }
524
525
    /**
526
     * @Then /^I should not see button "([^"]+)"$/
527
     */
528
    public function iShouldNotSeeButton($value)
529
    {
530
        $locator = sprintf('a:contains("%s"), button:contains("%s")', $value, $value);
531
532
        $this->assertSession()->elementsCount('css', $locator, 0);
533
    }
534
535
    /**
536
     * @Then /^I should be on 403 page$/
537
     * @Then /^I should be on 403 page with "([^"]+)"$/
538
     */
539
    public function iShouldBeOn403($message = "Vous n'avez pas accès à cette page.")
540
    {
541
        $this->assertStatusCodeEquals(403);
542
543
        $this->iShouldSeeAlertMessage(1, 'error');
544
        $this->assertSession()->pageTextContains($message);
545
    }
546
547
    /**
548
     * @Then /^I should see (\d+) "([^"]+)" checkbox(?:es)? in "([^"]+)" form$/
549
     */
550
    public function iShouldSeeCheckboxes($count, $type, $form)
551
    {
552
        $locator = sprintf('//input[@type="checkbox"][@name="%s[%s][]"]', $form, $type);
553
554
        $this->assertSession()->elementscount('xpath', $locator, $count);
555
    }
556
557
    /**
558
     * @Then /^I should see (\d+) "([^"]+)" options in "([^"]+)" form$/
559
     */
560
    public function iShouldSeeOptionsInSelect($count, $type, $form)
561
    {
562
        $xpath   = sprintf('%s[%s]', $form, $type);
563
        $locator = sprintf('//select[@name="%s" or @name="%s[]"]/option', $xpath, $xpath);
564
565
        $this->assertSession()->elementsCount('xpath', $locator, $count);
566
    }
567
568
    /**
569
     * @Given /^there are following machines:$/
570
     */
571
    public function thereAreFollowingMachines(TableNode $table)
572
    {
573
        foreach ($table->getHash() as $data) {
574
            $groups = isset($data['groups']) ? $data['groups'] : $data['group'];
575
            $groups = array_map('trim', explode(',', $groups));
576
577
            $this->thereIsMachine(
578
                $data['username'],
579
                $data['privateIp'],
580
                $data['key'],
581
                $groups,
582
                (isset($data['is64Bit']) ? $data['is64Bit'] == 'yes' : false),
583
                false
584
            );
585
        }
586
587
        $this->getEntityManager()->flush();
588
    }
589
590
    public function thereIsMachine($username, $privateIp = null, $privateKey = null, $groups = array(), $is64Bit = false, $flush = true)
591
    {
592
        if (null === $machine = $this->getRepository('machine')->findOneBy(array('username' => $username))) {
593
            $machine = $this->getRepository('machine')->createNew();
594
            $machine->setIp($privateIp);
595
            $machine->setUsername($username);
596
            $machine->setPrivateKeyName($privateKey);
597
            $machine->setHome('/home/' . $username);
598
            $machine->setIs64Bit($is64Bit);
599
600
            foreach ($groups AS $group) {
601
                $group = $this->thereIsGroup($group);
602
                $machine->addGroup($group);
603
            }
604
605
            $this->getEntityManager()->persist($machine);
606
607
            if ($flush) {
608
                $this->getEntityManager()->flush();
609
            }
610
        }
611
612
        return $machine;
613
    }
614
615
    public function findField($form, $fieldName)
616
    {
617
        $page = $this->getSession()->getPage();
618
        $fieldName = sprintf('%s[%s]', $form, $fieldName);
619
620
        if ((null === $field = $page->findField($fieldName)) && (null === $field = $page->findField($fieldName . '[]'))) {
621
            throw new \RuntimeException(sprintf('Form field with id|name|label|value "%s" or "%s[]" not found.', $fieldName, $fieldName));
622
        }
623
624
        return $field;
625
    }
626
627
    /**
628
     * @Then /^I should see (\d+) ([^" ]*) servers in (that|the) list$/
629
     */
630 View Code Duplication
    public function iShouldSeeThatMuchServersInTheList($amount, $type)
631
    {
632
        if (1 === count($this->getSession()->getPage()->findAll('css', '.server-list'))) {
633
            $this->assertSession()->elementsCount('css', '.server-list > .server-item', $amount);
634
        } else {
635
            $this->assertSession()->elementsCount('css', sprintf('#%s.server-list > .server-item', $type), $amount);
636
        }
637
    }
638
}
639