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

DomainConfigurationTest   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 413
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 9
dl 20
loc 413
rs 8.8798
c 0
b 0
f 0

44 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetHostWithMultiLanguage() 0 6 1
A testGetHostWithSingleLanguage() 0 6 1
A testGetHostWithAlias() 0 6 1
A testGetHostWithOverrideOnFrontend() 0 6 1
A testGetHostWithOverrideOnBackend() 0 6 1
A testHostOverrideReturnsNull() 0 10 1
A testGetHosts() 0 6 1
A testGetDefaultLocale() 0 6 1
A testGetDefaultLocaleWithUnknownDomain() 0 6 1
A testGetExtraDataWithoutDataSet() 0 6 1
A testGetExtraDataWithDataSet() 0 6 1
A testGetRootNode() 0 6 1
A testGetRootNodeWithUnknown() 0 6 1
A testIsMultiDomainHostWithSingleLanguage() 0 6 1
A testIsMultiDomainHostWithMultiLanguage() 0 6 1
A testIsMultiDomainHostWithUnknown() 0 6 1
A testIsMultiLanguageWithSingleLanguage() 0 6 1
A testIsMultiLanguageWithMultiLanguage() 0 6 1
A testIsMultiLanguageWithUnknown() 0 6 1
A testGetFrontendLocalesWithSingleLanguage() 0 6 1
A testGetHostBaseUrl() 0 6 1
A testGetFullHost() 0 6 1
A testGetLocalesExtraData() 0 11 1
A testGetFullHostConfig() 0 8 1
A testHasHostSwitched() 0 6 1
A testGetHostSwitched() 0 10 1
A testFullHostById() 0 7 1
A testGetFrontendLocalesWithMultiLanguage() 0 6 1
A testGetFrontendLocalesWithUnknown() 0 6 1
A testGetBackendLocalesWithSingleLanguage() 0 6 1
A testGetBackendLocalesWithMultiLanguage() 0 6 1
A testGetBackendLocalesWithUnknown() 0 6 1
A getEntityManager() 0 10 1
A getAdminRouteHelper() 0 18 1
A getNodeRepository() 0 11 1
A getRootNode() 0 6 1
A getRequestStack() 0 7 1
A getContainer() 20 20 1
A getUnknownDomainRequest() 0 4 1
A getMultiLanguageRequest() 0 4 1
A getSingleLanguageRequest() 0 4 1
A getAliasedRequest() 0 4 1
A getRequestWithOverride() 0 12 1
A getDomainConfiguration() 0 37 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DomainConfigurationTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DomainConfigurationTest, and based on these observations, apply Extract Interface, too.

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 Wim Vandersmissen
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 Ruud Denivel
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