Completed
Pull Request — 2.x (#328)
by
unknown
01:30
created

SeoPageTest::testBodyAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
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'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\SeoPage::hasHtmlAttribute() has been deprecated with message: use htmlAttributes()->has() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
        $page->removeHtmlAttributes('key2');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\Seo...:removeHtmlAttributes() has been deprecated with message: use htmlAttributes()->remove() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
83
        $this->assertFalse($page->hasHtmlAttribute('key2'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\SeoPage::hasHtmlAttribute() has been deprecated with message: use htmlAttributes()->has() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\SeoPage::hasHeadAttribute() has been deprecated with message: use headAttributes()->has() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
        $page->removeHeadAttribute('head1');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\Seo...::removeHeadAttribute() has been deprecated with message: use headAttributes()->remove() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
101
        $this->assertFalse($page->hasHeadAttribute('head1'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\SeoBundle\Seo\SeoPage::hasHeadAttribute() has been deprecated with message: use headAttributes()->has() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102
    }
103
104
    public function testBodyAttributes()
105
    {
106
        $page = new SeoPage();
107
        $page->bodyAttributes()->set(['body1' => 'value1']);
108
        $page->bodyAttributes()->add('body2', 'value2');
109
110
        $expected = [
111
            'body1' => 'value1',
112
            'body2' => 'value2',
113
        ];
114
115
        $this->assertSame($expected, $page->bodyAttributes()->all());
116
117
        $this->assertTrue($page->bodyAttributes()->has('body1'));
118
        $page->bodyAttributes()->remove('body1');
119
        $this->assertFalse($page->bodyAttributes()->has('body1'));
120
    }
121
122
    public function testSetTitle()
123
    {
124
        $page = new SeoPage();
125
        $page->setTitle('My title');
126
127
        $this->assertSame('My title', $page->getTitle());
128
    }
129
130
    public function testAddTitle()
131
    {
132
        $page = new SeoPage();
133
        $page->setTitle('My title');
134
        $page->setSeparator(' - ');
135
        $page->addTitle('Additional title');
136
137
        $this->assertSame('Additional title - My title', $page->getTitle());
138
    }
139
140
    public function testLinkCanonical()
141
    {
142
        $page = new SeoPage();
143
        $page->setLinkCanonical('http://example.com');
144
145
        $this->assertSame('http://example.com', $page->getLinkCanonical());
146
147
        $page->removeLinkCanonical();
148
        $this->assertSame('', $page->getLinkCanonical());
149
    }
150
151
    public function testLangAlternates()
152
    {
153
        $page = new SeoPage();
154
        $page->setLangAlternates(['http://example.com/' => 'x-default']);
155
        $page->addLangAlternate('http://example.com/en-us', 'en-us');
156
157
        $expected = [
158
            'http://example.com/' => 'x-default',
159
            'http://example.com/en-us' => 'en-us',
160
        ];
161
162
        $this->assertSame($expected, $page->getLangAlternates());
163
164
        $this->assertTrue($page->hasLangAlternate('http://example.com/'));
165
        $page->removeLangAlternate('http://example.com/');
166
        $this->assertFalse($page->hasLangAlternate('http://example.com/'));
167
    }
168
169
    /**
170
     * The hasMeta() should return true for a defined meta, false otherwise.
171
     */
172
    public function testHasMeta()
173
    {
174
        $page = new SeoPage();
175
        $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...
176
177
        $this->assertTrue($page->hasMeta('property', 'test'));
178
        $this->assertFalse($page->hasMeta('property', 'fake'));
179
    }
180
181
    public function testSetSeparator()
182
    {
183
        $page = new SeoPage();
184
185
        $this->assertInstanceOf(SeoPage::class, $page->setSeparator('-'));
186
    }
187
}
188