SeoPageTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 151
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddMeta() 0 15 1
A testOverrideMetas() 0 11 1
A testRemoveMeta() 0 8 1
A testInvalidMetas() 0 9 1
A testHtmlAttributes() 0 17 1
A testHeadAttributes() 0 17 1
A testSetTitle() 0 7 1
A testAddTitle() 0 9 1
A testLinkCanonical() 0 10 1
A testLangAlternates() 0 17 1
A testHasMeta() 0 8 1
A testSetSeparator() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\Tests\Seo;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\SeoBundle\Seo\SeoPage;
18
19
final class SeoPageTest extends TestCase
20
{
21
    public function testAddMeta(): void
22
    {
23
        $page = new SeoPage();
24
        $page->addMeta('property', 'foo', 'bar');
25
26
        $expected = [
27
            'http-equiv' => [],
28
            'name' => [],
29
            'schema' => [],
30
            'charset' => [],
31
            'property' => ['foo' => ['bar', []]],
32
        ];
33
34
        $this->assertSame($expected, $page->getMetas());
35
    }
36
37
    public function testOverrideMetas(): void
38
    {
39
        $page = new SeoPage();
40
        $page->setMetas(['property' => ['foo' => 'bar', 'foo2' => ['bar2', []]]]);
41
42
        $expected = [
43
            'property' => ['foo' => ['bar', []], 'foo2' => ['bar2', []]],
44
        ];
45
46
        $this->assertSame($expected, $page->getMetas());
47
    }
48
49
    public function testRemoveMeta(): void
50
    {
51
        $page = new SeoPage();
52
        $page->setMetas(['property' => ['foo' => 'bar', 'foo2' => ['bar2', []]]]);
53
        $page->removeMeta('property', 'foo');
54
55
        $this->assertFalse($page->hasMeta('property', 'foo'));
56
    }
57
58
    public function testInvalidMetas(): void
59
    {
60
        $this->expectException(\RuntimeException::class);
61
62
        $page = new SeoPage();
63
        $page->setMetas([
64
            'type' => 'not an array',
65
        ]);
66
    }
67
68
    public function testHtmlAttributes(): void
69
    {
70
        $page = new SeoPage();
71
        $page->setHtmlAttributes(['key1' => 'value1']);
72
        $page->addHtmlAttributes('key2', 'value2');
73
74
        $expected = [
75
            'key1' => 'value1',
76
            'key2' => 'value2',
77
        ];
78
79
        $this->assertSame($expected, $page->getHtmlAttributes());
80
81
        $this->assertTrue($page->hasHtmlAttribute('key2'));
82
        $page->removeHtmlAttributes('key2');
83
        $this->assertFalse($page->hasHtmlAttribute('key2'));
84
    }
85
86
    public function testHeadAttributes(): void
87
    {
88
        $page = new SeoPage();
89
        $page->setHeadAttributes(['head1' => 'value1']);
90
        $page->addHeadAttribute('head2', 'value2');
91
92
        $expected = [
93
            'head1' => 'value1',
94
            'head2' => 'value2',
95
        ];
96
97
        $this->assertSame($expected, $page->getHeadAttributes());
98
99
        $this->assertTrue($page->hasHeadAttribute('head1'));
100
        $page->removeHeadAttribute('head1');
101
        $this->assertFalse($page->hasHeadAttribute('head1'));
102
    }
103
104
    public function testSetTitle(): void
105
    {
106
        $page = new SeoPage();
107
        $page->setTitle('My title');
108
109
        $this->assertSame('My title', $page->getTitle());
110
    }
111
112
    public function testAddTitle(): void
113
    {
114
        $page = new SeoPage();
115
        $page->setTitle('My title');
116
        $page->setSeparator(' - ');
117
        $page->addTitle('Additional title');
118
119
        $this->assertSame('Additional title - My title', $page->getTitle());
120
    }
121
122
    public function testLinkCanonical(): void
123
    {
124
        $page = new SeoPage();
125
        $page->setLinkCanonical('http://example.com');
126
127
        $this->assertSame('http://example.com', $page->getLinkCanonical());
128
129
        $page->removeLinkCanonical();
130
        $this->assertSame('', $page->getLinkCanonical());
131
    }
132
133
    public function testLangAlternates(): void
134
    {
135
        $page = new SeoPage();
136
        $page->setLangAlternates(['http://example.com/' => 'x-default']);
137
        $page->addLangAlternate('http://example.com/en-us', 'en-us');
138
139
        $expected = [
140
            'http://example.com/' => 'x-default',
141
            'http://example.com/en-us' => 'en-us',
142
        ];
143
144
        $this->assertSame($expected, $page->getLangAlternates());
145
146
        $this->assertTrue($page->hasLangAlternate('http://example.com/'));
147
        $page->removeLangAlternate('http://example.com/');
148
        $this->assertFalse($page->hasLangAlternate('http://example.com/'));
149
    }
150
151
    /**
152
     * The hasMeta() should return true for a defined meta, false otherwise.
153
     */
154
    public function testHasMeta(): void
155
    {
156
        $page = new SeoPage();
157
        $page->addMeta('property', 'test', '');
158
159
        $this->assertTrue($page->hasMeta('property', 'test'));
160
        $this->assertFalse($page->hasMeta('property', 'fake'));
161
    }
162
163
    public function testSetSeparator(): void
164
    {
165
        $page = new SeoPage();
166
167
        $this->assertInstanceOf(SeoPage::class, $page->setSeparator('-'));
168
    }
169
}
170