CanonicalLinkExtensionTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
c 3
b 0
f 0
dl 0
loc 71
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testRenderLinkTag() 0 19 1
A testGenerateUrlMethodWithNoRouteDefaultsToCurrentRequest() 0 7 1
A testGenerateUrlMethod() 0 7 1
A getExtension() 0 10 1
1
<?php
2
3
namespace Palmtree\CanonicalUrlBundle\Tests\Twig\Extension;
4
5
use Palmtree\CanonicalUrlBundle\Service\CanonicalUrlGenerator;
6
use Palmtree\CanonicalUrlBundle\Tests\AbstractTest;
7
use Palmtree\CanonicalUrlBundle\Twig\Extension\CanonicalLinkExtension;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
10
class CanonicalLinkExtensionTest extends AbstractTest
11
{
12
    /**
13
     * @dataProvider configProvider
14
     *
15
     * @param array $config
16
     */
17
    public function testGenerateUrlMethod(array $config)
18
    {
19
        $extension = $this->getExtension($config);
20
21
        $url = $extension->generateUrl('foo');
22
23
        $this->assertEquals('https://example.org/foo', $url);
24
    }
25
26
    /**
27
     * @dataProvider configProvider
28
     *
29
     * @param array $config
30
     */
31
    public function testGenerateUrlMethodWithNoRouteDefaultsToCurrentRequest(array $config)
32
    {
33
        $extension = $this->getExtension($config);
34
35
        $url = $extension->generateUrl();
36
37
        $this->assertEquals('https://example.org/foo', $url);
38
    }
39
40
    /**
41
     * @dataProvider configProvider
42
     *
43
     * @param array $config
44
     */
45
    public function testRenderLinkTag(array $config)
46
    {
47
        $extension = $this->getExtension($config);
48
49
        $loader = new \Twig_Loader_Filesystem();
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        $loader = /** @scrutinizer ignore-deprecated */ new \Twig_Loader_Filesystem();
Loading history...
50
        $loader->setPaths(__DIR__ . '/../../../Resources/views', 'PalmtreeCanonicalUrl');
51
52
        $twig = new \Twig_Environment($loader, [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated: since Twig 2.7, use "Twig\Environment" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
        $twig = /** @scrutinizer ignore-deprecated */ new \Twig_Environment($loader, [
Loading history...
53
            'debug'      => true,
54
            'cache'      => false,
55
            'autoescape' => 'html',
56
        ]);
57
58
        $twig->addExtension($extension);
59
60
        $html = $extension->renderLinkTag($twig, 'https://example.org');
61
        $html = \trim($html);
62
63
        $this->assertEquals('<link rel="canonical" href="https://example.org">', $html);
64
    }
65
66
    /**
67
     * @param $config
68
     *
69
     * @return CanonicalLinkExtension
70
     */
71
    protected function getExtension($config)
72
    {
73
        $urlGenerator = new CanonicalUrlGenerator($this->getRouter(), $config);
74
        $requestStack = new RequestStack();
75
76
        $requestStack->push($this->getFooRequest());
77
78
        $extension = new CanonicalLinkExtension($urlGenerator, $requestStack);
79
80
        return $extension;
81
    }
82
}
83