FeatureContext::iWaitSeconds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Tests\Features\Context;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Behat\Context\SnippetAcceptingContext;
7
use Behat\Behat\Hook\Scope\AfterStepScope;
8
use Behat\Mink\Driver\Selenium2Driver;
9
use Behat\Mink\Element\Element;
10
use Behat\Mink\Exception\UnsupportedDriverActionException;
11
use Behat\Symfony2Extension\Context\KernelAwareContext;
12
use Behat\Symfony2Extension\Context\KernelDictionary;
13
use Behat\Symfony2Extension\Driver\KernelDriver;
14
use Knp\FriendlyContexts\Context\RawMinkContext;
15
16
/**
17
 * Feature context.
18
 */
19
class FeatureContext extends RawMinkContext implements Context, SnippetAcceptingContext, KernelAwareContext
0 ignored issues
show
Deprecated Code introduced by
The interface Behat\Behat\Context\SnippetAcceptingContext has been deprecated with message: will be removed in 4.0. Use --snippets-for CLI option instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    use KernelDictionary;
22
23
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$nbr" missing
Loading history...
24
     * @Given /^I wait (\d+) second$/
25
     * @Given /^I wait (\d+) seconds$/
26
     */
27
    public function iWaitSeconds($nbr)
28
    {
29
        $this->getSession()->wait($nbr * 1000);
30
    }
31
32
    public function getSymfonyProfile()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
33
    {
34
        $driver = $this->getSession()->getDriver();
35
        if (!$driver instanceof KernelDriver) {
36
            throw new UnsupportedDriverActionException(
37
                'You need to tag the scenario with '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
38
                '"@mink:symfony2". Using the profiler is not '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
39
                'supported by %s', $driver
40
            );
41
        }
42
43
        $profile = $driver->getClient()->getProfile();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\Client as the method getProfile() does only exist in the following sub-classes of Symfony\Component\BrowserKit\Client: Symfony\Bundle\FrameworkBundle\Client. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
44
        if (false === $profile) {
45
            throw new \RuntimeException(
46
                'The profiler is disabled. Activate it by setting '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
47
                'framework.profiler.only_exceptions to false in '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
48
                'your config'
49
            );
50
        }
51
52
        return $profile;
53
    }
54
55
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$elementId" missing
Loading history...
56
     * @Then /^I should see the css property "(.+)" of "(.+)" with "(.+)"$/
57
     *
58
     * @param string $property
59
     * @param string $value
0 ignored issues
show
introduced by
Doc comment for parameter $value does not match actual variable name $elementId
Loading history...
60
     */
61
    public function iShouldSeeCssOfWith($property, $elementId, $value)
62
    {
63
        $script = "return $('#".$elementId."').css('".$property."') === '".$value."';";
64
        $evaluated = $this->getSession()->evaluateScript($script);
65
        if (!$evaluated) {
66
            throw new \RuntimeException('The element with id "'.$elementId.'" and css property "'.$property.': '.$value.';" not found.');
67
        }
68
    }
69
70
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
introduced by
Doc comment for parameter "$url" missing
Loading history...
71
     * @Then I should see background-image of :id with relative url :url
72
     */
73
    public function iShouldSeeBackgroundImageWithRelativeUrl($id, $url)
74
    {
75
        $session = $this->getSession();
76
        $base_url = $session->getCurrentUrl();
0 ignored issues
show
Coding Style introduced by
Variable "base_url" is not in valid camel caps format
Loading history...
77
        $parse_url = parse_url($base_url);
0 ignored issues
show
Coding Style introduced by
Variable "parse_url" is not in valid camel caps format
Loading history...
Coding Style introduced by
Variable "base_url" is not in valid camel caps format
Loading history...
78
        $base_url = rtrim($base_url, $parse_url['path']);
0 ignored issues
show
Coding Style introduced by
Variable "base_url" is not in valid camel caps format
Loading history...
Coding Style introduced by
Variable "parse_url" is not in valid camel caps format
Loading history...
79
        $url = rtrim($base_url, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.ltrim($url, DIRECTORY_SEPARATOR);
0 ignored issues
show
Coding Style introduced by
Variable "base_url" is not in valid camel caps format
Loading history...
80
        $this->iShouldSeeCssOfWith('background-image', $id, 'url("'.$url.'")');
81
    }
82
83
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$title" missing
Loading history...
84
     * @Then the title should be :title
85
     */
86
    public function theTitleShouldBe($title)
87
    {
88
        $element = $this->getSession()->getPage()->find(
89
            'xpath',
90
            sprintf('//title[normalize-space(text()) = "%s"]', $title)
91
        );
92
93
        if (null === $element) {
94
            $message = sprintf('"%s" is not the title of the page', $title);
95
            throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
96
        }
97
    }
98
99
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$event" missing
Loading history...
100
     * @AfterStep
101
     */
102
    public function printLastResponseOnError(AfterStepScope $event)
103
    {
104
        if (!$event->getTestResult()->isPassed()) {
105
            $this->saveDebugScreenshot();
106
        }
107
    }
108
109
    /**
110
     * @Then /^save screenshot$/
111
     */
112
    public function saveDebugScreenshot()
113
    {
114
        $driver = $this->getSession()->getDriver();
115
116
        if (!$driver instanceof Selenium2Driver) {
117
            return;
118
        }
119
120
        if (!getenv('BEHAT_SCREENSHOTS')) {
121
            return;
122
        }
123
124
        $filename = microtime(true).'.png';
125
        $path = $this->getContainer()
126
                ->getParameter('kernel.root_dir').'/../behat_screenshots';
127
128
        if (!file_exists($path)) {
129
            mkdir($path);
130
        }
131
132
        $this->saveScreenshot($filename, $path);
0 ignored issues
show
Bug introduced by
The method saveScreenshot() does not seem to exist on object<Victoire\Tests\Fe...Context\FeatureContext>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
    }
134
}
135