Passed
Pull Request — master (#284)
by
unknown
02:17
created

FeatureContext::getSpecificBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SilverStripe\VersionedAdmin\Tests\Behat\Context;
4
5
use Behat\Mink\Element\NodeElement;
0 ignored issues
show
Bug introduced by
The type Behat\Mink\Element\NodeElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\BehatExtension\Context\SilverStripeContext;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtens...ext\SilverStripeContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
if (!class_exists(SilverStripeContext::class)) {
9
    return;
10
}
11
12
class FeatureContext extends SilverStripeContext
13
{
14
    /**
15
     * @Then I should see a list of blocks
16
     */
17
    public function iShouldSeeAListOfBlocks()
18
    {
19
        $this->getBlocks();
20
    }
21
22
    /**
23
     * Example: I should see "font-icon-block-file" as the "font icon" for block 2
24
     * Example: I should see "My File Block" as the "title" for block 1
25
     *
26
     * @Then /^I should (not |)see :text as the :selector font icon for block :position/
27
     * @param string $text
28
     * @param string $position
29
     */
30
    public function iShouldSeeAsTheFontIcon($text, $position)
31
    {
32
        $block = $this->getSpecificBlock($position);
0 ignored issues
show
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
Bug introduced by
$position of type string is incompatible with the type integer expected by parameter $position of SilverStripe\VersionedAd...ext::getSpecificBlock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $block = $this->getSpecificBlock(/** @scrutinizer ignore-type */ $position);
Loading history...
33
34
        switch ($text) {
35
            case 'font icon': {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
36
                $fontIcon = $version->find('css', '.element-editor-header__icon-container .i');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $version seems to be never defined.
Loading history...
37
                assertTrue($fontIcon->hasClass($text));
38
            }
39
            case 'title': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
40
                $title = $version->find('css', '.element-editor-header__title');
41
                assertTrue($title->hasClass($text));
42
            }
43
        }
44
    }
45
46
47
    /**
48
     * Example: I should see "font-icon-block-layout" as the title in block 2
49
     *
50
     * @Then /^I should (not |)see :text as the font icon in block :position/
51
     * @param string $text
52
     * @param string $position
53
     */
54
    public function iShouldSeeAsTheFontIcon($text, $position)
55
    {
56
        $block = $this->getSpecificBlock($position);
0 ignored issues
show
Bug introduced by
$position of type string is incompatible with the type integer expected by parameter $position of SilverStripe\VersionedAd...ext::getSpecificBlock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $block = $this->getSpecificBlock(/** @scrutinizer ignore-type */ $position);
Loading history...
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
57
        $fontIcon = $version->find('css', '.element-editor-header__icon-container .i');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $version seems to be never defined.
Loading history...
58
        assertTrue($fontIcon->hasClass($text));
59
    }
60
61
    /**
62
     * Returns the blocks from the element editor
63
     *
64
     * @param string $modifier Optional CSS selector modifier
65
     * @return NodeElement[]
66
     */
67
    protected function getBlocks($modifier = '')
68
    {
69
        // Wait for the list to be visible
70
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
71
72
        $versions = $this->getSession()
73
            ->getPage()
74
            ->findAll('css', '.elemental-editor__list .elemental-editor__element' . $modifier);
75
        return $versions;
76
    }
77
78
    /**
79
     * Returns the selected element
80
     *
81
     * @param int $position
82
     * @return NodeElement
83
     */
84
    protected function getSpecificBlock($position)
85
    {
86
        $blocks = $this->getBlocks();
87
            /** @var NodeElement $block */
88
            if ($blocks[$position] !== false) {
0 ignored issues
show
introduced by
The condition $blocks[$position] !== false is always true.
Loading history...
89
                return $blocks[$position];
90
            }
91
    }
92
}
93
94