Completed
Push — master ( 535b09...0667cb )
by
unknown
56:23
created

FeatureContext::iDeleteAllAddresses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Tests\Behat\Context;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Behat\Mink\Element\NodeElement;
7
use Doctrine\Common\Inflector\Inflector;
8
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
9
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroElementFactoryAware;
10
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\ElementFactoryDictionary;
11
12
class FeatureContext extends OroFeatureContext implements OroElementFactoryAware
13
{
14
    use ElementFactoryDictionary;
15
16
    /**
17
     * Assert that value of given field is a primary.
18
     * In frontend primary value is marked as bold.
19
     * Also primary value is value that showing in grid
20
     * Example: And Phone "+1 415-731-9375" should be primary
21
     * Example: And email "[email protected]" should be primary
22
     *
23
     * @Then /^(?P<field>[^"]+) "(?P<value>[^"]+)" should be primary$/
24
     */
25
    public function fieldValueShouldBePrimary($field, $value)
26
    {
27
        $labelSelector = sprintf("label:contains('%s')", ucfirst(Inflector::pluralize($field)));
28
        /** @var NodeElement $label */
29
        $label = $this->getSession()->getPage()->find('css', $labelSelector);
30
        self::assertNotNull($label, sprintf('Label "%s" not found', $field));
31
        $contactElements = $label->getParent()->findAll('css', '.contact-collection-element');
32
33
        /** @var NodeElement $contactElement */
34 View Code Duplication
        foreach ($contactElements as $contactElement) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35
            if (false !== stripos($contactElement->getText(), $value)) {
36
                self::assertTrue(
37
                    $contactElement->hasClass('primary'),
38
                    sprintf('Value "%s" was found but it is not primary', $value)
39
                );
40
41
                return;
42
            }
43
        }
44
45
        self::fail(sprintf('Value "%s" in "%s" field not found', $value, $field));
46
    }
47
48
    /**
49
     * Click edit icon (pencil) into address at entity view page
50
     * Example: And click edit LOS ANGELES address
51
     *
52
     * @Given /^click edit (?P<address>[^"]+) address$/
53
     */
54 View Code Duplication
    public function clickEditAddress($address)
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
        $addresses = $this->getSession()->getPage()->findAll('css', 'div.map-address-list .map-item');
57
58
        /** @var NodeElement $actualAddress */
59
        foreach ($addresses as $actualAddress) {
60
            if (false !== strpos($actualAddress->getText(), $address)) {
61
                $actualAddress->find('css', '.item-edit-button')->click();
62
63
                return;
64
            }
65
        }
66
67
        self::fail(sprintf('Address "%s" not found', $address));
68
    }
69
70
    /**
71
     * Delete address form entity view page by clicking on trash icon given address
72
     * Example: When I delete Ukraine address
73
     *
74
     * @When /^(?:|I )delete (?P<address>[^"]+) address$/
75
     */
76
    public function iDeleteAddress($address)
77
    {
78
        $addresses = $this->getSession()->getPage()->findAll('css', 'div.map-address-list .map-item');
79
80
        /** @var NodeElement $actualAddress */
81
        foreach ($addresses as $actualAddress) {
82
            if (false !== strpos($actualAddress->getText(), $address)) {
83
                $removeButton = $actualAddress->find('css', '.item-remove-button');
84
85
                self::assertNotNull(
86
                    $removeButton,
87
                    sprintf('Found address "%s" but it has\'t delete button. Maybe it\'s primary address?', $address)
88
                );
89
90
                $removeButton->click();
91
92
                return;
93
            }
94
        }
95
96
        self::fail(sprintf('Address "%s" not found', $address));
97
    }
98
99
    /**
100
     * Assert that entity view page has default avatar (info-user.png)
101
     *
102
     * @Then avatar should be default avatar
103
     */
104
    public function avatarShouldBeDefaultAvatar()
105
    {
106
        $img = $this->getSession()->getPage()->find('css', 'div.customer-info div.visual img');
107
108
        self::assertNotFalse(stripos($img->getAttribute('src'), 'info-user.png'), 'Avatar is not default avatar');
109
    }
110
111
    /**
112
     * Assert avatar image src, e.g. charlie-sheen.jpg
113
     * Example: And avatar should be "charlie-sheen.jpg"
114
     *
115
     * @Then avatar should be :arg1
116
     */
117
    public function avatarShouldBe($imgName)
118
    {
119
        $img = $this->getSession()->getPage()->find('css', 'div.customer-info div.visual img');
120
121
        self::assertNotFalse(stripos($img->getAttribute('src'), $imgName), sprintf('Avatar is "%s" image', $imgName));
122
    }
123
124
    /**
125
     * Assert that two accounts sets at view entity page
126
     * Example: And Warner Brothers and Columbia Pictures should be set as accounts
127
     *
128
     * @Then /^(?P<acc1>[^"]+) and (?P<acc2>[^"]+) should be set as accounts$/
129
     */
