Passed
Push — master ( 656f92...afef6b )
by Marc
02:43
created

BaseContext::getOuterHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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