1<?php 2 3declare(strict_types=1); 4 5namespace phpweb\Test\Unit; 6 7use phpweb\LangChooser; 8use PHPUnit\Framework; 9 10#[Framework\Attributes\CoversClass(LangChooser::class)] 11class LangChooserTest extends Framework\TestCase 12{ 13 private const DEFAULT_LANGUAGE_LIST = [ 14 'en' => 'English', 15 'de' => 'German', 16 'ja' => 'Japanese', 17 'pt_BR' => 'Brazilian Portuguese', 18 'zh' => 'Chinese (Simplified)', 19 ]; 20 21 public function testChooseCodeWithLangParameter(): void 22 { 23 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', '', 'en'); 24 $result = $langChooser->chooseCode('de', '/', null); 25 26 self::assertSame(['de', 'de'], $result); 27 } 28 29 public function testChooseCodeWithShortcutPath(): void 30 { 31 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 32 $result = $langChooser->chooseCode('', '/de/echo', null); 33 34 self::assertSame(['de', 'de'], $result); 35 } 36 37 #[Framework\Attributes\TestWith(['de', 'de'])] 38 #[Framework\Attributes\TestWith(['pt_BR', 'pt_BR'])] 39 public function testChooseCodeWithManualPath(string $pathLang, string $expected): void 40 { 41 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 42 $result = $langChooser->chooseCode('', "/manual/$pathLang", null); 43 44 self::assertSame([$expected, $expected], $result); 45 } 46 47 public function testChooseCodeWithUserPreference(): void 48 { 49 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], 'de', 'en'); 50 $result = $langChooser->chooseCode('', '/', null); 51 52 self::assertSame(['de', ''], $result); 53 } 54 55 public function testChooseCodeWithAcceptLanguage(): void 56 { 57 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 58 $result = $langChooser->chooseCode('', '/', 'de,ja,en'); 59 60 self::assertSame(['de', ''], $result); 61 } 62 63 public function testChooseCodeWithAcceptLanguageQuality(): void 64 { 65 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 66 $result = $langChooser->chooseCode('', '/', 'de;q=0.8,ja,en'); 67 68 self::assertSame(['ja', ''], $result); 69 } 70 71 #[Framework\Attributes\TestWith(['de-at', 'de'])] 72 #[Framework\Attributes\TestWith(['pt-br', 'pt_BR'])] 73 #[Framework\Attributes\TestWith(['zh-cn', 'zh'])] 74 #[Framework\Attributes\TestWith(['zh-tw', 'en'])] 75 public function testChooseCodeWithAcceptLanguageFollowedByCountryCode(string $acceptLanguage, string $expected): void 76 { 77 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 78 $result = $langChooser->chooseCode('', '/', $acceptLanguage); 79 80 self::assertSame([$expected, ''], $result); 81 } 82 83 public function testChooseCodeWithMirrorDefaultLanguage(): void 84 { 85 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'de'); 86 $result = $langChooser->chooseCode('', '/', null); 87 88 self::assertSame(['de', ''], $result); 89 } 90 91 public function testChooseCodeWithDefaultLanguage(): void 92 { 93 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'fr'); 94 $result = $langChooser->chooseCode('', '/', null); 95 96 self::assertSame(['en', ''], $result); 97 } 98 99 public function testChooseCodeWithLangParameterAndManualPath(): void 100 { 101 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 102 $result = $langChooser->chooseCode('de', '/manual/en', null); 103 104 self::assertSame(['de', 'de'], $result); 105 } 106 107 public function testChooseCodeWithManualPathAndUserPreference(): void 108 { 109 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], 'en', 'en'); 110 $result = $langChooser->chooseCode('', '/manual/de', null); 111 112 self::assertSame(['de', 'de'], $result); 113 } 114 115 public function testChooseCodeWithManualPathAndAcceptLanguage(): void 116 { 117 $langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en'); 118 $result = $langChooser->chooseCode('', '/manual/de', 'en'); 119 120 self::assertSame(['de', 'de'], $result); 121 } 122 123 public function testChooseCodeInactiveLanguageIsNotChosen(): void 124 { 125 $langChooser = new LangChooser(['en' => 'English', 'de' => 'German', 'pl' => 'Polish'], ['pl' => 'Polish'], '', ''); 126 $result = $langChooser->chooseCode('pl', '/manual/pl', 'pl'); 127 128 self::assertSame(['en', 'pl'], $result); 129 } 130} 131