130
    public function assertAccountsNames($acc1, $acc2)
131
    {
132
        $labelSelector = sprintf("label:contains('%s')", 'Accounts');
133
        /** @var NodeElement $label */
134
        $label = $this->getSession()->getPage()->find('css', $labelSelector);
135
        $accounts = $label->getParent()->findAll('css', '.control-label a');
136
        $accounts = array_map(function (NodeElement $a) {
137
            return $a->getText();
138
        }, $accounts);
139
140
        foreach ([$acc1, $acc2] as $acc) {
141
            self::assertTrue(
142
                in_array($acc, $accounts, true),
143
                sprintf('Value "%s" not found in "%s" accounts', $acc, implode(', ', $accounts))
144
            );
145
        }
146
    }
147
148
    /**
149
     * Assert social links
150
     * Example: And should see next social links:
151
     *            | Twitter    | https://twitter.com/charliesheen                  |
152
     *            | Facebook   | https://www.facebook.com/CharlieSheen             |
153
     *            | Google+    | https://profiles.google.com/111536551725236448567 |
154
     *            | LinkedIn   | http://www.linkedin.com/in/charlie-sheen-74755931 |
155
     *
156
     * @Then should see next social links:
157
     */
158
    public function shouldSeeNextSocialLinks(TableNode $table)
159
    {
160
        $labelSelector = sprintf("label:contains('%s')", 'Social');
161
        /** @var NodeElement $label */
162
        $label = $this->getSession()->getPage()->find('css', $labelSelector);
163
        $links = $label->getParent()->findAll('css', 'ul.list-inline li a');
164
165
        $socialNetworks = [];
166
167
        /** @var NodeElement $link */
168
        foreach ($links as $link) {
169
            $socialNetworks[$link->getAttribute('title')] = trim($link->getAttribute('href'));
170
        }
171
172
        foreach ($table->getRows() as list($networkName, $networkLink)) {
173
            self::assertArrayHasKey(
174
                $networkName,
175
                $socialNetworks,
176
                sprintf('%s not found in social networks', $networkName)
177
            );
178
            self::assertEquals(
179
                $networkLink,
180
                $socialNetworks[$networkName],
181
                sprintf('%s expect to be "%s" but got "%s"', $networkName, $networkLink, $socialNetworks[$networkName])
182
            );
183
        }
184
    }
185
186
    /**
187
     * Assert count of addresses in entity view page
188
     * Example: And contact has 2 addresses
189
     * Example: Then contact has one address
190
     * Example: And two addresses should be in page
191
     *
192
     * @Then :count addresses should be in page
193
     * @Then /^(.*) has (?P<count>(one|two|[\d]+)) address(?:|es)$/
194
     */
195
    public function assertAddressCount($count)
196
    {
197
        $addresses = $this->getSession()->getPage()->findAll('css', 'div.map-address-list .map-item');
198
199
        self::assertCount(
200
            $this->getCount($count),
201
            $addresses,
202
            sprintf('Expect %s addresses but found %s', $count, count($addresses))
203
        );
204
    }
205
206
    /**
207
     * Assert that given address is a primary address.
208
     * Be aware that you can't delete primary address.
209
     * Example: Then LOS ANGELES address must be primary
210
     *
211
     * @Then /^(?P<address>[^"]+) address must be primary$/
212
     */
213
    public function assertPrimaryAddress($address)
214
    {
215
        $addresses = $this->getSession()->getPage()->findAll('css', 'div.map-address-list .map-item');
216
217
        /** @var NodeElement $actualAddress */
218 View Code Duplication
        foreach ($addresses as $actualAddress) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
219
            if (false !== stripos($actualAddress->getText(), $address)) {
220
                self::assertEquals(
221
                    'Primary',
222
                    $actualAddress->find('css', 'ul.inline')->getText(),
223
                    sprintf('Address "%s" was found but it is not primary', $address)
224
                );
225
226
                return;
227
            }
228
        }
229
230
        self::fail(sprintf('Address "%s" not found', $address));
231
    }
232
233
    /**
234
     * Delete all elements in collection field
235
     * Example: And I delete all addresses
236
     *
237
     * @Given /^(?:|I )delete all (?P<field>[^"]+)$/
238
     */
239
    public function iDeleteAllAddresses($field)
240
    {
241
        $collection = $this->elementFactory->createElement('OroForm')->findField(ucfirst(Inflector::pluralize($field)));
242
243
        /** @var NodeElement $removeButton */
244
        while ($removeButton = $collection->find('css', '.removeRow')) {
245
            $removeButton->click();
246
        }
247
    }
248
249
    /**
250
     * @param int|string $count
251
     * @return int
252
     */
253 View Code Duplication
    protected function getCount($count)
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...
254
    {
255
        switch (trim($count)) {
256
            case '':
257
                return 1;
258
            case 'one':
259
                return 1;
260
            case 'two':
261
                return 2;
262
            default:
263
                return (int) $count;
264
        }
265
    }
266
}
267