Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

VictoireContext::ivisitHomepageThroughDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Tests\Features\Context;
4
5
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
6
use Behat\Mink\Element\Element;
7
use Behat\Symfony2Extension\Context\KernelDictionary;
8
use Knp\FriendlyContexts\Context\MinkContext;
9
use Knp\FriendlyContexts\Context\RawMinkContext;
10
11
/**
12
 * This class gives some usefull methods for Victoire navigation.
13
 *
14
 * @property MinkContext minkContext
15
 */
16
class VictoireContext extends RawMinkContext
17
{
18
    use KernelDictionary;
19
    protected $minkContext;
20
21
    /** @BeforeScenario */
22
    public function gatherContexts(BeforeScenarioScope $scope)
23
    {
24
        $environment = $scope->getEnvironment();
25
        $this->minkContext = $environment->getContext('Knp\FriendlyContexts\Context\MinkContext');
26
    }
27
28
    /**
29
     * @BeforeScenario
30
     *
31
     * @param BeforeScenarioScope $scope
32
     */
33
    public function resetViewsReference(BeforeScenarioScope $scope)
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        $viewsReferences = $this->getContainer()->get('victoire_core.view_helper')->buildViewsReferences();
36
        $this->getContainer()->get('victoire_view_reference.manager')->saveReferences($viewsReferences);
37
    }
38
39
    /**
40
     * @Given I am logged in as :email
41
     */
42
    public function iAmLoggedInAsUser($email)
43
    {
44
        $this->minkContext->visit('/login');
45
        $this->minkContext->fillField('username', $email);
46
        $this->minkContext->fillField('password', 'test');
47
        $this->minkContext->pressButton('_submit');
48
    }
49
50
    /**
51
     * @Given I login as visitor
52
     */
53
    public function iLoginAsVisitor()
54
    {
55
        $this->getSession()->getDriver()->stop();
56
        $url = 'http://[email protected]:[email protected]:8000';
57
        $this->minkContext->setMinkParameter('base_url', $url);
58
    }
59
60
    /**
61
     * @Given /^I visit homepage through domain "([^"]*)"$/
62
     */
63
    public function ivisitHomepageThroughDomain($domain)
64
    {
65
        $this->getSession()->getDriver()->stop();
66
        $url = sprintf('http://[email protected]:test@%s:8000/app_domain.php', $domain);
67
        $this->minkContext->setMinkParameter('base_url', $url);
68
        $this->minkContext->visitPath('/');
69
    }
70
71
    /**
72
     * @Then /^I fill in wysiwyg with "([^"]*)"$/
73
     */
74
    public function iFillInWysiwygOnFieldWith($arg)
75
    {
76
        $js = 'CKEDITOR.instances.victoire_widget_form_ckeditor_content.setData("'.$arg.'");';
77
        $this->getSession()->executeScript($js);
78
    }
79
80
    /**
81
     * @Then /^I select "([^"]*)" from the "([^"]*)" select of "([^"]*)" slot$/
82
     */
83
    public function iSelectFromTheSelectOfSlot($widget, $nth, $slot)
84
    {
85
        $slot = $this->getSession()->getPage()->find('xpath', 'descendant-or-self::*[@id="vic-slot-'.$slot.'"]');
86
        $selects = $slot->findAll('css', 'select[role="menu"]');
87
        $selects[$nth - 1]->selectOption(str_replace('\\"', '"', $widget));
88
    }
89
90
    /**
91
     * @Then /^I switch to "([^"]*)" mode$/
92
     */
93 View Code Duplication
    public function iSwitchToMode($mode)
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...
94
    {
95
        $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', 'descendant-or-self::*[@data-mode="admin-'.$mode.'"]');
96
97
        if (null === $element) {
98
            $message = sprintf('Element not found in the page after 10 seconds"');
99
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
100
        }
101
        $element->click();
102
    }
103
104
    /**
105
     * @Then /^I (open|close|toggle) the hamburger menu$/
106
     */
107 View Code Duplication
    public function iOpenTheHamburgerMenu()
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...
108
    {
109
        $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', 'descendant-or-self::*[@id="vic-menu-leftnavbar-trigger"]');
110
111
        if (null === $element) {
112
            $message = sprintf('Element not found in the page after 10 seconds"');
113
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
114
        }
115
        $element->click();
116
    }
117
118
    /**
119
     * @When I follow the tab :name
120
     */
121 View Code Duplication
    public function iFollowTheTab($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...
122
    {
123
        $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::a[@data-toggle="vic-tab" and normalize-space(text()) = "%s"]', $name));
124
125
        if (null === $element) {
126
            $message = sprintf('Element not found in the page after 10 seconds"');
127
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
128
        }
129
        $element->click();
130
    }
131
132
    /**
133
     * @Then /^I submit the widget$/
134
     * @Then /^I submit the modal$/
135
     */
136
    public function iSubmitTheWidget()
137
    {
138
        $element = $this->getSession()->getPage()->find('xpath', 'descendant-or-self::a[@data-modal="create"]');
139
140
        if (!$element) {
141
            $element = $this->getSession()->getPage()->find('xpath', 'descendant-or-self::a[@data-modal="update"]');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $element is correct as $this->getSession()->get...@data-modal="update"]') (which targets Behat\Mink\Element\Element::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
142
        }
143
        $element->click();
144
        $this->getSession()->wait(2000);
145
    }
