Passed
Push — master ( afef6b...b42680 )
by Marc
03:21
created

BaseContext::toRelativeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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