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

testBackwardsCompatibleHeadAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
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\Request;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\SeoBundle\Seo\AttributeBag;
18
use Sonata\SeoBundle\Seo\SeoPageAttributesInterface;
19
use Sonata\SeoBundle\Seo\SeoPageInterface;
20
use Sonata\SeoBundle\Twig\Extension\SeoExtension;
21
use Twig\Loader\FilesystemLoader;
22
23
class SeoExtensionTest extends TestCase
24
{
25
    /**
26
     * @group legacy
27
     */
28
    public function testBackwardCompatibleHtmlAttributes()
29
    {
30
        $page = $this->createMock(SeoPageInterface::class);
31
        $page->expects($this->once())->method('getHtmlAttributes')->will($this->returnValue([
32
            'xmlns' => 'http://www.w3.org/1999/xhtml',
33
            'xmlns:og' => 'http://opengraphprotocol.org/schema/',
34
        ]));
35
36
        $extension = new SeoExtension($page, 'UTF-8');
37
38
        $this->assertSame(
39
            'xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"',
40
            $extension->getHtmlAttributes($this->getEnvironment())
0 ignored issues
show
Documentation introduced by
$this->getEnvironment() is of type object<Twig_Environment>, but the function expects a null|object<Twig\Environment>.

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...
41
        );
42
    }
43
44
    public function testHtmlAttributes()
45
    {
46
        $page = $this->createMock([SeoPageInterface::class, SeoPageAttributesInterface::class]);
47
        $page->expects($this->once())->method('htmlAttributes')->willReturn(new AttributeBag([
48
            'xmlns' => 'http://www.w3.org/1999/xhtml',
49
            'xmlns:og' => 'http://opengraphprotocol.org/schema/',
50
        ]));
51
52
        $extension = new SeoExtension($page, 'UTF-8');
53
54
        $this->assertSame(
55
            'xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/"',
56
            $extension->getHtmlAttributes($this->getEnvironment())
0 ignored issues
show
Documentation introduced by
$this->getEnvironment() is of type object<Twig_Environment>, but the function expects a null|object<Twig\Environment>.

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...
57
        );
58
    }
59
60
    /**
61
     * @group legacy
62
     */
63
    public function testBackwardsCompatibleHeadAttributes()
64
    {
65
        $page = $this->createMock(SeoPageInterface::class);
66
        $page->expects($this->once())->method('getHeadAttributes')->will($this->returnValue([]));
67
68
        $extension = new SeoExtension($page, 'UTF-8');
69
70
        $this->assertSame('', $extension->getHeadAttributes($this->getEnvironment()));
0 ignored issues
show
Documentation introduced by
$this->getEnvironment() is of type object<Twig_Environment>, but the function expects a null|object<Twig\Environment>.

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...
71
    }
72
73
    public function testHeadAttributes()
74
    {
75
        $page = $this->createMock([SeoPageInterface::class, SeoPageAttributesInterface::class]);
76
        $page->expects($this->once())->method('headAttributes')->willReturn(new AttributeBag());
77
78
        $extension = new SeoExtension($page, 'UTF-8');
79
80
        $this->assertSame('', $extension->getHeadAttributes($this->getEnvironment()));
0 ignored issues
show
Documentation introduced by
$this->getEnvironment() is of type object<Twig_Environment>, but the function expects a null|object<Twig\Environment>.

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...
81
    }
82
83
    public function testBodyAttributes()
84
    {
85
        $page = $this->createMock([SeoPageInterface::class, SeoPageAttributesInterface::class]);
86
87
        $page->expects($this->once())->method('bodyAttributes')->willReturn(new AttributeBag());
88
89
        $extension = new SeoExtension($page, 'UTF-8');
90
91
        $this->assertSame('', $extension->getBodyAttributes($this->getEnvironment()));
0 ignored issues
show
Compatibility introduced by
$this->getEnvironment() of type object<Twig_Environment> is not a sub-type of object<Twig\Environment>. It seems like you assume a child class of the class Twig_Environment to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
92
    }
93
94
    public function testTitle()
95
    {
96
        $page = $this->createMock(SeoPageInterface::class);
97
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('<b>foo bar</b>'));
98
99
        $extension = new SeoExtension($page, 'UTF-8');
100
101
        $this->assertSame('<title>foo bar</title>', $extension->getTitle());
102
    }
