Completed
Push — master ( 584969...3f5763 )
by
unknown
11:30
created

FeatureContext   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
c 2
b 0
f 1
lcom 1
cbo 0
dl 30
loc 108
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A assertCountContactsAddedToForm() 0 4 1
A assertCountOfContacts() 0 5 1
A assertDefaultContact() 0 18 3
A selectContactAsDefault() 15 15 3
A deleteContact() 15 15 3
A getFormContacts() 0 6 1
A getCount() 0 13 4

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace OroCRM\Bundle\AccountBundle\Tests\Behat\Context;
4
5
use Behat\Mink\Exception\ExpectationException;
6
use Behat\MinkExtension\Context\RawMinkContext;
7
use Behat\Mink\Element\NodeElement;
8
9
class FeatureContext extends RawMinkContext
10
{
11
    /**
12
     * @Then /^(?P<contactsCount>(?:|one|two|\d+)) contacts added to form$/
13
     */
14
    public function assertCountContactsAddedToForm($contactsCount)
15
    {
16
        expect($this->getFormContacts())->toHaveCount($this->getCount($contactsCount));
17
    }
18
19
    /**
20
     * @When /^(?:|I should )see (?P<contactsCount>(?:|one|two|\d+)) contact(?:|s)$/
21
     */
22
    public function assertCountOfContacts($contactsCount)
23
    {
24
        expect($this->getSession()->getPage()->findAll('css', '.contact-box'))
25
            ->toHaveCount($this->getCount($contactsCount));
26
    }
27
28
    /**
29
     * @When :name should be default contact
30
     */
31
    public function assertDefaultContact($name)
32
    {
33
        $contactBoxes = $this->getSession()->getPage()->findAll('css', '.contact-box');
34
35
        /** @var NodeElement $box */
36
        foreach ($contactBoxes as $box) {
37
            if (false !== strpos($box->getText(), $name)) {
38
                expect($box->getText())
39
                    ->toMatch('/Default Contact/i');
40
                return;
41
            }
42
        }
43
44
        throw new ExpectationException(
45
            sprintf('Can\'t find contact with "%s" name', $name),
46
            $this->getSession()->getDriver()
47
        );
48
    }
49
50
51
    /**
52
     * @Then /^(?:|I )select ([\w\s]*) contact as default$/
53
     */
54 View Code Duplication
    public function selectContactAsDefault($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
55
    {
56
        foreach ($this->getFormContacts() as $contact) {
57
            if (false !== strpos($contact->getText(), $name)) {
58
                $contact->find('css', 'input[type="radio"]')->click();
59
60
                return;
61
            }
62
        }
63
64
        throw new ExpectationException(
65
            sprintf('Can\'t find contact with "%s" name', $name),
66
            $this->getSession()->getDriver()
67
        );
68
    }
69
70
    /**
71
     * @Then delete :name contact
72
     */
73 View Code Duplication
    public function deleteContact($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        foreach ($this->getFormContacts() as $contact) {
76
            if (false !== strpos($contact->getText(), $name)) {
77
                $contact->find('css', 'i.icon-remove')->click();
78
79
                return;
80
            }
81
        }
82
83
        throw new ExpectationException(
84
            sprintf('Can\'t find contact with "%s" name', $name),
85
            $this->getSession()->getDriver()
86
        );
87
    }
88
89
    /**
90
     * @return NodeElement[]
91
     */
92
    protected function getFormContacts()
93
    {
94
        $page = $this->getSession()->getPage();
95
96
        return $page->findAll('css', 'div[id^="orocrm_account_form_contacts"] .list-group-item');
97
    }
98
99
    /**
100
     * @param int|string $count
101
     * @return int
102
     */
103
    protected function getCount($count)
104
    {
105
        switch (trim($count)) {
106
            case '':
107
                return 1;
108
            case 'one':
109
                return 1;
110
            case 'two':
111
                return 2;
112
            default:
113
                return (int) $count;
114
        }
115
    }
116
}
117