Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

testGetBackendLocalesWithUnknown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\MultiDomainBundle\Tests\Helper;
4
5
use Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration;
6
use Kunstmaan\NodeBundle\Entity\Node;
7
use PHPUnit\Framework\TestCase;
8
use ReflectionClass;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Session;
11
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
12
13
class DomainConfigurationTest extends TestCase
14
{
15
    /**
16
     * @var Node
17
     */
18
    protected $node;
19
20
    public function testGetHostWithMultiLanguage()
21
    {
22
        $request = $this->getMultiLanguageRequest();
23
        $object = $this->getDomainConfiguration($request);
24
        $this->assertEquals('multilangdomain.tld', $object->getHost());
25
    }
26
27
    public function testGetHostWithSingleLanguage()
28
    {
29
        $request = $this->getSingleLanguageRequest();
30
        $object = $this->getDomainConfiguration($request);
31
        $this->assertEquals('singlelangdomain.tld', $object->getHost());
32
    }
33
34
    public function testGetHostWithAlias()
35
    {
36
        $request = $this->getAliasedRequest();
37
        $object = $this->getDomainConfiguration($request);
38
        $this->assertEquals('singlelangdomain.tld', $object->getHost());
39
    }
40
41
    /**
42
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::__construct
43
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getMasterRequest
44
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getHost
45
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::hasHostOverride
46
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getHostOverride
47
     */
48
    public function testGetHostWithOverrideOnFrontend()
49
    {
50
        $request = $this->getRequestWithOverride('/frontend-uri');
51
        $object = $this->getDomainConfiguration($request);
52
        $this->assertEquals('multilangdomain.tld', $object->getHost());
53
    }
54
55
    /**
56
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::__construct
57
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getMasterRequest
58
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getHost
59
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::hasHostOverride
60
     * @covers \Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration::getHostOverride
61
     */
62
    public function testGetHostWithOverrideOnBackend()
63
    {
64
        $request = $this->getRequestWithOverride('/nl/admin/backend-uri');
65
        $object = $this->getDomainConfiguration($request);
66
        $this->assertEquals('singlelangdomain.tld', $object->getHost());
67
    }
68
69
    /**
70
     * @throws \ReflectionException
71
     */
72
    public function testHostOverrideReturnsNull()
73
    {
74
        $request = new Request();
75
        $object = $this->getDomainConfiguration($request);
76
        $reflection = new ReflectionClass(DomainConfiguration::class);
77
        $method = $reflection->getMethod('getHostOverride');
78
        $method->setAccessible(true);
79
80
        $this->assertNull($method->invoke($object));
81
    }
82
83
    public function testGetHosts()
84
    {
85
        $request = $this->getSingleLanguageRequest();
86
        $object = $this->getDomainConfiguration($request);
87
        $this->assertEquals(array('multilangdomain.tld', 'singlelangdomain.tld'), $object->getHosts());
88
    }
89
90
    public function testGetDefaultLocale()
91
    {
92
        $request = $this->getSingleLanguageRequest();
93
        $object = $this->getDomainConfiguration($request);
94
        $this->assertEquals('en_GB', $object->getDefaultLocale());
95
    }
96
97
    public function testGetDefaultLocaleWithUnknownDomain()
98
    {
99
        $request = $this->getUnknownDomainRequest();
100
        $object = $this->getDomainConfiguration($request);
101
        $this->assertEquals('en', $object->getDefaultLocale());
102
    }
103
104
    public function testGetExtraDataWithoutDataSet()
105
    {
106
        $request = $this->getSingleLanguageRequest();
107
        $object = $this->getDomainConfiguration($request);
108
        $this->assertEquals(array(), $object->getExtraData());
109
    }
110
111
    public function testGetExtraDataWithDataSet()
112
    {
113
        $request = $this->getMultiLanguageRequest();
114
        $object = $this->getDomainConfiguration($request);
115
        $this->assertEquals(array('foo' => 'bar'), $object->getExtraData());
116
    }
117
118
    public function testGetRootNode()
119
    {
120
        $request = $this->getSingleLanguageRequest();
121
        $object = $this->getDomainConfiguration($request);
122
        $this->assertEquals($this->node, $object->getRootNode());
123
    }
124
125
    public function testGetRootNodeWithUnknown()
126
    {
127
        $request = $this->getUnknownDomainRequest();
128
        $object = $this->getDomainConfiguration($request);
129
        $this->assertNull($object->getRootNode());
130
    }
131
132
    public function testIsMultiDomainHostWithSingleLanguage()
133
    {
134
        $request = $this->getSingleLanguageRequest();
135
        $object = $this->getDomainConfiguration($request);
136
        $this->assertTrue($object->isMultiDomainHost());
137
    }
138
139
    public function testIsMultiDomainHostWithMultiLanguage()
140
    {
141
        $request = $this->getMultiLanguageRequest();
142
        $object = $this->getDomainConfiguration($request);
143
        $this->assertTrue($object->isMultiDomainHost());
144
    }
145
146
    public function testIsMultiDomainHostWithUnknown()
147
    {
148
        $request = $this->getUnknownDomainRequest();
149
        $object = $this->getDomainConfiguration($request);
150
        $this->assertFalse($object->isMultiDomainHost());
151
    }
152
153
    public function testIsMultiLanguageWithSingleLanguage()
154
    {
155
        $request = $this->getSingleLanguageRequest();
156
        $object = $this->getDomainConfiguration($request);
157
        $this->assertFalse($object->isMultiLanguage());
158
    }
159
160
    public function testIsMultiLanguageWithMultiLanguage()
161
    {
162
        $request = $this->getMultiLanguageRequest();
163
        $object = $this->getDomainConfiguration($request);
164
        $this->assertTrue($object->isMultiLanguage());
165
    }
166
167
    public function testIsMultiLanguageWithUnknown()
168
    {
169
        $request = $this->getUnknownDomainRequest();
170
        $object = $this->getDomainConfiguration($request);
171
        $this->assertFalse($object->isMultiLanguage());
172
    }
173
174
    public function testGetFrontendLocalesWithSingleLanguage()
175
    {
176
        $request = $this->getSingleLanguageRequest();
177
        $object = $this->getDomainConfiguration($request);
178
        $this->assertEquals(array('en'), $object->getFrontendLocales());
179
    }
180
181
    public function testGetHostBaseUrl()
182
    {
183
        $request = $this->getSingleLanguageRequest();
184
        $object = $this->getDomainConfiguration($request);
185
        $this->assertEquals('http://multilangdomain.tld', $object->getHostBaseUrl('multilangdomain.tld'));
186
    }
187
188
    public function testGetFullHost()
189
    {
190
        $request = $this->getSingleLanguageRequest();
191
        $object = $this->getDomainConfiguration($request);
192
        $this->assertNull($object->getFullHost('not-here.tld'));
193
    }
194
195
    public function testGetLocalesExtraData()
196
    {
197
        $request = $this->getSingleLanguageRequest();
198
        $object = $this->getDomainConfiguration($request);
199
        $data = $object->getLocalesExtraData();
200
        $this->assertEmpty($data);
201
        $request = $this->getMultiLanguageRequest();
202
        $object = $this->getDomainConfiguration($request);
203
        $data = $object->getLocalesExtraData();
204
        $this->assertArrayHasKey('foo', $data);
205
    }
206
207
    public function testGetFullHostConfig()
208
    {
209
        $request = $this->getSingleLanguageRequest();
210
        $object = $this->getDomainConfiguration($request);
211
        $array = $object->getFullHostConfig();
212
        $this->assertArrayHasKey('multilangdomain.tld', $array);
213
        $this->assertArrayHasKey('singlelangdomain.tld', $array);
214
    }
215
216
    public function testHasHostSwitched()
217
    {
218
        $request = $this->getRequestWithOverride('/admin/somewhere');
219
        $object = $this->getDomainConfiguration($request);
220
        $this->assertTrue($object->hasHostSwitched());
221
    }
222
223
    public function testGetHostSwitched()
224
    {
225
        $request = $this->getRequestWithOverride('/admin/somewhere');
226
        $object = $this->getDomainConfiguration($request);
227
        $switched = $object->getHostSwitched();
228
        $this->assertArrayHasKey('id', $switched);
229
        $this->assertArrayHasKey('host', $switched);
230
        $this->assertArrayHasKey('protocol', $switched);
231
        $this->assertArrayHasKey('type', $switched);
232
    }
233
234
    public function testFullHostById()
235
    {
236
        $request = $this->getMultiLanguageRequest();
237
        $object = $this->getDomainConfiguration($request);
238
        $this->assertNull($object->getFullHostById(666));
239
        $this->assertNotNull($object->getFullHostById(123));
240
    }
241
242
    public function testGetFrontendLocalesWithMultiLanguage()
243
    {
244
        $request = $this->getMultiLanguageRequest();
245
        $object = $this->getDomainConfiguration($request);
246
        $this->assertEquals(array('nl', 'fr', 'en'), $object->getFrontendLocales());
247
    }
248
249
    public function testGetFrontendLocalesWithUnknown()
250
    {
251
        $request = $this->getUnknownDomainRequest();
252
        $object = $this->getDomainConfiguration($request);
253
        $this->assertEquals(array('en'), $object->getFrontendLocales());
254
    }
255
256
    public function testGetBackendLocalesWithSingleLanguage()
257
    {
258
        $request = $this->getSingleLanguageRequest();
259
        $object = $this->getDomainConfiguration($request);
260
        $this->assertEquals(array('en_GB'), $object->getBackendLocales());
261
    }
262
263
    public function testGetBackendLocalesWithMultiLanguage()
264
    {
265
        $request = $this->getMultiLanguageRequest();
266
        $object = $this->getDomainConfiguration($request);
267
        $this->assertEquals(array('nl_BE', 'fr_BE', 'en_GB'), $object->getBackendLocales());
268
    }
269
270
    public function testGetBackendLocalesWithUnknown()
271
    {
272
        $request = $this->getUnknownDomainRequest();
273
        $object = $this->getDomainConfiguration($request);
274
        $this->assertEquals(array('en'), $object->getBackendLocales());
275
    }
276
277 View Code Duplication
    private function getContainer($map, $request)
0 ignored issues
show
Duplication introduced by
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...
278
    {
279
        $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
280
281
        $container
282
            ->method('getParameter')
283
            ->willReturnMap($map);
284
285
        $serviceMap = array(
286
            array('request_stack', 1, $this->getRequestStack($request)),
287
            array('doctrine.orm.entity_manager', 1, $this->getEntityManager()),
288
            array('kunstmaan_admin.adminroute.helper', 1, $this->getAdminRouteHelper()),
289
        );
290
291
        $container
292
            ->method('get')
293
            ->willReturnMap($serviceMap);
294
295
        return $container;
296
    }
297
298
    private function getEntityManager()
299
    {
300
        $em = $this->createMock('Doctrine\ORM\EntityManagerInterface');
301
        $em
302
            ->method('getRepository')
303
            ->with($this->equalTo('KunstmaanNodeBundle:Node'))
304
            ->willReturn($this->getNodeRepository());
305
306
        return $em;
307
    }
308
309
    private function getAdminRouteHelper()
310
    {
311
        $adminRouteReturnValueMap = [
312
            ['/frontend-uri', false],
313
            ['/nl/admin/backend-uri', true],
314
            ['/admin/somewhere', true],
315
        ];
316
317
        $adminRouteHelper = $this->getMockBuilder('Kunstmaan\AdminBundle\Helper\AdminRouteHelper')
318
            ->disableOriginalConstructor()
319
            ->getMock();
320
        $adminRouteHelper
321
            ->expects($this->any())
322
            ->method('isAdminRoute')
323
            ->willReturnMap($adminRouteReturnValueMap);
324
325
        return $adminRouteHelper;
326
    }
327
328
    private function getNodeRepository()
329
    {
330
        $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeRepository')
331
            ->disableOriginalConstructor()
332
            ->getMock();
333
        $repository
334
            ->method('getNodeByInternalName')
335
            ->willReturn($this->getRootNode());
336
337
        return $repository;
338
    }
339
340
    private function getRootNode()
341
    {
342
        $this->node = $this->createMock('Kunstmaan\NodeBundle\Entity\Node');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock('Kunst...eBundle\\Entity\\Node') of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\NodeBundle\Entity\Node> of property $node.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
343
344
        return $this->node;
345
    }
346
347
    private function getRequestStack($request)
348
    {
349
        $requestStack = $this->createMock('Symfony\Component\HttpFoundation\RequestStack');
350
        $requestStack->expects($this->any())->method('getMasterRequest')->willReturn($request);
351
352
        return $requestStack;
353
    }
354
355
    private function getUnknownDomainRequest()
356
    {
357
        return Request::create('http://unknown.tld/');
358
    }
359
360
    private function getMultiLanguageRequest()
361
    {
362
        return Request::create('http://multilangdomain.tld/');
363
    }
364
365
    private function getSingleLanguageRequest()
366
    {
367
        return Request::create('http://singlelangdomain.tld/');
368
    }
369
370
    private function getAliasedRequest()
371
    {
372
        return Request::create('http://single-alias.tld/');
373
    }
374
375
    private function getRequestWithOverride($uri)
376
    {
377
        $session = new Session(new MockArraySessionStorage());
378
        $session->set(DomainConfiguration::OVERRIDE_HOST, 'singlelangdomain.tld');
379
        $session->set(DomainConfiguration::SWITCH_HOST, 'multilangdomain.tld');
380
381
        $request = Request::create('http://multilangdomain.tld' . $uri);
382
        $request->setSession($session);
383
        $request->cookies->set($session->getName(), null);
384
385
        return $request;
386
    }
387
388
    private function getDomainConfiguration($request)
389
    {
390
        $hostMap = array(
391
            'multilangdomain.tld' => [
392
                'id' => 456,
393
                'host' => 'multilangdomain.tld',
394
                'protocol' => 'http',
395
                'type' => 'multi_lang',
396
                'default_locale' => 'en_GB',
397
                'locales' => ['nl' => 'nl_BE', 'fr' => 'fr_BE', 'en' => 'en_GB'],
398
                'reverse_locales' => ['nl_BE' => 'nl', 'fr_BE' => 'fr', 'en_GB' => 'en'],
399
                'root' => 'homepage_multi',
400
                'aliases' => ['multi-alias.tld'],
401
                'extra' => ['foo' => 'bar'],
402
                'locales_extra' => ['foo' => 'bar'],
403
            ],
404
            'singlelangdomain.tld' => [
405
                'id' => 123,
406
                'host' => 'singlelangdomain.tld',
407
                'type' => 'single_lang',
408
                'default_locale' => 'en_GB',
409
                'locales' => ['en' => 'en_GB'],
410
                'reverse_locales' => ['en_GB' => 'en'],
411
                'root' => 'homepage_single',
412
                'aliases' => ['single-alias.tld'],
413
            ],
414
        );
415
416
        $map = array(
417
            array('kunstmaan_admin.multi_language', false),
418
            array('kunstmaan_admin.default_locale', 'en'),
419
            array('kunstmaan_admin.required_locales', 'en'),
420
            array('kunstmaan_multi_domain.hosts', $hostMap),
421
        );
422
423
        return new DomainConfiguration($this->getContainer($map, $request));
424
    }
425
}
426