Skip to content

Commit 3bb9924

Browse files
authored
Merge pull request #2932 from tarlepp/feat/request-locale
Feat - Added support to get request locale
2 parents ef3aa4a + 2a548eb commit 3bb9924

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

src/Service/Localization.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,27 @@
1414
use DateTimeImmutable;
1515
use DateTimeZone;
1616
use Psr\Log\LoggerInterface;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1718
use Symfony\Contracts\Cache\CacheInterface;
1819
use Symfony\Contracts\Cache\ItemInterface;
1920
use Throwable;
2021
use function explode;
2122
use function floor;
23+
use function in_array;
2224
use function str_replace;
2325

2426
/**
2527
* @package App\Service
2628
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
2729
*/
28-
class Localization
30+
readonly class Localization
2931
{
3032
final public const string DEFAULT_TIMEZONE = 'Europe/Helsinki';
3133

3234
public function __construct(
33-
private readonly CacheInterface $appCacheApcu,
34-
private readonly LoggerInterface $logger,
35+
private CacheInterface $appCacheApcu,
36+
private LoggerInterface $logger,
37+
private RequestStack $requestStack,
3538
) {
3639
}
3740

@@ -51,6 +54,13 @@ public function getLocales(): array
5154
return Locale::getValues();
5255
}
5356

57+
public function getRequestLocale(): string
58+
{
59+
$locale = $this->requestStack->getCurrentRequest()?->getLocale() ?? Locale::getDefault()->value;
60+
61+
return in_array($locale, $this->getLocales(), true) ? $locale : Locale::getDefault()->value;
62+
}
63+
5464
/**
5565
* @return array<int, array{timezone: string, identifier: string, offset: string, value: string}>
5666
*/

tests/Integration/Service/LocalizationTest.php

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,63 @@
88

99
namespace App\Tests\Integration\Service;
1010

11+
use App\Enum\Language;
12+
use App\Enum\Locale;
1113
use App\Service\Localization;
14+
use DateTimeZone;
1215
use Exception;
1316
use PHPUnit\Framework\Attributes\TestDox;
14-
use PHPUnit\Framework\MockObject\MockObject;
1517
use Psr\Log\LoggerInterface;
1618
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\RequestStack;
1721
use Symfony\Contracts\Cache\CacheInterface;
22+
use function count;
1823

1924
/**
2025
* @package App\Tests\Integration\Service
2126
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
2227
*/
2328
class LocalizationTest extends KernelTestCase
2429
{
25-
#[TestDox('Test that `LoggerInterface::error` method is called when `CacheInterface')]
30+
#[TestDox('Test that `getLanguages` returns expected')]
31+
public function testThatGetLanguagesReturnsExpected(): void
32+
{
33+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
34+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
35+
$requestStack = new RequestStack();
36+
37+
$expected = Language::getValues();
38+
39+
self::assertSame(
40+
$expected,
41+
(new Localization($cache, $logger, $requestStack))->getLanguages(),
42+
);
43+
}
44+
45+
#[TestDox('Test that `getLocales` returns expected')]
46+
public function testThatGetLocalesReturnsExpected(): void
47+
{
48+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
49+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
50+
$requestStack = new RequestStack();
51+
52+
$expected = Locale::getValues();
53+
54+
self::assertSame(
55+
$expected,
56+
(new Localization($cache, $logger, $requestStack))->getLocales(),
57+
);
58+
}
59+
60+
#[TestDox('Test that `LoggerInterface::error` method is called when `CacheInterface` throws an exception')]
2661
public function testThatLoggerIsCalledWhenCacheThrowsAnException(): void
2762
{
2863
$exception = new Exception('test exception');
2964

30-
$cache = $this->getCache();
31-
$logger = $this->getLogger();
65+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
66+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
67+
$requestStack = new RequestStack();
3268

3369
$cache
3470
->expects($this->once())
@@ -40,23 +76,59 @@ public function testThatLoggerIsCalledWhenCacheThrowsAnException(): void
4076
->method('error')
4177
->with($exception->getMessage(), $exception->getTrace());
4278

43-
(new Localization($cache, $logger))
79+
(new Localization($cache, $logger, $requestStack))
4480
->getTimezones();
4581
}
4682

47-
/**
48-
* @phpstan-return MockObject&CacheInterface
49-
*/
50-
private function getCache(): MockObject
83+
#[TestDox('Test that `getFormattedTimezones` method returns expected amount of results')]
84+
public function testThatGetFormattedTimezonesMethodReturnsExpectedAmountOfResults(): void
5185
{
52-
return $this->getMockBuilder(CacheInterface::class)->getMock();
86+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
87+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
88+
$requestStack = new RequestStack();
89+
90+
$output = (new Localization($cache, $logger, $requestStack))
91+
->getFormattedTimezones();
92+
93+
self::assertCount(count(DateTimeZone::listIdentifiers()), $output);
5394
}
5495

55-
/**
56-
* @phpstan-return MockObject&LoggerInterface
57-
*/
58-
private function getLogger(): MockObject
96+
#[TestDox('Test that `getRequestLocale` method returns expected locale when request is not set')]
97+
public function testThatGetRequestLocaleReturnsDefaultLocaleIfThereIsNoCurrentRequest(): void
5998
{
60-
return $this->getMockBuilder(LoggerInterface::class)->getMock();
99+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
100+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
101+
$requestStack = new RequestStack();
102+
103+
$cache
104+
->expects($this->never())
105+
->method('get');
106+
107+
$logger
108+
->expects($this->never())
109+
->method('error');
110+
111+
self::assertSame(
112+
Locale::getDefault()->value,
113+
(new Localization($cache, $logger, $requestStack))->getRequestLocale(),
114+
);
115+
}
116+
117+
#[TestDox('Test that `getRequestLocale` method returns expected locale when request is set')]
118+
public function testThatGetRequestLocaleReturnsDefaultLocaleWhenThereIsRequest(): void
119+
{
120+
$cache = $this->getMockBuilder(CacheInterface::class)->getMock();
121+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
122+
123+
$request = new Request();
124+
$request->setLocale('en');
125+
126+
$requestStack = new RequestStack();
127+
$requestStack->push($request);
128+
129+
self::assertSame(
130+
'en',
131+
(new Localization($cache, $logger, $requestStack))->getRequestLocale(),
132+
);
61133
}
62134
}

0 commit comments

Comments
 (0)