Completed
Pull Request — 2.x (#266)
by Maximilian
01:28
created

SeoPageTest::testSetStructuredData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
class SeoPageTest extends TestCase
20
{
21
    public function testAddMeta()
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()
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()
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()
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()
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()
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()
105
    {
106
        $page = new SeoPage();
107
        $page->setTitle('My title');
108
109
        $this->assertSame('My title', $page->getTitle());
110
    }
111
112
    public function testAddTitle()
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()
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()
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()
155
    {
156
        $page = new SeoPage();
157
        $page->addMeta('property', 'test', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
159
        $this->assertTrue($page->hasMeta('property', 'test'));
160
        $this->assertFalse($page->hasMeta('property', 'fake'));
161
    }
162
163
    public function testSetSeparator()
164
    {
165
        $page = new SeoPage();
166
167
        $this->assertInstanceOf(SeoPage::class, $page->setSeparator('-'));
168
    }
169
170
    public function testSetStructuredData()
171
    {
172
        $page = new SeoPage();
173
        $structuredData = file_get_contents(__DIR__.'/../Fixtures/structured_data.jsonld');
174
175
        $this->assertInstanceOf(SeoPage::class, $page->setStructuredData($structuredData));
176
    }
177
}
178