Passed
Pull Request — master (#14)
by
unknown
02:56
created

HTMLContext::getFirstLineOfPageContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace MOrtola\BehatSEOContexts\Context;
4
5
use Behat\Behat\Tester\Exception\PendingException;
6
use HtmlValidator\Exception\ServerException;
7
use HtmlValidator\Exception\UnknownParserException;
8
use HtmlValidator\Validator;
9
use InvalidArgumentException;
10
use Webmozart\Assert\Assert;
11
12
class HTMLContext extends BaseContext
13
{
14
    const VALIDATION_SERVICES = [
15
        Validator::DEFAULT_VALIDATOR_URL,
16
        'https://validator.nu/',
17
        'https://validator.w3.org/nu/',
18
    ];
19
    private const HTML5_DOCTYPE_MARKUP = '<!doctype html>';
20
21
    /**
22
     * @Then the page HTML markup should be valid
23
     */
24
    public function thePageHtmlMarkupShouldBeValid(): void
25
    {
26
        $validated        = false;
27
        $validationErrors = [];
28
29
        foreach (self::VALIDATION_SERVICES as $validatorService) {
30
            try {
31
                $validator        = new Validator($validatorService);
32
                $validatorResult  = $validator->validateDocument($this->getSession()->getPage()->getContent());
33
                $validated        = true;
34
                $validationErrors = $validatorResult->getErrors();
35
                break;
36
            } catch (ServerException | UnknownParserException $e) {
37
                // @ignoreException
38
            }
39
        }
40
41
        if (!$validated) {
42
            throw new PendingException('HTML validation services are not working');
43
        }
44
45
        if (isset($validationErrors[0])) {
46
            throw new InvalidArgumentException(
47
                sprintf(
48
                    'HTML markup validation error: Line %s: "%s" - %s in %s',
49
                    $validationErrors[0]->getFirstLine(),
50
                    $validationErrors[0]->getExtract(),
51
                    $validationErrors[0]->getText(),
52
                    $this->getCurrentUrl()
53
                )
54
            );
55
        }
56
    }
57
58
    /**
59
     * @Then the page HTML markup should not be valid
60
     */
61
    public function thePageHtmlMarkupShouldNotBeValid(): void
62
    {
63
        $this->assertInverse(
64
            [$this, 'thePageHtmlMarkupShouldBeValid'],
65
            'HTML markup should not be valid.'
66
        );
67
    }
68
    
69
    /**
70
     * @Then the page HTML5 doctype declaration should be valid     
71
     */
72
    public function thePageHtml5DoctypeDeclarationShouldBeValid(): void
73
    {
74
        Assert::eq(
75
            self::HTML5_DOCTYPE_MARKUP,
76
            $this->getFirstLineOfPageContent()
77
        );
78
    }
79
80
    /**
81
     * @Then the page HTML5 doctype declaration should not be valid
82
     */
83
    public function thePageHtml5DoctypeDeclarationShouldBeNotValid(): void
84
    {
85
        Assert::notEq(
86
            self::HTML5_DOCTYPE_MARKUP,
87
            $this->getFirstLineOfPageContent()
88
        );
89
    }
90
91
    private function getFirstLineOfPageContent(): string
92
    {
93
        $pageContent = $this->getSession()->getPage()->getContent();
94
        $pageContentLines = explode(PHP_EOL, $pageContent);
95
        return trim(strtolower($pageContentLines[0]));
96
    }
97
}
98