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 | View Code Duplication | public function testGenerate() |
|
0 ignored issues
–
show
|
|||
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() |
|
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() |
|
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() |
|
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() |
|
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() |
|
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 | ->will($this->returnValueMap($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() |
|
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 | ->will($this->returnValueMap($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) |
|
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 | ->will($this->returnValueMap($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 | $request = Request::create($url); |
||
242 | |||
243 | return $request; |
||
244 | } |
||
245 | |||
246 | View Code Duplication | private function getEntityManager($nodeTranslation = null) |
|
247 | { |
||
248 | $em = $this->createMock('Doctrine\ORM\EntityManagerInterface'); |
||
249 | $em |
||
250 | ->method('getRepository') |
||
251 | ->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation')) |
||
252 | ->willReturn($this->getNodeTranslationRepository($nodeTranslation)); |
||
253 | |||
254 | return $em; |
||
255 | } |
||
256 | |||
257 | View Code Duplication | private function getNodeTranslationRepository($nodeTranslation = null) |
|
258 | { |
||
259 | $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository') |
||
260 | ->disableOriginalConstructor() |
||
261 | ->getMock(); |
||
262 | $repository |
||
263 | ->method('getNodeTranslationForUrl') |
||
264 | ->willReturn($nodeTranslation); |
||
265 | |||
266 | return $repository; |
||
267 | } |
||
268 | } |
||
269 |
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.