103
104
    public function testEncoding()
105
    {
106
        $page = $this->createMock(SeoPageInterface::class);
107
        $page->expects($this->once())->method('getTitle')->will($this->returnValue('pięć głów zatkniętych na pal'));
108
        $page->expects($this->once())->method('getMetas')->will($this->returnValue([
109
            'http-equiv' => [],
110
            'name' => ['foo' => ['pięć głów zatkniętych na pal', []]],
111
            'schema' => [],
112
            'charset' => [],
113
            'property' => [],
114
        ]));
115
116
        $extension = new SeoExtension($page, 'UTF-8');
117
118
        $this->assertSame('<title>pięć głów zatkniętych na pal</title>', $extension->getTitle());
119
120
        $this->assertSame(
121
            "<meta name=\"foo\" content=\"pięć gł&oacute;w zatkniętych na pal\" />\n",
122
            $extension->getMetadatas()
123
        );
124
    }
125
126
    public function testMetadatas()
127
    {
128
        $page = $this->createMock(SeoPageInterface::class);
129
        $page->expects($this->once())->method('getMetas')->will($this->returnValue([
130
            'http-equiv' => [],
131
            'name' => ['foo' => ['bar "\'"', []]],
132
            'schema' => [],
133
            'charset' => ['UTF-8' => ['', []]],
134
            'property' => [],
135
        ]));
136
137
        $extension = new SeoExtension($page, 'UTF-8');
138
139
        $this->assertSame(
140
            "<meta name=\"foo\" content=\"bar &quot;'&quot;\" />\n<meta charset=\"UTF-8\" />\n",
141
            $extension->getMetadatas()
142
        );
143
    }
144
145
    public function testName()
146
    {
147
        $page = $this->createMock(SeoPageInterface::class);
148
        $extension = new SeoExtension($page, 'UTF-8');
149
150
        $this->assertSame('sonata_seo', $extension->getName());
151
    }
152
153
    public function testLinkCanonical()
154
    {
155
        $page = $this->createMock(SeoPageInterface::class);
156
        $page->expects($this->any())->method('getLinkCanonical')->will($this->returnValue('http://example.com'));
157
158
        $extension = new SeoExtension($page, 'UTF-8');
159
160
        $this->assertSame(
161
            "<link rel=\"canonical\" href=\"http://example.com\"/>\n",
162
            $extension->getLinkCanonical()
163
        );
164
    }
165
166
    public function testLangAlternates()
167
    {
168
        $page = $this->createMock(SeoPageInterface::class);
169
        $page->expects($this->once())->method('getLangAlternates')->will($this->returnValue([
170
                    'http://example.com/' => 'x-default',
171
                ]));
172
173
        $extension = new SeoExtension($page, 'UTF-8');
174
175
        $this->assertSame(
176
            "<link rel=\"alternate\" href=\"http://example.com/\" hreflang=\"x-default\"/>\n",
177
            $extension->getLangAlternates()
178
        );
179
    }
180
181
    public function testOEmbedLinks()
182
    {
183
        $page = $this->createMock(SeoPageInterface::class);
184
        $page->expects($this->once())->method('getOembedLinks')->will($this->returnValue([
185
            'Foo' => 'http://example.com/',
186
        ]));
187
188
        $extension = new SeoExtension($page, 'UTF-8');
189
190
        $this->assertSame(
191
            "<link rel=\"alternate\" type=\"application/json+oembed\" href=\"http://example.com/\" title=\"Foo\" />\n",
192
            $extension->getOembedLinks()
193
        );
194
    }
195
196
    protected function getEnvironment()
197
    {
198
        $loader = new FilesystemLoader();
199
        $loader->addPath(__DIR__.'/../../../src/Resources/views', 'SonataSeo');
200
201
        return new \Twig_Environment($loader);
202
    }
203
}
204