Completed
Push — travis-trusty-dmore ( 8460e1...952faa )
by Kamil
22:22
created

SlugGenerationHelper::isElementReadonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service;
15
16
use Behat\Mink\Element\NodeElement;
17
use Behat\Mink\Session;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Jan Góralski <[email protected]>
22
 */
23
abstract class SlugGenerationHelper
0 ignored issues
show
Coding Style introduced by
SlugGenerationHelper does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
24
{
25
    /**
26
     * @param Session $session
27
     * @param NodeElement $element
28
     */
29
    public static function waitForSlugGeneration(Session $session, NodeElement $element)
0 ignored issues
show
Unused Code introduced by
The parameter $element 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...
30
    {
31
        Assert::true(DriverHelper::supportsJavascript($session->getDriver()), 'Browser does not support Javascript.');
32
33
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
34
35
        usleep(500000); // TODO: Remove hardcoded sleep from tests
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
36
    }
37
38
    /**
39
     * @param Session $session
40
     * @param NodeElement $element
41
     */
42
    public static function enableSlugModification(Session $session, NodeElement $element)
43
    {
44
        Assert::true(DriverHelper::supportsJavascript($session->getDriver()), 'Browser does not support Javascript.');
45
46
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
47
        static::waitForElementToBeClickable($session, $element);
0 ignored issues
show
Bug introduced by
Since waitForElementToBeClickable() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of waitForElementToBeClickable() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
48
49
        $element->click();
50
51
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
52
    }
53
54
    /**
55
     * @param Session $session
56
     * @param NodeElement $element
57
     *
58
     * @return bool
59
     */
60
    public static function isSlugReadonly(Session $session, NodeElement $element)
61
    {
62
        if (DriverHelper::supportsJavascript($session->getDriver())) {
63
            JQueryHelper::waitForAsynchronousActionsToFinish($session);
64
        }
65
66
        return $element->hasAttribute('readonly');
67
    }
68
69
    /**
70
     * @param Session $session
71
     * @param NodeElement $element
72
     */
73
    private static function waitForElementToBeClickable(Session $session, NodeElement $element)
74
    {
75
        $session->wait(5000, sprintf(
76
            'false === $(document.evaluate(%s, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).hasClass("loading")',
77
            json_encode($element->getParent()->getParent()->getXpath())
78
        ));
79
    }
80
81
}
82