1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Novaway\CommonContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Element\DocumentElement; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Context for Formstone components |
9
|
|
|
* http://formstone.it/ |
10
|
|
|
*/ |
11
|
|
|
class FormstoneContext extends BaseContext |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Choose an option in selecter field |
15
|
|
|
* |
16
|
|
|
* @When /^(?:|I )fill in selecter "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
17
|
|
|
* @When /^(?:|I )fill in selecter "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
18
|
|
|
*/ |
19
|
|
|
public function iFillInSelecter($field, $value) |
20
|
|
|
{ |
21
|
|
|
$this->selectValue('selecter', $field, $value); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Choose an option in dropdown component field |
26
|
|
|
* |
27
|
|
|
* @When /^(?:|I )fill in dropdown "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
28
|
|
|
* @When /^(?:|I )fill in dropdown "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
29
|
|
|
*/ |
30
|
|
|
public function iFillInDropdown($field, $value) |
31
|
|
|
{ |
32
|
|
|
$this->selectValue('dropdown', $field, $value); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Select value in component |
37
|
|
|
* |
38
|
|
|
* @param string $component |
39
|
|
|
* @param string $field |
40
|
|
|
* @param string $value |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
private function selectValue($component, $field, $value) |
44
|
|
|
{ |
45
|
|
|
$page = $this->getSession()->getPage(); |
46
|
|
|
|
47
|
|
|
$this->openComponent($page, $component, $field); |
48
|
|
|
$this->selectComponentValue($page, $component, $field, $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Open component choice list |
53
|
|
|
* |
54
|
|
|
* @param DocumentElement $page |
55
|
|
|
* @param string $component |
56
|
|
|
* @param string $field |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
private function openComponent(DocumentElement $page, $component, $field) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$select = $page->find('css', sprintf('#%s', $field)); |
62
|
|
|
if (!$select) { |
63
|
|
|
throw new \Exception(sprintf('No select "%s" found', $field)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$fieldName = sprintf('.fs-%1$s .fs-%1$s-selected', $component); |
67
|
|
|
$choices = $select->getParent()->find('css', $fieldName); |
68
|
|
|
if (!$choices) { |
69
|
|
|
throw new \Exception(sprintf('No field "%s" found', $field)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$choices->press(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Select value in choice list |
77
|
|
|
* |
78
|
|
|
* @param DocumentElement $page |
79
|
|
|
* @param string $component |
80
|
|
|
* @param string $field |
81
|
|
|
* @param string $value |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
private function selectComponentValue(DocumentElement $page, $component, $field, $value) |
85
|
|
|
{ |
86
|
|
|
$select = $page->find('css', sprintf('#%s', $field)); |
87
|
|
|
if (!$select) { |
88
|
|
|
throw new \Exception(sprintf('No select "%s" found', $field)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$selector = sprintf('.fs-%1$s button.fs-%1$s-item', $component); |
92
|
|
|
$choices = $page->findAll('css', $selector); |
93
|
|
|
foreach ($choices as $choice) { |
94
|
|
|
if ($choice->getText() == $value) { |
95
|
|
|
$choice->click(); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw new \Exception(sprintf('Value "%s" not found for "%s"', $value, $field)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.