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
|
|
|
* @param array $config |
15
|
|
|
*/ |
16
|
|
|
public function testGenerateUrlMethod(array $config) |
17
|
|
|
{ |
18
|
|
|
$extension = $this->getExtension($config); |
19
|
|
|
|
20
|
|
|
$url = $extension->generateUrl('foo'); |
21
|
|
|
|
22
|
|
|
$this->assertEquals('https://example.org/foo', $url); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @dataProvider configProvider |
27
|
|
|
* @param array $config |
28
|
|
|
*/ |
29
|
|
|
public function testGenerateUrlMethodWithNoRouteDefaultsToCurrentRequest(array $config) |
30
|
|
|
{ |
31
|
|
|
$extension = $this->getExtension($config); |
32
|
|
|
|
33
|
|
|
$url = $extension->generateUrl(); |
34
|
|
|
|
35
|
|
|
$this->assertEquals('https://example.org/foo', $url); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider configProvider |
40
|
|
|
* @param array $config |
41
|
|
|
*/ |
42
|
|
|
public function testRenderLinkTag(array $config) |
43
|
|
|
{ |
44
|
|
|
$extension = $this->getExtension($config); |
45
|
|
|
|
46
|
|
|
$loader = new \Twig_Loader_Filesystem(); |
47
|
|
|
$loader->setPaths(__DIR__ . '/../../../Resources/views', 'PalmtreeCanonicalUrl'); |
48
|
|
|
|
49
|
|
|
$twig = new \Twig_Environment($loader, [ |
50
|
|
|
'debug' => true, |
51
|
|
|
'cache' => false, |
52
|
|
|
'autoescape' => 'html', |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$twig->addExtension($extension); |
56
|
|
|
|
57
|
|
|
$html = $extension->renderLinkTag($twig, 'https://example.org'); |
58
|
|
|
$html = trim($html); |
59
|
|
|
|
60
|
|
|
$this->assertEquals('<link rel="canonical" href="https://example.org">', $html); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $config |
65
|
|
|
* @return CanonicalLinkExtension |
66
|
|
|
*/ |
67
|
|
|
protected function getExtension($config) |
68
|
|
|
{ |
69
|
|
|
$urlGenerator = new CanonicalUrlGenerator($this->getRouter(), $config); |
70
|
|
|
$requestStack = new RequestStack(); |
71
|
|
|
|
72
|
|
|
$requestStack->push($this->getFooRequest()); |
73
|
|
|
|
74
|
|
|
$extension = new CanonicalLinkExtension($urlGenerator, $requestStack); |
75
|
|
|
|
76
|
|
|
return $extension; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|