Passed
Pull Request — master (#13)
by Robbie
03:22
created

FeatureContext::getVersions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 9.6666
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 versions
16
     */
17
    public function iShouldSeeAListOfVersions()
18
    {
19
        $page = $this->getSession()->getPage();
20
        $versionsTable = $page->find('css', '.history-viewer .table');
21
        assertNotNull($versionsTable, 'I should see a list of versions');
22
    }
23
24
    /**
25
     * @When I click on the first version
26
     */
27
    public function iClickOnTheFirstVersion()
28
    {
29
        $versions = $this->getVersions();
30
        $this->clickVersion(current($versions));
31
    }
32
33
    /**
34
     * @When I click on version :versionNo
35
     */
36
    public function iClickOnVersion($versionNo)
37
    {
38
        $versions = $this->getVersions(' td:first-child');
39
        $desiredVersion = null;
40
        foreach ($versions as $version) {
41
            /** @var NodeElement $version */
42
            if ($version->getText() == $versionNo) {
43
                $desiredVersion = $version;
44
                break;
45
            }
46
        }
47
        assertNotNull($desiredVersion, 'Desired version ' . $versionNo . ' was not found in the page.');
48
        $this->clickVersion($desiredVersion);
0 ignored issues
show
Bug introduced by
It seems like $desiredVersion can also be of type null; however, parameter $version of SilverStripe\VersionedAd...Context::clickVersion() does only seem to accept Behat\Mink\Element\NodeElement, maybe add an additional type check? ( Ignorable by Annotation )

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

48
        $this->clickVersion(/** @scrutinizer ignore-type */ $desiredVersion);
Loading history...
49
    }
50
51
    /**
52
     * Click on the given version
53
     *
54
     * @param NodeElement $version
55
     */
56
    protected function clickVersion(NodeElement $version)
57
    {
58
        $version->click();
59
60
        // Wait for the form builder to load
61
        $this->getSession()->wait(3000, 'window.jQuery("#Form_versionForm").length > 0');
62
    }
63
64
    /**
65
     * Returns the versions from the history viewer list (table rows)
66
     *
67
     * @param string $modifier Optional CSS selector modifier
68
     * @return NodeElement[]
69
     */
70
    protected function getVersions($modifier = '')
71
    {
72
        $versions = $this
73
            ->getSession()
74
            ->getPage()
75
            ->findAll('css', '.history-viewer .table tbody tr' . $modifier);
76
77
        assertNotEmpty($versions, 'I see a list of versions');
78
        return $versions;
79
    }
80
}
81