Completed
Push — 5.0 ( 5d9832...217db4 )
by Kristof
96:00 queued 83:47
created

Tests/Router/DomainBasedLocaleRouterTest.php (2 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\MultiDomainBundle\Tests\Router;
4
5
use Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter;
6
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
10
class DomainBasedLocaleRouterTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * Sets up the fixture, for example, opens a network connection.
14
     * This method is called before a test is executed.
15
     */
16
    protected function setUp()
17
    {
18
    }
19
20
    /**
21
     * Tears down the fixture, for example, closes a network connection.
22
     * This method is called after a test is executed.
23
     */
24
    protected function tearDown()
25
    {
26
    }
27
28
    /**
29
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::generate
30
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getRequestLocale
31
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::isMultiDomainHost
32
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getReverseLocaleMap
33
     */
34 View Code Duplication
    public function testGenerate()
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...
35
    {
36
        $request = $this->getRequest();
37
        $container = $this->getContainer($request);
38
        $object = new DomainBasedLocaleRouter($container);
39
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en_GB'), UrlGeneratorInterface::ABSOLUTE_URL);
40
        $this->assertEquals('http://multilangdomain.tld/en/some-uri', $url);
41
42
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en_GB'), UrlGeneratorInterface::ABSOLUTE_PATH);
43
        $this->assertEquals('/en/some-uri', $url);
44
    }
45
46
    /**
47
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::generate
48
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getRequestLocale
49
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::isMultiDomainHost
50
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getReverseLocaleMap
51
     */
52 View Code Duplication
    public function testGenerateWithLocaleBasedOnCurrentRequest()
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...
53
    {
54
        $request = $this->getRequest();
55
        $request->setLocale('nl_BE');
56
        $container = $this->getContainer($request);
57
        $object = new DomainBasedLocaleRouter($container);
58
        $url = $object->generate('_slug', array('url' => 'some-uri'), UrlGeneratorInterface::ABSOLUTE_URL);
59
        $this->assertEquals('http://multilangdomain.tld/nl/some-uri', $url);
60
61
        $url = $object->generate('_slug', array('url' => 'some-uri'), UrlGeneratorInterface::ABSOLUTE_PATH);
62
        $this->assertEquals('/nl/some-uri', $url);
63
    }
64
65
    /**
66
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::match
67
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getNodeTranslation
68
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::getLocaleMap
69
     */
70 View Code Duplication
    public function testMatchWithNodeTranslation()
71
    {
72
        $request = $this->getRequest();
73
        $nodeTranslation = new NodeTranslation();
74
        $container = $this->getContainer($request, $nodeTranslation);
75
        $object = new DomainBasedLocaleRouter($container);
76
        $result = $object->match('/en/some-uri');
77
        $this->assertEquals('some-uri', $result['url']);
78
        $this->assertEquals('en_GB', $result['_locale']);
79
        $this->assertEquals($nodeTranslation, $result['_nodeTranslation']);
80
    }
81
82
    /**
83
     * @covers \Kunstmaan\MultiDomainBundle\Router\DomainBasedLocaleRouter::match
84
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
85
     */
86 View Code Duplication
    public function testMatchWithoutNodeTranslation()
87
    {
88
        $request = $this->getRequest();
89
        $container = $this->getContainer($request);
90
        $object = new DomainBasedLocaleRouter($container);
91
        $object->match('/en/some-uri');
92
    }
93
94 View Code Duplication
    private function getContainer($request, $nodeTranslation = null)
95
    {
96
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
97
        $serviceMap = array(
98
            array('request_stack', 1, $this->getRequestStack($request)),
99
            array('kunstmaan_admin.domain_configuration', 1, $this->getDomainConfiguration()),
100
            array('doctrine.orm.entity_manager', 1, $this->getEntityManager($nodeTranslation)),
101
        );
102
103
        $container
104
            ->method('get')
105
            ->will($this->returnValueMap($serviceMap));
106
107
        return $container;
108
    }
109
110
    private function getRequestStack($request)
111
    {
112
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
113
        $requestStack->expects($this->any())->method('getMasterRequest')->willReturn($request);
114
115
        return $requestStack;
116
    }
117
118
    private function getDomainConfiguration()
119
    {
120
        $domainConfiguration = $this->getMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface');
121
        $domainConfiguration->method('getHost')
122
            ->willReturn('override-domain.tld');
123
124
        $domainConfiguration->method('isMultiDomainHost')
125
            ->willReturn(true);
126
127
        $domainConfiguration->method('isMultiLanguage')
128
            ->willReturn(true);
129
130
        $domainConfiguration->method('getDefaultLocale')
131
            ->willReturn('nl_BE');
132
133
        $domainConfiguration->method('getFrontendLocales')
134
            ->willReturn(array('nl', 'en'));
135
136
        $node = $this->getMock('Kunstmaan\NodeBundle\Entity\Node');
137
        $domainConfiguration->method('getRootNode')
138
            ->willReturn($node);
139
140
        $domainConfiguration->method('getBackendLocales')
141
            ->willReturn(array('nl_BE', 'en_GB'));
142
143
        return $domainConfiguration;
144
    }
145
146
    private function getRequest($url = 'http://multilangdomain.tld/')
147
    {
148
        $request = Request::create($url);
149
150
        return $request;
151
    }
152
153 View Code Duplication
    private function getEntityManager($nodeTranslation = null)
154
    {
155
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
156
        $em
157
            ->method('getRepository')
158
            ->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation'))
159
            ->willReturn($this->getNodeTranslationRepository($nodeTranslation));
160
161
        return $em;
162
    }
163
164 View Code Duplication
    private function getNodeTranslationRepository($nodeTranslation = null)
165
    {
166
        $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository')
167
            ->disableOriginalConstructor()
168
            ->getMock();
169
        $repository
170
            ->method('getNodeTranslationForUrl')
171
            ->willReturn($nodeTranslation);
172
173
        return $repository;
174
    }
175
}
176