|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
-
- declare(strict_types=1);
-
- namespace OrpheusNET\Logchecker\Parser\EAC;
-
- use FilesystemIterator;
- use OrpheusNET\Logchecker\Exception\UnknownLanguageException;
- use OrpheusNET\Logchecker\Util;
- use PHPUnit\Framework\TestCase;
-
- class TranslatorTest extends TestCase
- {
- public function foreignLogDataProvider()
- {
- $logs = [];
- $logPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'utf8']);
- foreach (new FilesystemIterator($logPath, FilesystemIterator::SKIP_DOTS) as $dir) {
- if ($dir->isFile()) {
- continue;
- }
- foreach (new FilesystemIterator($dir->getPathname(), FilesystemIterator::SKIP_DOTS) as $logFiles) {
- if (preg_match("/[0-9]+\.log/", $logFiles->getFilename())) {
- $logs[] = [$dir->getFilename(), $logFiles->getPathname()];
- }
- }
- }
- return $logs;
- }
-
- /**
- * @dataProvider foreignLogDataProvider
- */
- public function testTranslateLog($language, $logPath)
- {
- $log = file_get_contents($logPath);
- $translatedLogPath = str_replace('.log', '_en.log', $logPath);
- $langDetails = Translator::getLanguage($log);
- $this->assertSame($language, $langDetails['code']);
- $this->assertNotNull($langDetails['name']);
- $this->assertNotNull($langDetails['name_english']);
- $this->assertStringEqualsFile($translatedLogPath, Translator::translate($log, $language));
- }
-
- public function testInvalidLanguage()
- {
- $this->expectException(UnknownLanguageException::class);
- $this->expectExceptionMessage('Could not determine language of EAC log');
- Translator::getLanguage(
- file_get_contents(
- implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'xld', 'originals', 'xld_perfect.log'])
- )
- );
- }
-
- public function englishLogProvider()
- {
- return array_map(
- function ($file) {
- return [$file];
- },
- array_filter(
- scandir(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'originals'])),
- function ($file) {
- return substr($file, 0, 2) === 'en';
- }
- )
- );
- }
-
- /**
- * @dataProvider englishLogProvider
- */
- public function testEnglishLanguage(string $file)
- {
- $logPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'originals', $file]);
- $log = file_get_contents($logPath);
- $log = Util::decodeEncoding($log, $logPath);
- $langDetails = Translator::getLanguage($log);
- $this->assertSame('en', $langDetails['code']);
- $this->assertSame('English', $langDetails['name']);
- $this->assertSame('English', $langDetails['name_english']);
- }
- }
|