88
99namespace App \Tests \Integration \Service ;
1010
11+ use App \Enum \Language ;
12+ use App \Enum \Locale ;
1113use App \Service \Localization ;
14+ use DateTimeZone ;
1215use Exception ;
1316use PHPUnit \Framework \Attributes \TestDox ;
14- use PHPUnit \Framework \MockObject \MockObject ;
1517use Psr \Log \LoggerInterface ;
1618use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
19+ use Symfony \Component \HttpFoundation \Request ;
20+ use Symfony \Component \HttpFoundation \RequestStack ;
1721use 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 */
2328class 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