Completed
Push — master ( 74fc07...2d0c85 )
by Jeroen
132:12 queued 125:10
created

SlugRouterTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 45.93 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 62
loc 135
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testGenerateMultiLanguage() 11 11 1
A testGenerateSingleLanguage() 11 11 1
A testSetContext() 0 8 1
A testMatchWithNodeTranslation() 11 11 1
A testMatchWithoutNodeTranslation() 8 8 1
A getRequestStack() 0 7 1
A getDomainConfiguration() 0 26 3
A getContainer() 0 15 1
A getRequest() 0 4 1
A getEntityManager() 10 10 1
A getNodeTranslationRepository() 11 11 1

How to fix   Duplicated Code   

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:

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 View Code Duplication
    public function testGenerateMultiLanguage()
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...
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 View Code Duplication
    public function testGenerateSingleLanguage()
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...
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);
0 ignored issues
show
Documentation introduced by
$context is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Routing\RequestContext>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $this->assertEquals($context, $object->getContext());
45
    }
46
47 View Code Duplication
    public function testMatchWithNodeTranslation()
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...
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()
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...
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
    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)
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...
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)
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...
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