|
| 1 | +<?php |
| 2 | +use PHPUnit\Framework\TestCase; |
| 3 | +use GetAnyMessage\Locales\Lang; |
| 4 | + |
| 5 | +class LangTest extends TestCase |
| 6 | +{ |
| 7 | + private string $testDataDir; |
| 8 | + |
| 9 | + protected function setUp(): void |
| 10 | + { |
| 11 | + parent::setUp(); |
| 12 | + $this->testDataDir = __DIR__ . '/data_test'; |
| 13 | + if (!file_exists($this->testDataDir)) { |
| 14 | + mkdir($this->testDataDir, 0777, true); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + parent::tearDown(); |
| 21 | + $this->deleteDir($this->testDataDir); |
| 22 | + } |
| 23 | + |
| 24 | + private function deleteDir(string $dir): void |
| 25 | + { |
| 26 | + if (!file_exists($dir)) return; |
| 27 | + $files = array_diff(scandir($dir), ['.', '..']); |
| 28 | + foreach ($files as $file) { |
| 29 | + $path = "$dir/$file"; |
| 30 | + is_dir($path) ? $this->deleteDir($path) : unlink($path); |
| 31 | + } |
| 32 | + rmdir($dir); |
| 33 | + } |
| 34 | + |
| 35 | + private function getLangInstance(): Lang |
| 36 | + { |
| 37 | + $contextMock = new class($this->testDataDir) { |
| 38 | + public string $dataPath; |
| 39 | + public function __construct(string $path) { $this->dataPath = $path; } |
| 40 | + }; |
| 41 | + |
| 42 | + return new class($contextMock) extends Lang { |
| 43 | + private object $mockContext; |
| 44 | + public function __construct(object $context) |
| 45 | + { |
| 46 | + parent::__construct($context); |
| 47 | + $this->mockContext = $context; |
| 48 | + } |
| 49 | + public function getUserLang(int $senderId): string |
| 50 | + { |
| 51 | + $path = $this->mockContext->dataPath . "/$senderId/lang.txt"; |
| 52 | + if (file_exists($path)) { |
| 53 | + return trim(file_get_contents($path)); |
| 54 | + } |
| 55 | + return 'en'; |
| 56 | + } |
| 57 | + public function setUserLang(int $senderId, string $lang): bool |
| 58 | + { |
| 59 | + $dir = $this->mockContext->dataPath . "/$senderId"; |
| 60 | + if (!file_exists($dir)) mkdir($dir, 0777, true); |
| 61 | + return file_put_contents("$dir/lang.txt", $lang) !== false; |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + public function testDefaultLangIsEnglish() |
| 67 | + { |
| 68 | + $lang = $this->getLangInstance(); |
| 69 | + $this->assertEquals('en', $lang->getUserLang(999999)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testSetAndGetUserLang() |
| 73 | + { |
| 74 | + $lang = $this->getLangInstance(); |
| 75 | + $senderId = 999999; |
| 76 | + $this->assertTrue($lang->setUserLang($senderId, 'fr')); |
| 77 | + $this->assertEquals('fr', $lang->getUserLang($senderId)); |
| 78 | + } |
| 79 | + |
| 80 | + public function testLoadTranslationsFallsBackToEnglish() |
| 81 | + { |
| 82 | + $lang = $this->getLangInstance(); |
| 83 | + $translations = $lang->loadTranslations('nonexistent'); |
| 84 | + $this->assertArrayHasKey('language_name', $translations); |
| 85 | + } |
| 86 | + |
| 87 | + public function testGetAvailableLanguages() |
| 88 | + { |
| 89 | + $lang = $this->getLangInstance(); |
| 90 | + $langs = $lang->getAvailableLanguages(); |
| 91 | + $this->assertIsArray($langs); |
| 92 | + $this->assertNotEmpty($langs); |
| 93 | + } |
| 94 | + |
| 95 | + public function testGetLanguageButtons() |
| 96 | + { |
| 97 | + $lang = $this->getLangInstance(); |
| 98 | + $buttons = $lang->getLanguageButtons(); |
| 99 | + $this->assertArrayHasKey('inline_keyboard', $buttons); |
| 100 | + } |
| 101 | +} |
0 commit comments