Completed
Push — master ( 84107b...f0cb22 )
by Robbie
11s
created

DetectLocaleMiddlewareTest::testGetPersistKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace TractorCow\Fluent\Tests\Middleware;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
use TractorCow\Fluent\Extension\FluentDirectorExtension;
10
use TractorCow\Fluent\Model\Domain;
11
use TractorCow\Fluent\Model\Locale;
12
use TractorCow\Fluent\State\FluentState;
13
14
class DetectLocaleMiddlewareTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'DetectLocaleMiddlewareTest.yml';
17
18
    /**
19
     * @var DetectLocaleMiddleware
20
     */
21
    protected $middleware;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
        $this->middleware = new Stub\DetectLocaleMiddlewareSpy;
0 ignored issues
show
Documentation Bug introduced by
It seems like new TractorCow\Fluent\Te...ctLocaleMiddlewareSpy() of type TractorCow\Fluent\Tests\...tectLocaleMiddlewareSpy is incompatible with the declared type TractorCow\Fluent\Tests\...\DetectLocaleMiddleware of property $middleware.

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...
27
28
        Config::modify()->set(FluentDirectorExtension::class, 'query_param', 'l');
29
30
        // Enable localedetection
31
        FluentDirectorExtension::config()->set('detect_locale', true);
32
33
        // Clear cache
34
        Locale::clearCached();
35
        Domain::clearCached();
36
    }
37
38
    public function testGetPersistKey()
39
    {
40
        $state = FluentState::singleton();
41
        $state->setIsFrontend(true);
42
        $this->assertSame('FluentLocale', $this->middleware->getPersistKey());
43
44
        $state->setIsFrontend(false);
45
        $this->assertSame('FluentLocale_CMS', $this->middleware->getPersistKey());
46
    }
47
48
    /**
49
     * @dataProvider localePriorityProvider
50
     */
51
    public function testGetLocalePriority($url, $routeParams, $queryParams, $persisted, $header, $expected)
52
    {
53
        $request = new HTTPRequest('GET', $url, $queryParams);
54
        $request->setRouteParams($routeParams);
55
        $request->setSession(Controller::curr()->getRequest()->getSession());
56
        $this->middleware->setPersistLocale($request, null);
57
58
        if ($persisted) {
59
            $this->middleware->setPersistLocale($request, $persisted);
60
        }
61
        if ($header) {
62
            $request->addHeader('Accept-Language', $header);
63
        }
64
65
        $this->assertSame($expected, $this->middleware->getLocale($request));
66
    }
67
68
    /**
69
     * @return array[] List of tests with arguments: $url, $routeParams, $queryParams, $persisted, $header, $expected
70
     */
71
    public function localePriorityProvider()
72
    {
73
        return [/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
            // First priority: controller routing params
75
            ['/nz/foo', ['l' => 'en_NZ'], ['l' => 'en_AU'], 'fr_FR', 'es-US', 'en_NZ'],
76
            // Second priority: request params
77
            ['/foo', [], ['l' => 'en_AU'], 'fr_FR', 'es-US', 'en_AU'],
78
            // Third priority: persisted locale
79
            ['/foo', [], [], 'fr_FR', 'es-US', 'fr_FR'],
80
            // Default to the default locale when not on the homepage
81
            ['/foo', [], [], null, 'es-US', 'es_ES'],*/
82
            // Home page only - fourth priority is request header
83
            ['/', [], [], null, 'es-US', 'es_US'],
84
        ];
85
    }
86
87
    public function testLocaleIsAlwaysPersistedEvenIfNotSetByTheMiddleware()
88
    {
89
        $request = new HTTPRequest('GET', '/');
90
        FluentState::singleton()->setLocale('dummy');
91
92
        $middleware = $this->getMockBuilder(Stub\DetectLocaleMiddlewareSpy::class)
93
            ->setMethods(['getLocale', 'setPersistLocale'])
94
            ->getMock();
95
96
        $middleware->expects($this->never())->method('getLocale');
97
        $middleware->expects($this->once())->method('setPersistLocale')->with($request, 'dummy');
0 ignored issues
show
Bug introduced by
'dummy' of type string is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $middleware->expects($this->once())->method('setPersistLocale')->with($request, /** @scrutinizer ignore-type */ 'dummy');
Loading history...
Bug introduced by
$request of type SilverStripe\Control\HTTPRequest is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $middleware->expects($this->once())->method('setPersistLocale')->with(/** @scrutinizer ignore-type */ $request, 'dummy');
Loading history...
98
99
        $middleware->process($request, function () {
100
            // no-op
101
        });
102
    }
103
}
104