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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.