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

NodeBundle/Tests/Router/SlugRouterTest.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\NodeBundle\Tests\Router;
4
5
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
6
use Kunstmaan\NodeBundle\Router\SlugRouter;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
10
class SlugRouterTest 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\NodeBundle\Router\SlugRouter::__construct
30
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::generate
31
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getRouteCollection
32
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::addPreviewRoute
33
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getPreviewRouteParameters
34
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getEscapedLocales
35
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::addSlugRoute
36
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getSlugRouteParameters
37
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getContext
38
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getDefaultLocale
39
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::isMultiLanguage
40
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getBackendLocales
41
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getFrontendLocales
42
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::addRoute
43
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getSlugPattern
44
     */
45 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...
46
    {
47
        $request = $this->getRequest();
48
        $container = $this->getContainer($request, true);
49
        $object = new SlugRouter($container);
50
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en'), UrlGeneratorInterface::ABSOLUTE_URL);
51
        $this->assertEquals('http://domain.tld/en/some-uri', $url);
52
53
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'en'), UrlGeneratorInterface::ABSOLUTE_PATH);
54
        $this->assertEquals('/en/some-uri', $url);
55
    }
56
57
    /**
58
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::generate
59
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getRouteCollection
60
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getContext
61
     */
62 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...
63
    {
64
        $request = $this->getRequest();
65
        $container = $this->getContainer($request);
66
        $object = new SlugRouter($container);
67
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'nl'), UrlGeneratorInterface::ABSOLUTE_URL);
68
        $this->assertEquals('http://domain.tld/some-uri', $url);
69
70
        $url = $object->generate('_slug', array('url' => 'some-uri', '_locale' => 'nl'), UrlGeneratorInterface::ABSOLUTE_PATH);
71
        $this->assertEquals('/some-uri', $url);
72
    }
73
74
    /**
75
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::setContext
76
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getContext
77
     */
78
    public function testSetContext()
79
    {
80
        $context = $this->getMock('Symfony\Component\Routing\RequestContext');
81
        $container = $this->getContainer(null);
82
        $object = new SlugRouter($container);
83
        $object->setContext($context);
84
        $this->assertEquals($context, $object->getContext());
85
    }
86
87
    /**
88
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::match
89
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getRouteCollection
90
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getContext
91
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getNodeTranslation
92
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getMasterRequest
93
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::getNodeTranslationRepository
94
     */
95 View Code Duplication
    public function testMatchWithNodeTranslation()
96
    {
97
        $request = $this->getRequest();
98
        $nodeTranslation = new NodeTranslation();
99
        $container = $this->getContainer($request, true, $nodeTranslation);
100
        $object = new SlugRouter($container);
101
        $result = $object->match('/en/some-uri');
102
        $this->assertEquals('some-uri', $result['url']);
103
        $this->assertEquals('en', $result['_locale']);
104
        $this->assertEquals($nodeTranslation, $result['_nodeTranslation']);
105
    }
106
107
    /**
108
     * @covers \Kunstmaan\NodeBundle\Router\SlugRouter::match
109
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
110
     */
111 View Code Duplication
    public function testMatchWithoutNodeTranslation()
112
    {
113
        $request = $this->getRequest();
114
        $container = $this->getContainer($request);
115
        $object = new SlugRouter($container);
116
        $object->match('/en/some-uri');
117
    }
118
119 View Code Duplication
    private function getContainer($request, $multiLanguage = false, $nodeTranslation = null)
120
    {
121
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
122
        $serviceMap = array(
123
            array('request_stack', 1, $this->getRequestStack($request)),
124
            array('kunstmaan_admin.domain_configuration', 1, $this->getDomainConfiguration($multiLanguage)),
125
            array('doctrine.orm.entity_manager', 1, $this->getEntityManager($nodeTranslation)),
126
        );
127
128
        $container
129
            ->method('get')
130
            ->will($this->returnValueMap($serviceMap));
131
132
        return $container;
133
    }
134
135
    private function getRequestStack($request)
136
    {
137
        $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
138
        $requestStack->expects($this->any())->method('getMasterRequest')->willReturn($request);
139
140
        return $requestStack;
141
    }
142
143
    private function getDomainConfiguration($multiLanguage = false)
144
    {
145
        $domainConfiguration = $this->getMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface');
146
        $domainConfiguration->method('getHost')
147
            ->willReturn('domain.tld');
148
149
        $domainConfiguration->method('isMultiDomainHost')
150
            ->willReturn(false);
151
152
        $domainConfiguration->method('isMultiLanguage')
153
            ->willReturn($multiLanguage);
154
155
        $domainConfiguration->method('getDefaultLocale')
156
            ->willReturn('nl');
157
158
        $domainConfiguration->method('getFrontendLocales')
159
            ->willReturn($multiLanguage ? array('nl', 'en') : array('nl'));
160
161
        $domainConfiguration->method('getBackendLocales')
162
            ->willReturn($multiLanguage ? array('nl', 'en') : array('nl'));
163
164
        $domainConfiguration->method('getRootNode')
165
            ->willReturn(null);
166
167
        return $domainConfiguration;
168
    }
169
170
    private function getRequest($url = 'http://domain.tld/')
171
    {
172
        $request = Request::create($url);
173
174
        return $request;
175
    }
176
177 View Code Duplication
    private function getEntityManager($nodeTranslation = null)
178
    {
179
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
180
        $em
181
            ->method('getRepository')
182
            ->with($this->equalTo('KunstmaanNodeBundle:NodeTranslation'))
183
            ->willReturn($this->getNodeTranslationRepository($nodeTranslation));
184
185
        return $em;
186
    }
187
188 View Code Duplication
    private function getNodeTranslationRepository($nodeTranslation = null)
189
    {
190
        $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeTranslationRepository')
191
            ->disableOriginalConstructor()
192
            ->getMock();
193
        $repository
194
            ->method('getNodeTranslationForUrl')
195
            ->willReturn($nodeTranslation);
196
197
        return $repository;
198
    }
199
}
200