Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

NodeBundle/Tests/unit/Router/SlugRouterTest.php (8 issues)

Upgrade to new PHP Analysis Engine

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\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
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
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
$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
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
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 View Code Duplication
    private function getContainer($request, $multiLanguage = false, $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...
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
            ->will($this->returnValueMap($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
        $request = Request::create($url);
122
123
        return $request;
124
    }
125
126 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...
127
    {
128
        $em = $this->createMock('Doctrine\ORM\EntityManagerInterface');
129
        $em
130
            ->method('getRepository')
131
            ->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation'))
132
            ->willReturn($this->getNodeTranslationRepository($nodeTranslation));
133
134
        return $em;
135
    }
136
137 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...
138
    {
139
        $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository')
140
            ->disableOriginalConstructor()
141
            ->getMock();
142
        $repository
143
            ->method('getNodeTranslationForUrl')
144
            ->willReturn($nodeTranslation);
145
146
        return $repository;
147
    }
148
}
149