Passed
Push — feature/tests ( 9b7685...30969f )
by Marc
02:06
created

BaseContext::doesNotSupportDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MOrtola\BehatSEOContexts\Context;
4
5
use Behat\Mink\Exception\DriverException;
6
use Behat\Mink\Exception\UnsupportedDriverActionException;
7
use Behat\MinkExtension\Context\RawMinkContext;
8
use Behat\Symfony2Extension\Driver\KernelDriver;
9
use PHPUnit\Framework\ExpectationFailedException;
10
11
class BaseContext extends RawMinkContext
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $webUrl;
17
18
    /**
19
     * @BeforeScenario
20
     */
21
    public function setupWebUrl()
22
    {
23
        $this->webUrl = $this->getMinkParameter('base_url');
24
    }
25
26
    /**
27
     * @throws DriverException
28
     */
29
    protected function visit(string $url)
30
    {
31
        $driver = $this->getSession()->getDriver();
32
33
        if ($driver instanceof KernelDriver) {
34
            $driver->getClient()->request('GET', $url);
35
        } else {
36
            $driver->visit($url);
37
        }
38
    }
39
40
    protected function getStatusCode(): int
41
    {
42
        return $this->getSession()->getStatusCode();
43
    }
44
45
    /**
46
     * @throws \Exception
47
     */
48
    protected function spin(callable $closure, int $seconds = 5): bool
49
    {
50
        $i = 1;
51
        while ($i++ <= $seconds * 4) {
52
            if ($closure($this)) {
53
                return true;
54
            }
55
            $this->getSession()->wait(1000 / 4);
56
        }
57
        $backtrace = debug_backtrace();
58
59
        throw new \Exception(
60
            sprintf(
61
                "Timeout thrown by %s::%s()\n%s, line %s",
62
                $backtrace[0]['class'],
63
                $backtrace[0]['function'],
64
                $backtrace[0]['file'],
65
                $backtrace[0]['line']
66
            )
67
        );
68
    }
69
70
    protected function toAbsoluteUrl(string $url): string
71
    {
72
        if (false === strpos($url, '://')) {
73
            $url = sprintf('%s%s', $this->webUrl, $url);
74
        }
75
76
        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
77
            throw new \InvalidArgumentException(
78
                sprintf('%s is not a valid URL', $url)
79
            );
80
        }
81
82
        return $url;
83
    }
84
85
    protected function getCurrentUrl(bool $relative = false): string
86
    {
87
        if ($relative) {
88
            $this->toRelativeUrl($this->getSession()->getCurrentUrl());
89
        }
90
91
        return $this->getSession()->getCurrentUrl();
92
    }
93
94
    protected function toRelativeUrl(string $url): string
95
    {
96
        return parse_url($url, PHP_URL_PATH);
97
    }
98
99
    /**
100
     * @throws UnsupportedDriverActionException
101
     */
102
    protected function supportsDriver(string $driverClass)
103
    {
104
        if (!is_a($this->getSession()->getDriver(), $driverClass)) {
105
            throw new UnsupportedDriverActionException(
106
                sprintf('This step is only supported by the %s driver', $driverClass),
107
                $this->getSession()->getDriver()
108
            );
109
        }
110
    }
111
112
    /**
113
     * @throws UnsupportedDriverActionException
114
     */
115
    protected function doesNotSupportDriver(string $driverClass)
116
    {
117
        if (is_a($this->getSession()->getDriver(), $driverClass)) {
118
            throw new UnsupportedDriverActionException(
119
                sprintf('This step is not supported by the %s driver', $driverClass),
120
                $this->getSession()->getDriver()
121
            );
122
        }
123
    }
124
125
    protected function assertInverse(callable $callableStepDefinition, string $exceptionMessage = '')
126
    {
127
        try {
128
            $callableStepDefinition();
129
        } catch (ExpectationFailedException $e) {
130
            return;
131
        }
132
133
        throw new ExpectationFailedException($exceptionMessage);
134
    }
135
}
136