BaseContext   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 22

11 Methods

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