File: LocaleSwitcherTest.php

package info (click to toggle)
symfony 7.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,424 kB
  • sloc: php: 1,510,651; xml: 7,039; javascript: 979; sh: 586; makefile: 242; pascal: 70
file content (109 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Translation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Translation\LocaleSwitcher;
use Symfony\Contracts\Translation\LocaleAwareInterface;

#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('intl')]
class LocaleSwitcherTest extends TestCase
{
    private string $intlLocale;

    protected function setUp(): void
    {
        $this->intlLocale = \Locale::getDefault();
    }

    protected function tearDown(): void
    {
        \Locale::setDefault($this->intlLocale);
    }

    public function testCanSwitchLocale()
    {
        \Locale::setDefault('en');

        $service = new DummyLocaleAware('en');
        $switcher = new LocaleSwitcher('en', [$service]);

        $this->assertSame('en', \Locale::getDefault());
        $this->assertSame('en', $service->getLocale());
        $this->assertSame('en', $switcher->getLocale());

        $switcher->setLocale('fr');

        $this->assertSame('fr', \Locale::getDefault());
        $this->assertSame('fr', $service->getLocale());
        $this->assertSame('fr', $switcher->getLocale());
    }

    public function testCanSwitchLocaleForCallback()
    {
        \Locale::setDefault('en');

        $service = new DummyLocaleAware('en');
        $switcher = new LocaleSwitcher('en', [$service]);

        $this->assertSame('en', \Locale::getDefault());
        $this->assertSame('en', $service->getLocale());
        $this->assertSame('en', $switcher->getLocale());

        $switcher->runWithLocale('fr', function (string $locale) use ($switcher, $service) {
            $this->assertSame('fr', \Locale::getDefault());
            $this->assertSame('fr', $service->getLocale());
            $this->assertSame('fr', $switcher->getLocale());
            $this->assertSame('fr', $locale);
        });

        $this->assertSame('en', \Locale::getDefault());
        $this->assertSame('en', $service->getLocale());
        $this->assertSame('en', $switcher->getLocale());
    }

    public function testWithRequestContext()
    {
        $context = new RequestContext();
        $service = new LocaleSwitcher('en', [], $context);

        $this->assertSame('en', $service->getLocale());

        $service->setLocale('fr');

        $this->assertSame('fr', $service->getLocale());
        $this->assertSame('fr', $context->getParameter('_locale'));

        $service->reset();

        $this->assertSame('en', $service->getLocale());
        $this->assertSame('en', $context->getParameter('_locale'));
    }
}

class DummyLocaleAware implements LocaleAwareInterface
{
    public function __construct(private string $locale)
    {
    }

    public function setLocale(string $locale): void
    {
        $this->locale = $locale;
    }

    public function getLocale(): string
    {
        return $this->locale;
    }
}