Completed
Push — master ( 9e29b7...c3d8ab )
by Yaroslav
19:31
created

MinkContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkOption() 0 23 5
B fillField() 0 23 6
A scrollIntoView() 0 7 1
1
<?php
2
3
use Behat\Mink\Element\NodeElement;
4
use Behat\MinkExtension\Context\MinkContext as BaseMinkContext;
5
6
class MinkContext extends BaseMinkContext
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * Updated version of MinkContext::checkOption which correctly deal with iCheck fields.
10
     *
11
     * {@inheritdoc}
12
     */
13
    public function checkOption($option)
14
    {
15
        $option = $this->fixStepArgument($option);
16
        $element = $this->getSession()->getPage()->findField($option);
17
18
        if (!$element) {
19
            throw new \InvalidArgumentException(sprintf("Field '%s' not found", $option));
20
        }
21
22
        $parent = $element->getParent();
23
24
        if ($parent && $parent->hasClass('icheckbox_square-blue')) {
25
            $iCheckElement = $parent->find('css', '.iCheck-helper');
26
27
            if (!$iCheckElement) {
28
                throw new \InvalidArgumentException(sprintf("iCheck element '.iCheck-helper' not found for '%s'", $option));
29
            }
30
31
            $iCheckElement->click();
32
        } else {
33
            $element->check();
34
        }
35
    }
36
37
    /**
38
     * Updated version of MinkContext::fillField which correctly deal with CKEDITOR fields.
39
     *
40
     * {@inheritdoc}
41
     */
42
    public function fillField($field, $value)
43
    {
44
        $field = $this->fixStepArgument($field);
45
        $value = $this->fixStepArgument($value);
46
        /** @var NodeElement[] $elements */
47
        $element = $this->getSession()->getPage()->find('named', ['field', $field]);
48
49
        if (!$element) {
50
            throw new \InvalidArgumentException(sprintf("Field '%s' not found", $field));
51
        }
52
53
        if ($element->getTagName() === 'textarea' && $element->hasClass('ckeditor')) {
54
            $fieldId = $element->getAttribute('id');
55
56
            if ($fieldId === null || $fieldId === '') {
57
                throw new \LogicException(sprintf("Unable to fill in CKEDITOR field '%s' as it's id attribute is missing.", $field));
58
            }
59
60
            $this->getSession()->executeScript(sprintf('CKEDITOR && CKEDITOR.instances["%s"].setData("%s");', $fieldId, $value));
61
        } else {
62
            $element->setValue($value);
63
        }
64
    }
65
66
    /**
67
     * @When I scroll to ":locator"
68
     * @param string $locator
69
     */
70
    public function scrollIntoView($locator)
71
    {
72
        $element = $this->getSession()->getPage()->find('named', ['field', $locator]);
73
        $xpath = str_replace("\n", '', $element->getXpath());
74
        $function = "document.evaluate(\"{$xpath}\", document).iterateNext().scrollIntoView();";
75
        $this->getSession()->executeScript($function);
76
    }
77
}
78