Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Tests/unit/Helper/DomainConfigurationTest.php (1 issue)

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\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
    private function getEntityManager()
278
    {
279
        $em = $this->createMock('Doctrine\ORM\EntityManagerInterface');
280
        $em
281
            ->method('getRepository')
282
            ->with($this->equalTo(Node::class))
283
            ->willReturn($this->getNodeRepository());
284
285
        return $em;
286
    }
287
288
    private function getAdminRouteHelper()
289
    {
290
        $adminRouteReturnValueMap = [
291
            ['/frontend-uri', false],
292
            ['/nl/admin/backend-uri', true],
293
            ['/admin/somewhere', true],
294
        ];
295
296
        $adminRouteHelper = $this->getMockBuilder('Kunstmaan\AdminBundle\Helper\AdminRouteHelper')
297
            ->disableOriginalConstructor()
298
            ->getMock();
299
        $adminRouteHelper
300
            ->expects($this->any())
301
            ->method('isAdminRoute')
302
            ->willReturnMap($adminRouteReturnValueMap);
303
304
        return $adminRouteHelper;
305
    }
306
307
    private function getNodeRepository()
308
    {
309
        $this->node = $this->createMock('Kunstmaan\NodeBundle\Entity\Node');
0 ignored issues
show
Documentation Bug introduced by
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...
310
311
        $repository = $this->getMockBuilder('Kunstmaan\NodeBundle\Repository\NodeRepository')
312
            ->disableOriginalConstructor()
313
            ->getMock();
314
        $repository
315
            ->method('getNodeByInternalName')
316
            ->willReturn($this->node);
317
318
        return $repository;
319
    }
320
321
    private function getUnknownDomainRequest()
322
    {
323
        return Request::create('http://unknown.tld/');
324
    }
325
326
    private function getMultiLanguageRequest()
327
    {
328
        return Request::create('http://multilangdomain.tld/');
329
    }
330
331
    private function getSingleLanguageRequest()
332
    {
333
        return Request::create('http://singlelangdomain.tld/');
334
    }
335
336
    private function getAliasedRequest()
337
    {
338
        return Request::create('http://single-alias.tld/');
339
    }
340
341
    private function getRequestWithOverride($uri)
342
    {
343
        $session = new Session(new MockArraySessionStorage());
344
        $session->set(DomainConfiguration::OVERRIDE_HOST, 'singlelangdomain.tld');
345
        $session->set(DomainConfiguration::SWITCH_HOST, 'multilangdomain.tld');
346
347
        $request = Request::create('http://multilangdomain.tld' . $uri);
348
        $request->setSession($session);
349
        $request->cookies->set($session->getName(), null);
350
351
        return $request;
352
    }
353
354
    private function getDomainConfiguration($request)
355
    {
356
        $hostMap = array(
357
            'multilangdomain.tld' => [
358
                'id' => 456,
359
                'host' => 'multilangdomain.tld',
360
                'protocol' => 'http',
361
                'type' => 'multi_lang',
362
                'default_locale' => 'en_GB',
363
                'locales' => ['nl' => 'nl_BE', 'fr' => 'fr_BE', 'en' => 'en_GB'],
364
                'reverse_locales' => ['nl_BE' => 'nl', 'fr_BE' => 'fr', 'en_GB' => 'en'],
365
                'root' => 'homepage_multi',
366
                'aliases' => ['multi-alias.tld'],
367
                'extra' => ['foo' => 'bar'],
368
                'locales_extra' => ['foo' => 'bar'],
369
            ],
370
            'singlelangdomain.tld' => [
371
                'id' => 123,
372
                'host' => 'singlelangdomain.tld',
373
                'type' => 'single_lang',
374
                'default_locale' => 'en_GB',
375
                'locales' => ['en' => 'en_GB'],
376
                'reverse_locales' => ['en_GB' => 'en'],
377
                'root' => 'homepage_single',
378
                'aliases' => ['single-alias.tld'],
379
            ],
380
        );
381
382
        $requestStack = $this->createMock('Symfony\Component\HttpFoundation\RequestStack');
383
        $requestStack->method('getMasterRequest')->willReturn($request);
384
385
        return new DomainConfiguration($requestStack, false, 'en', 'en', $this->getAdminRouteHelper(), $this->getEntityManager(), $hostMap);
386
    }
387
}
388