Passed
Push — feature/tests ( a58f99...9b7685 )
by Marc
02:09
created

MetaContext::thePageTitleShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace MOrtola\BehatSEOContexts\Context;
4
5
use Behat\Mink\Element\NodeElement;
6
use PHPUnit\Framework\Assert;
7
8
class MetaContext extends BaseContext
9
{
10
    /**
11
     * @throws \Exception
12
     *
13
     * @Then the page canonical should be :expectedCanonicalUrl
14
     */
15
    public function thePageCanonicalShouldBe(string $expectedCanonicalUrl)
16
    {
17
        Assert::assertNotNull(
18
            $this->getCanonicalElement(),
19
            'Canonical element does not exist'
20
        );
21
22
        Assert::assertEquals(
23
            $this->toAbsoluteUrl($expectedCanonicalUrl),
24
            $this->getCanonicalElement()->getAttribute('href'),
25
            sprintf('Canonical url should be "%s"', $this->toAbsoluteUrl($expectedCanonicalUrl))
26
        );
27
    }
28
29
    /**
30
     * @return NodeElement|null
31
     */
32
    private function getCanonicalElement()
33
    {
34
        return $this->getSession()->getPage()->find(
35
            'xpath',
36
            '//head/link[@rel="canonical"]'
37
        );
38
    }
39
40
    /**
41
     * @Then the page meta robots should be noindex
42
     */
43
    public function thePageShouldBeNoindex()
44
    {
45
        Assert::assertNotNull(
46
            $this->getMetaRobotsElement(),
47
            'Meta robots does not exist.'
48
        );
49
50
        Assert::assertContains(
51
            'noindex',
52
            strtolower($this->getMetaRobotsElement()->getAttribute('content')),
53
            sprintf(
54
                'Url %s is not noindex: %s',
55
                $this->getCurrentUrl(),
56
                $this->getMetaRobotsElement()->getOuterHtml()
57
            )
58
        );
59
    }
60
61
    /**
62
     * @return NodeElement|null
63
     */
64
    private function getMetaRobotsElement()
65
    {
66
        return $this->getSession()->getPage()->find(
67
            'xpath',
68
            '//head/meta[@name="robots"]'
69
        );
70
    }
71
72
    /**
73
     * @Then the page meta robots should not be noindex
74
     */
75
    public function thePageShouldNotBeNoindex()
76
    {
77
        $this->assertInverse(
78
            [$this, 'thePageShouldBeNoindex'],
79
            'Page meta robots is noindex.'
80
        );
81
    }
82
83
    /**
84
     * @throws \Exception
85
     *
86
     * @Then the page title should be :expectedTitle
87
     */
88
    public function thePageTitleShouldBe(string $expectedTitle)
89
    {
90
        Assert::assertNotNull(
91
            $this->getTitleElement(),
92
            'Title tag does not exist'
93
        );
94
95
        Assert::assertEquals(
96
            $expectedTitle,
97
            $this->getTitleElement()->getText(),
98
            sprintf(
99
                'Title tag is not "%s"',
100
                $expectedTitle
101
            )
102
        );
103
    }
104
105
    /**
106
     * @return NodeElement|null
107
     */
108
    private function getTitleElement()
109
    {
110
        return $this->getSession()->getPage()->find('css', 'title');
111
    }
112
113
    /**
114
     * @throws \Exception
115
     *
116
     * @Then the page meta description should be :expectedMetaDescription
117
     */
118
    public function thePageMetaDescriptionShouldBe(string $expectedMetaDescription)
119
    {
120
        Assert::assertNotNull(
121
            $this->getMetaDescriptionElement(),
122
            'Meta description does not exist'
123
        );
124
125
        Assert::assertEquals(
126
            $expectedMetaDescription,
127
            $this->getMetaDescriptionElement()->getAttribute('content'),
128
            sprintf(
129
                'Meta description is not "%s"',
130
                $expectedMetaDescription
131
            )
132
        );
133
    }
134
135
    /**
136
     * @return NodeElement|null
137
     */
138
    private function getMetaDescriptionElement()
139
    {
140
        return $this->getSession()->getPage()->find(
141
            'xpath',
142
            '//head/meta[@name="description"]'
143
        );
144
    }
145
146
    /**
147
     * @Then the page canonical should not exist
148
     */
149
    public function thePageCanonicalShouldNotExist()
150
    {
151
        Assert::assertNull(
152
            $this->getCanonicalElement(),
153
            'Canonical does exist'
154
        );
155
    }
156
157
    /**
158
     * @Then the page title should not exist
159
     */
160
    public function thePageTitleShouldNotExist()
161
    {
162
        Assert::assertNull(
163
            $this->getTitleElement(),
164
            'Title tag does exist.'
165
        );
166
    }
167
168
    /**
169
     * @Then the page meta description should not exist
170
     */
171
    public function thePageMetaDescriptionShouldNotExist()
172
    {
173
        Assert::assertNull(
174
            $this->getMetaDescriptionElement(),
175
            'Meta description does exist'
176
        );
177
    }
178
}
179