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

SeoPageTest::testSetStructuredData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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