1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Tests\Router; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
6
|
|
|
use Kunstmaan\NodeBundle\Router\SlugRouter; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11
|
|
|
|
12
|
|
|
class SlugRouterTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testGenerateMultiLanguage() |
15
|
|
|
{ |
16
|
|
|
$request = $this->getRequest(); |
17
|
|
|
$container = $this->getContainer($request, true); |
18
|
|
|
$object = new SlugRouter($container); |
19
|
|
|
$url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en'), UrlGeneratorInterface::ABSOLUTE_URL); |
20
|
|
|
$this->assertEquals('http://domain.tld/en/some-uri', $url); |
21
|
|
|
|
22
|
|
|
$url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en'), UrlGeneratorInterface::ABSOLUTE_PATH); |
23
|
|
|
$this->assertEquals('/en/some-uri', $url); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGenerateSingleLanguage() |
27
|
|
|
{ |
28
|
|
|
$request = $this->getRequest(); |
29
|
|
|
$container = $this->getContainer($request); |
30
|
|
|
$object = new SlugRouter($container); |
31
|
|
|
$url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'nl'), UrlGeneratorInterface::ABSOLUTE_URL); |
32
|
|
|
$this->assertEquals('http://domain.tld/some-uri', $url); |
33
|
|
|
|
34
|
|
|
$url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'nl'), UrlGeneratorInterface::ABSOLUTE_PATH); |
35
|
|
|
$this->assertEquals('/some-uri', $url); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testSetContext() |
39
|
|
|
{ |
40
|
|
|
$context = $this->createMock('Symfony\Component\Routing\RequestContext'); |
41
|
|
|
$container = $this->getContainer(null); |
42
|
|
|
$object = new SlugRouter($container); |
43
|
|
|
$object->setContext($context); |
|
|
|
|
44
|
|
|
$this->assertEquals($context, $object->getContext()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testMatchWithNodeTranslation() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$request = $this->getRequest(); |
50
|
|
|
$nodeTranslation = new NodeTranslation(); |
51
|
|
|
$container = $this->getContainer($request, true, $nodeTranslation); |
52
|
|
|
$object = new SlugRouter($container); |
53
|
|
|
$result = $object->match('/en/some-uri'); |
54
|
|
|
$this->assertEquals('some-uri', $result['url']); |
55
|
|
|
$this->assertEquals('en', $result['_locale']); |
56
|
|
|
$this->assertEquals($nodeTranslation, $result['_nodeTranslation']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testMatchWithoutNodeTranslation() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->expectException(ResourceNotFoundException::class); |
62
|
|
|
$request = $this->getRequest(); |
63
|
|
|
$container = $this->getContainer($request); |
64
|
|
|
$object = new SlugRouter($container); |
65
|
|
|
$object->match('/en/some-uri'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
private function getContainer($request, $multiLanguage = false, $nodeTranslation = null) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
71
|
|
|
$serviceMap = array( |
72
|
|
|
array('request_stack', 1, $this->getRequestStack($request)), |
73
|
|
|
array('kunstmaan_admin.domain_configuration', 1, $this->getDomainConfiguration($multiLanguage)), |
74
|
|
|
array('doctrine.orm.entity_manager', 1, $this->getEntityManager($nodeTranslation)), |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$container |
78
|
|
|
->method('get') |
79
|
|
|
->willReturnMap($serviceMap); |
80
|
|
|
|
81
|
|
|
return $container; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getRequestStack($request) |
85
|
|
|
{ |
86
|
|
|
$requestStack = $this->createMock('Symfony\Component\HttpFoundation\RequestStack'); |
87
|
|
|
$requestStack->expects($this->any())->method('getMasterRequest')->willReturn($request); |
88
|
|
|
|
89
|
|
|
return $requestStack; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getDomainConfiguration($multiLanguage = false) |
93
|
|
|
{ |
94
|
|
|
$domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface'); |
95
|
|
|
$domainConfiguration->method('getHost') |
96
|
|
|
->willReturn('domain.tld'); |
97
|
|
|
|
98
|
|
|
$domainConfiguration->method('isMultiDomainHost') |
99
|
|
|
->willReturn(false); |
100
|
|
|
|
101
|
|
|
$domainConfiguration->method('isMultiLanguage') |
102
|
|
|
->willReturn($multiLanguage); |
103
|
|
|
|
104
|
|
|
$domainConfiguration->method('getDefaultLocale') |
105
|
|
|
->willReturn('nl'); |
106
|
|
|
|
107
|
|
|
$domainConfiguration->method('getFrontendLocales') |
108
|
|
|
->willReturn($multiLanguage ? array('nl', 'en') : array('nl')); |
109
|
|
|
|
110
|
|
|
$domainConfiguration->method('getBackendLocales') |
111
|
|
|
->willReturn($multiLanguage ? array('nl', 'en') : array('nl')); |
112
|
|
|
|
113
|
|
|
$domainConfiguration->method('getRootNode') |
114
|
|
|
->willReturn(null); |
115
|
|
|
|
116
|
|
|
return $domainConfiguration; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getRequest($url = 'http://domain.tld/') |
120
|
|
|
{ |
121
|
|
|
return Request::create($url); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
private function getEntityManager($nodeTranslation = null) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$em = $this->createMock('Doctrine\ORM\EntityManagerInterface'); |
127
|
|
|
$em |
128
|
|
|
->method('getRepository') |
129
|
|
|
->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation')) |
130
|
|
|
->willReturn($this->getNodeTranslationRepository($nodeTranslation)); |
131
|
|
|
|
132
|
|
|
return $em; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
private function getNodeTranslationRepository($nodeTranslation = null) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository') |
138
|
|
|
->disableOriginalConstructor() |
139
|
|
|
->getMock(); |
140
|
|
|
$repository |
141
|
|
|
->method('getNodeTranslationForUrl') |
142
|
|
|
->willReturn($nodeTranslation); |
143
|
|
|
|
144
|
|
|
return $repository; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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: