These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\MultiDomainBundle\Tests\Router; |
||
4 | |||
5 | use Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter; |
||
6 | use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
||
7 | use PHPUnit\Framework\TestCase; |
||
8 | use ReflectionClass; |
||
9 | use Symfony\Component\DependencyInjection\Container; |
||
10 | use Symfony\Component\HttpFoundation\Request; |
||
11 | use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
||
12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
13 | |||
14 | class DomainBasedLocaleRouterTest extends TestCase |
||
15 | { |
||
16 | public function testGenerate() |
||
17 | { |
||
18 | $request = $this->getRequest(); |
||
19 | $container = $this->getContainer($request); |
||
20 | $object = new DomainBasedLocaleRouter($container); |
||
21 | $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en_GB'), UrlGeneratorInterface::ABSOLUTE_URL); |
||
22 | $this->assertEquals('http://multilangdomain.tld/en/some-uri', $url); |
||
23 | |||
24 | $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en_GB'), UrlGeneratorInterface::ABSOLUTE_PATH); |
||
25 | $this->assertEquals('/en/some-uri', $url); |
||
26 | } |
||
27 | |||
28 | View Code Duplication | public function testGenerateWithOtherSite() |
|
0 ignored issues
–
show
|
|||
29 | { |
||
30 | $request = $this->getRequest(); |
||
31 | $request->setLocale('nl_BE'); |
||
32 | $container = $this->getContainer($request); |
||
33 | $object = new DomainBasedLocaleRouter($container); |
||
34 | $url = $object->generate('_slug', array('url' => 'some-uri', 'otherSite' => 'https://cia.gov'), UrlGeneratorInterface::ABSOLUTE_URL); |
||
35 | $this->assertEquals('http://multilangdomain.tld/nl/some-uri', $url); |
||
36 | |||
37 | $url = $object->generate('_slug', array('url' => 'some-uri'), UrlGeneratorInterface::ABSOLUTE_PATH); |
||
38 | $this->assertEquals('/nl/some-uri', $url); |
||
39 | } |
||
40 | |||
41 | View Code Duplication | public function testGenerateWithLocaleBasedOnCurrentRequest() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
42 | { |
||
43 | $request = $this->getRequest(); |
||
44 | $request->setLocale('nl_BE'); |
||
45 | $container = $this->getContainer($request); |
||
46 | $object = new DomainBasedLocaleRouter($container); |
||
47 | $url = $object->generate('_slug', array('url' => 'some-uri'), UrlGeneratorInterface::ABSOLUTE_URL); |
||
48 | $this->assertEquals('http://multilangdomain.tld/nl/some-uri', $url); |
||
49 | |||
50 | $url = $object->generate('_slug', array('url' => 'some-uri'), UrlGeneratorInterface::ABSOLUTE_PATH); |
||
51 | $this->assertEquals('/nl/some-uri', $url); |
||
52 | } |
||
53 | |||
54 | View Code Duplication | public function testMatchWithNodeTranslation() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
55 | { |
||
56 | $request = $this->getRequest(); |
||
57 | $nodeTranslation = new NodeTranslation(); |
||
58 | $container = $this->getContainer($request, $nodeTranslation); |
||
59 | $object = new DomainBasedLocaleRouter($container); |
||
60 | $result = $object->match('/en/some-uri'); |
||
61 | $this->assertEquals('some-uri', $result['url']); |
||
62 | $this->assertEquals('en_GB', $result['_locale']); |
||
63 | $this->assertEquals($nodeTranslation, $result['_nodeTranslation']); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function testMatchWithoutNodeTranslation() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
67 | { |
||
68 | $this->expectException(ResourceNotFoundException::class); |
||
69 | $request = $this->getRequest(); |
||
70 | $container = $this->getContainer($request); |
||
71 | $object = new DomainBasedLocaleRouter($container); |
||
72 | $object->match('/en/some-uri'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @throws \ReflectionException |
||
77 | */ |
||
78 | View Code Duplication | public function testAddMultiLangSlugRoute() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
79 | { |
||
80 | $domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface'); |
||
81 | $domainConfiguration->method('getHost') |
||
82 | ->willReturn('override-domain.tld'); |
||
83 | |||
84 | $domainConfiguration->method('isMultiDomainHost') |
||
85 | ->willReturn(true); |
||
86 | |||
87 | $domainConfiguration->method('isMultiLanguage') |
||
88 | ->willReturn(true); |
||
89 | |||
90 | $domainConfiguration->method('getDefaultLocale') |
||
91 | ->willReturn('nl_BE'); |
||
92 | |||
93 | $domainConfiguration->method('getFrontendLocales') |
||
94 | ->willReturn(array('nl', 'en')); |
||
95 | |||
96 | $node = $this->createMock('Kunstmaan\NodeBundle\Entity\Node'); |
||
97 | $domainConfiguration->method('getRootNode') |
||
98 | ->willReturn($node); |
||
99 | |||
100 | $domainConfiguration->method('getBackendLocales') |
||
101 | ->willReturn(array('nl_BE', 'en_GB')); |
||
102 | |||
103 | $request = $this->getRequest('http://singlelangdomain.tld/'); |
||
104 | |||
105 | $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
||
106 | $serviceMap = array( |
||
107 | array('request_stack', 1, $this->getRequestStack($request)), |
||
108 | array('kunstmaan_admin.domain_configuration', 1, $domainConfiguration), |
||
109 | array('doctrine.orm.entity_manager', 1, $this->getEntityManager(new NodeTranslation())), |
||
110 | ); |
||
111 | |||
112 | $container |
||
113 | ->method('get') |
||
114 | ->willReturnMap($serviceMap); |
||
115 | /** @var Container $container */ |
||
116 | $object = new DomainBasedLocaleRouter($container); |
||
117 | |||
118 | $mirror = new ReflectionClass(DomainBasedLocaleRouter::class); |
||
119 | $property = $mirror->getProperty('otherSite'); |
||
120 | $property->setAccessible(true); |
||
121 | $property->setValue($object, ['host' => 'https://cia.gov']); |
||
122 | $collection = $object->getRouteCollection(); |
||
123 | $array = $collection->getIterator()->getArrayCopy(); |
||
124 | $this->assertArrayHasKey('_slug', $array); |
||
125 | $this->assertArrayHasKey('_slug_preview', $array); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @throws \ReflectionException |
||
130 | */ |
||
131 | View Code Duplication | public function testGetRouteCollection() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
132 | { |
||
133 | $domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface'); |
||
134 | $domainConfiguration->method('getHost') |
||
135 | ->willReturn('override-domain.tld'); |
||
136 | |||
137 | $domainConfiguration->method('isMultiDomainHost') |
||
138 | ->willReturn(false); |
||
139 | |||
140 | $domainConfiguration->method('isMultiLanguage') |
||
141 | ->willReturn(false); |
||
142 | |||
143 | $domainConfiguration->method('getDefaultLocale') |
||
144 | ->willReturn('nl_BE'); |
||
145 | |||
146 | $domainConfiguration->method('getFrontendLocales') |
||
147 | ->willReturn(array('nl', 'en')); |
||
148 | |||
149 | $node = $this->createMock('Kunstmaan\NodeBundle\Entity\Node'); |
||
150 | $domainConfiguration->method('getRootNode') |
||
151 | ->willReturn($node); |
||
152 | |||
153 | $domainConfiguration->method('getBackendLocales') |
||
154 | ->willReturn(array('nl_BE', 'en_GB')); |
||
155 | |||
156 | $request = $this->getRequest('http://singlelangdomain.tld/'); |
||
157 | |||
158 | $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
||
159 | $serviceMap = array( |
||
160 | array('request_stack', 1, $this->getRequestStack($request)), |
||
161 | array('kunstmaan_admin.domain_configuration', 1, $domainConfiguration), |
||
162 | array('doctrine.orm.entity_manager', 1, $this->getEntityManager(new NodeTranslation())), |
||
163 | ); |
||
164 | |||
165 | $container |
||
166 | ->method('get') |
||
167 | ->willReturnMap($serviceMap); |
||
168 | /** @var Container $container */ |
||
169 | $object = new DomainBasedLocaleRouter($container); |
||
170 | $mirror = new ReflectionClass(DomainBasedLocaleRouter::class); |
||
171 | $property = $mirror->getProperty('otherSite'); |
||
172 | $property->setAccessible(true); |
||
173 | $property->setValue($object, ['host' => 'https://cia.gov']); |
||
174 | $collection = $object->getRouteCollection(); |
||
175 | $array = $collection->getIterator()->getArrayCopy(); |
||
176 | $this->assertArrayHasKey('_slug', $array); |
||
177 | $this->assertArrayHasKey('_slug_preview', $array); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param $request |
||
182 | * @param null $nodeTranslation |
||
183 | * |
||
184 | * @return Container |
||
185 | */ |
||
186 | View Code Duplication | private function getContainer($request, $nodeTranslation = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
187 | { |
||
188 | $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
||
189 | $serviceMap = array( |
||
190 | array('request_stack', 1, $this->getRequestStack($request)), |
||
191 | array('kunstmaan_admin.domain_configuration', 1, $this->getDomainConfiguration()), |
||
192 | array('doctrine.orm.entity_manager', 1, $this->getEntityManager($nodeTranslation)), |
||
193 | ); |
||
194 | |||
195 | $container |
||
196 | ->method('get') |
||
197 | ->willReturnMap($serviceMap); |
||
198 | |||
199 | /* @var Container $container */ |
||
200 | return $container; |
||
201 | } |
||
202 | |||
203 | private function getRequestStack($request) |
||
204 | { |
||
205 | $requestStack = $this->createMock('Symfony\Component\HttpFoundation\RequestStack'); |
||
206 | $requestStack->expects($this->any())->method('getMasterRequest')->willReturn($request); |
||
207 | |||
208 | return $requestStack; |
||
209 | } |
||
210 | |||
211 | private function getDomainConfiguration() |
||
212 | { |
||
213 | $domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface'); |
||
214 | $domainConfiguration->method('getHost') |
||
215 | ->willReturn('override-domain.tld'); |
||
216 | |||
217 | $domainConfiguration->method('isMultiDomainHost') |
||
218 | ->willReturn(true); |
||
219 | |||
220 | $domainConfiguration->method('isMultiLanguage') |
||
221 | ->willReturn(true); |
||
222 | |||
223 | $domainConfiguration->method('getDefaultLocale') |
||
224 | ->willReturn('nl_BE'); |
||
225 | |||
226 | $domainConfiguration->method('getFrontendLocales') |
||
227 | ->willReturn(array('nl', 'en')); |
||
228 | |||
229 | $node = $this->createMock('Kunstmaan\NodeBundle\Entity\Node'); |
||
230 | $domainConfiguration->method('getRootNode') |
||
231 | ->willReturn($node); |
||
232 | |||
233 | $domainConfiguration->method('getBackendLocales') |
||
234 | ->willReturn(array('nl_BE', 'en_GB')); |
||
235 | |||
236 | return $domainConfiguration; |
||
237 | } |
||
238 | |||
239 | private function getRequest($url = 'http://multilangdomain.tld/') |
||
240 | { |
||
241 | return Request::create($url); |
||
242 | } |
||
243 | |||
244 | View Code Duplication | private function getEntityManager($nodeTranslation = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
245 | { |
||
246 | $em = $this->createMock('Doctrine\ORM\EntityManagerInterface'); |
||
247 | $em |
||
248 | ->method('getRepository') |
||
249 | ->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation')) |
||
250 | ->willReturn($this->getNodeTranslationRepository($nodeTranslation)); |
||
251 | |||
252 | return $em; |
||
253 | } |
||
254 | |||
255 | View Code Duplication | private function getNodeTranslationRepository($nodeTranslation = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
256 | { |
||
257 | $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository') |
||
258 | ->disableOriginalConstructor() |
||
259 | ->getMock(); |
||
260 | $repository |
||
261 | ->method('getNodeTranslationForUrl') |
||
262 | ->willReturn($nodeTranslation); |
||
263 | |||
264 | return $repository; |
||
265 | } |
||
266 | } |
||
267 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.