Completed
Pull Request — 3.1 (#348)
by Piotr
06:19 queued 04:50
created

FormContext   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 174
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iChangeFormFieldWithValue() 0 5 1
A iShouldSeeFormWithFollowingFields() 0 7 2
A iPressFormButton() 0 4 1
A iFillFormFields() 0 15 4
A transformToNoneditableCollection() 0 4 1
A transformToCollection() 0 4 1
A collectionShouldHaveElements() 0 5 1
A collectionShouldHaveButton() 0 4 1
A allCollectionButtonsDisabled() 0 13 3
A collectionAddButtonIsDisabled() 0 8 2
A collectionRemoveButtonsAreEnabled() 0 8 2
A iPressInCollection() 0 6 1
A iFillWithInCollectionAtPosition() 0 5 1
A iRemoveElementInCollection() 0 5 1
A getFormElement() 0 4 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Behat\Context;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Behat\Mink\Element\NodeElement;
16
use FSi\Bundle\AdminBundle\Behat\Element\Form;
17
use FSi\Bundle\AdminBundle\Behat\Page\DefaultPage;
18
19
class FormContext extends AbstractContext
20
{
21
    /**
22
     * @var DefaultPage
23
     */
24
    private $defaultPage;
25
26
    public function __construct(DefaultPage $defaultPage)
27
    {
28
        $this->defaultPage = $defaultPage;
29
    }
30
31
    /**
32
     * @When I change form field :field to value :value
33
     */
34
    public function iChangeFormFieldWithValue($field, $value)
35
    {
36
        expect($this->getFormElement()->findField($field)->getValue())->toNotBe($value);
37
        $this->getFormElement()->fillField($field, $value);
38
    }
39
40
    /**
41
     * @Given /^I should see form with following fields$/
42
     */
43
    public function iShouldSeeFormWithFollowingFields(TableNode $table)
44
    {
45
        $form = $this->getFormElement();
46
        foreach($table->getHash() as $fieldRow) {
47
            expect($form->hasField($fieldRow['Field name']))->toBe(true);
48
        }
49
    }
50
51
    /**
52
     * @Given /^I press form "([^"]*)" button$/
53
     */
54
    public function iPressFormButton($button)
55
    {
56
        $this->getFormElement()->pressButton($button);
57
    }
58
59
    /**
60
     * @Given I fill the form with values:
61
     */
62
    public function iFillFormFields(TableNode $table)
63
    {
64
        $form = $this->getFormElement();
65
        foreach($table->getHash() as $fieldRow) {
66
            $fieldName = $fieldRow['Field name'];
67
            $fieldValue = $fieldRow['Field value'];
68
            expect($form->hasField($fieldName))->toBe(true);
69
            $field = $form->findField($fieldName);
70
            if ($field->getAttribute('type') === 'checkbox') {
71
                $this->parseScenarioValue($fieldValue) ? $field->check() : $field->uncheck();
72
            } else {
73
                $field->setValue($fieldValue);
74
            }
75
        }
76
    }
77
78
    /**
79
     * @Transform /"([^"]*)" non-editable collection/
80
     * @Transform /non-editable collection "([^"]*)"/
81
     * @Transform /removable-only collection "([^"]*)"/
82
     */
83
    public function transformToNoneditableCollection($collectionNames)
84
    {
85
        return $this->defaultPage->getNonEditableCollection($collectionNames);
86
    }
87
88
    /**
89
     * @Transform /^"([^"]*)" collection/
90
     * @Transform /collection "([^"]*)"/
91
     */
92
    public function transformToCollection($collectionNames)
93
    {
94
        return $this->defaultPage->getCollection($collectionNames);
95
    }
96
97
    /**
98
     * @Given /^("[^"]*" collection) has (\d+) (element|elements)$/
99
     * @Then /^("[^"]*" collection) should have (\d+) (element|elements)$/
100
     * @Given /^(non-editable collection "[^"]*") has (\d+) (element|elements)$/
101
     * @Then /^(non-editable collection "[^"]*") should have (\d+) (element|elements)$/
102
     * @Then /^(removable-only collection "[^"]*") should have (\d+) (element|elements)$/
103
     */
104
    public function collectionShouldHaveElements(NodeElement $collection, $elementsCount)
105
    {
106
        $elements = $collection->findAll('xpath', '/*/*[@class = "form-group"]');
107
        expect(count($elements))->toBe($elementsCount);
108
    }
109
110
    /**
111
     * @Given /^(collection "[^"]*") should have "([^"]*)" button$/
112
     */
113
    public function collectionShouldHaveButton(NodeElement $collection, $buttonName)
114
    {
115
        expect($collection->findButton($buttonName))->toNotBeNull();
116
    }
117
118
    /**
119
     * @Then /^all buttons for adding and removing items in (non-editable collection "[^"]*") should be disabled$/
120
     */
121
    public function allCollectionButtonsDisabled(NodeElement $collection)
122
    {
123
        $removeButtons = $collection->findAll('css', '.collection-remove');
124
        expect(count($removeButtons))->notToBe(0);
125
        foreach ($removeButtons as $removeButton) {
126
            expect($removeButton->hasClass('disabled'))->toBe(true);
127
        }
128
        $addButtons = $collection->findAll('css', '.collection-add');
129
        expect(count($addButtons))->notToBe(0);
130
        foreach ($addButtons as $addButton) {
131
            expect($addButton->hasClass('disabled'))->toBe(true);
132
        }
133
    }
134
135
    /**
136
     * @Then /^button for adding item in (removable-only collection "[^"]*") should be disabled$/
137
     */
138
    public function collectionAddButtonIsDisabled(NodeElement $collection)
139
    {
140
        $addButtons = $collection->findAll('css', '.collection-add');
141
        expect(count($addButtons))->notToBe(0);
142
        foreach ($addButtons as $addButton) {
143
            expect($addButton->hasClass('disabled'))->toBe(true);
144
        }
145
    }
146
147
    /**
148
     * @Then /^buttons for removing items in (removable-only collection "[^"]*") should be enabled/
149
     */
150
    public function collectionRemoveButtonsAreEnabled(NodeElement $collection)
151
    {
152
        $addButtons = $collection->findAll('css', '.collection-remove');
153
        expect(count($addButtons))->notToBe(0);
154
        foreach ($addButtons as $addButton) {
155
            expect($addButton->hasClass('disabled'))->toBe(false);
156
        }
157
    }
158
159
    /**
160
     * @When /^I press "([^"]*)" in (collection "[^"]*")$/
161
     */
162
    public function iPressInCollection($buttonName, NodeElement $collection)
163
    {
164
        $collection
165
            ->find('xpath', '/*[contains(concat(" ",normalize-space(@class)," ")," collection-add ")]')
166
            ->press();
167
    }
168
169
    /**
170
     * @Given /^I fill "([^"]*)" with "([^"]*)" in (collection "[^"]*") at position (\d+)$/
171
     */
172
    public function iFillWithInCollectionAtPosition($fieldName, $fieldValue, NodeElement $collection, $position)
173
    {
174
        $collectionRow = $collection->find('xpath', sprintf('/*/*[@class = "form-group"][%d]', $position));
175
        $collectionRow->fillField($fieldName, $fieldValue);
176
    }
177
178
    /**
179
     * @Given /^I remove (\w+) element in (collection "[^"]*")$/
180
     * @Given /^I remove (\w+) element in (removable-only collection "[^"]*")$/
181
     */
182
    public function iRemoveElementInCollection($index, NodeElement $collection)
183
    {
184
        $collection->find('xpath', sprintf('/*/*[@class = "form-group"][%d]', $index))
185
             ->find('css', '.collection-remove')->click();
186
    }
187
188
    private function getFormElement(): Form
189
    {
190
        return $this->defaultPage->getElement('Form');
191
    }
192
}
193