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(); |
|
|
|
|
50
|
|
|
$loader->setPaths(__DIR__ . '/../../../Resources/views', 'PalmtreeCanonicalUrl'); |
51
|
|
|
|
52
|
|
|
$twig = new \Twig_Environment($loader, [ |
|
|
|
|
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
|
|
|
|