IndexationContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 52
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A gatherContexts() 0 7 2
A thePageShouldNotBeIndexable() 0 5 1
A thePageShouldBeIndexable() 0 13 2
1
<?php declare(strict_types=1);
2
3
namespace MOrtola\BehatSEOContexts\Context;
4
5
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
6
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
7
use Webmozart\Assert\Assert;
8
9
class IndexationContext extends BaseContext
10
{
11
    /**
12
     * @var RobotsContext
13
     */
14
    private $robotsContext;
15
16
    /**
17
     * @var MetaContext
18
     */
19
    private $metaContext;
20
21
    /**
22
     * @BeforeScenario
23
     */
24
    public function gatherContexts(BeforeScenarioScope $scope): void
25
    {
26
        $env = $scope->getEnvironment();
27
28
        if ($env instanceof InitializedContextEnvironment) {
29
            $this->robotsContext = $env->getContext(RobotsContext::class);
30
            $this->metaContext   = $env->getContext(MetaContext::class);
31
        }
32
    }
33
34
    /**
35
     * @Then the page should not be indexable
36
     */
37
    public function thePageShouldNotBeIndexable(): void
38
    {
39
        $this->assertInverse(
40
            [$this, 'thePageShouldBeIndexable'],
41
            'The page is indexable.'
42
        );
43
    }
44
45
    /**
46
     * @Then the page should be indexable
47
     */
48
    public function thePageShouldBeIndexable(): void
49
    {
50
        $this->metaContext->thePageShouldNotBeNoindex();
51
        $this->robotsContext->iShouldBeAbleToCrawl($this->getCurrentUrl());
52
53
        if ($robotsHeaderTag = $this->getResponseHeader('X-Robots-Tag')) {
54
            Assert::notContains(
55
                strtolower($robotsHeaderTag),
56
                'noindex',
57
                sprintf(
58
                    'Url %s should not send X-Robots-Tag HTTP header with noindex value: %s',
59
                    $this->getCurrentUrl(),
60
                    $robotsHeaderTag
61
                )
62
            );
63
        }
64
    }
65
}
66