146
147
    /**
148
     * @Given /^I edit an "([^"]*)" widget$/
149
     * @Given /^I edit the "([^"]*)" widget$/
150
     */
151
    public function iEditTheWidget($widgetType)
152
    {
153
        $selector = sprintf('.vic-widget-%s > a.vic-hover-widget', strtolower($widgetType));
154
        $session = $this->getSession(); // get the mink session
155
        $element = $this->findOrRetry($session->getPage(), 'css', $selector);
156
157
        // errors must not pass silently
158
        if (null === $element) {
159
            throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $selector));
160
        }
161
162
        // ok, let's hover it
163
        $element->mouseOver();
164
        $element->click();
165
    }
166
167
    /**
168
     * @Then /^"([^"]*)" should precede "([^"]*)"$/
169
     */
170
    public function shouldPrecedeForTheQuery($textBefore, $textAfter)
171
    {
172
        $element = $this->getSession()->getPage()->find(
173
            'xpath',
174
            sprintf('//*[normalize-space(text()) = "%s"][preceding::*[normalize-space(text()) = "%s"]]',
175
                $textAfter,
176
                $textBefore
177
            )
178
        );
179
        if (null === $element) {
180
            $message = sprintf('"%s" does not preceed "%s"', $textBefore, $textAfter);
181
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
182
        }
183
    }
184
185
    /**
186
     * @When /^I select the option "(?P<option>[^"]*)" in the dropdown "(?P<dropdown>[^"]*)"$/
187
     */
188
    public function iSelectTheOptionInTheDropdown($option, $dropdown)
189
    {
190
        $link = $this->getSession()->getPage()->find('css', sprintf('a.vic-dropdown-toggle[title="%s"]', $dropdown));
191
        $link->click();
192
        $optionButton = $this->getSession()->getPage()->find('css', sprintf('ul[aria-labelledby="%sDropdownMenu"] > li > a[title="%s"]', $dropdown, $option));
193
        $optionButton->click();
194
    }
195
196
    /**
197
     * @Then /^I attach image with id "(\d+)" to victoire field "(.+)"$/
198
     */
199
    public function attachImageToVictoireScript($imageId, $fieldId)
200
    {
201
        $script = sprintf('$("#%s input").val(%d)', $fieldId, $imageId);
202
        $this->getSession()->executeScript($script);
203
    }
204
205
    /**
206
     * @Then I should find css element :element with selector :selector and value :value
207
     */
208
    public function iShouldFindCssWithSelectorAndValue($element, $selector, $value)
209
    {
210
        $css = sprintf('%s[%s="%s"]', $element, $selector, $value);
211
        $session = $this->getSession();
212
        $element = $this->findOrRetry($session->getPage(), 'css', $css);
213
214
        if (null === $element) {
215
            $message = sprintf('Element not found. String generate: %s[%s="%s"]', $element, $selector, $value);
216
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
217
        }
218
    }
219
220
    /**
221
     * Try to find value in element and retry for a given time.
222
     *
223
     * @param Element $element
224
     * @param string  $selectorType xpath|css
225
     * @param string  $value
226
     * @param int     $timeout
227
     */
228
    protected function findOrRetry(Element $element, $selectorType, $value, $timeout = 10000)
229
    {
230
        if ($timeout <= 0) {
231
            return;
232
        }
233
234
        $item = $element->find($selectorType, $value);
235
236
        if ($item) {
237
            return $item;
238
        } else {
239
            $this->getSession()->wait(100);
240
241
            return $this->findOrRetry($element, $selectorType, $value, $timeout - 100);
242
        }
243
    }
244
245
    /**
246
     * @Then I should see disable tab :name
247
     */
248 View Code Duplication
    public function iShouldSeeDisableTab($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...
249
    {
250
        $element = $this->findOrRetry($this->getSession()->getPage(), 'xpath', sprintf('descendant-or-self::li[@class="vic-disable" and normalize-space(.) = "%s"]', $name));
251
252
        if (null === $element) {
253
            $message = sprintf('Element not found in the page after 10 seconds"');
254
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
255
        }
256
    }
257
258
    /**
259
     * @Then /^I move the widgetMap "(.+)" "(.+)" the widgetMap "(.*)"$/
260
     */
261
    public function iMoveWidgetUnder($widgetMapMoved, $position, $widgetMapMovedTo)
262
    {
263
        if (!$widgetMapMovedTo) {
264
            $widgetMapMovedTo = 'null';
265
        }
266
        $js = 'updateWidgetPosition({"parentWidgetMap": '.$widgetMapMovedTo.', "slot": "main_content", "position": "'.$position.'", "widgetMap": '.$widgetMapMoved.'})';
267
268
        $this->getSession()->executeScript($js);
269
    }
270
}
271