Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TranslatorTest.php 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. namespace OrpheusNET\Logchecker\Parser\EAC;
  4. use FilesystemIterator;
  5. use OrpheusNET\Logchecker\Exception\UnknownLanguageException;
  6. use OrpheusNET\Logchecker\Util;
  7. use PHPUnit\Framework\TestCase;
  8. class TranslatorTest extends TestCase
  9. {
  10. public function foreignLogDataProvider()
  11. {
  12. $logs = [];
  13. $logPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'utf8']);
  14. foreach (new FilesystemIterator($logPath, FilesystemIterator::SKIP_DOTS) as $dir) {
  15. if ($dir->isFile()) {
  16. continue;
  17. }
  18. foreach (new FilesystemIterator($dir->getPathname(), FilesystemIterator::SKIP_DOTS) as $logFiles) {
  19. if (preg_match("/[0-9]+\.log/", $logFiles->getFilename())) {
  20. $logs[] = [$dir->getFilename(), $logFiles->getPathname()];
  21. }
  22. }
  23. }
  24. return $logs;
  25. }
  26. /**
  27. * @dataProvider foreignLogDataProvider
  28. */
  29. public function testTranslateLog($language, $logPath)
  30. {
  31. $log = file_get_contents($logPath);
  32. $translatedLogPath = str_replace('.log', '_en.log', $logPath);
  33. $langDetails = Translator::getLanguage($log);
  34. $this->assertSame($language, $langDetails['code']);
  35. $this->assertNotNull($langDetails['name']);
  36. $this->assertNotNull($langDetails['name_english']);
  37. $this->assertStringEqualsFile($translatedLogPath, Translator::translate($log, $language));
  38. }
  39. public function testInvalidLanguage()
  40. {
  41. $this->expectException(UnknownLanguageException::class);
  42. $this->expectExceptionMessage('Could not determine language of EAC log');
  43. Translator::getLanguage(
  44. file_get_contents(
  45. implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'xld', 'originals', 'xld_perfect.log'])
  46. )
  47. );
  48. }
  49. public function englishLogProvider()
  50. {
  51. return array_map(
  52. function ($file) {
  53. return [$file];
  54. },
  55. array_filter(
  56. scandir(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'originals'])),
  57. function ($file) {
  58. return substr($file, 0, 2) === 'en';
  59. }
  60. )
  61. );
  62. }
  63. /**
  64. * @dataProvider englishLogProvider
  65. */
  66. public function testEnglishLanguage(string $file)
  67. {
  68. $logPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'logs', 'eac', 'originals', $file]);
  69. $log = file_get_contents($logPath);
  70. $log = Util::decodeEncoding($log, $logPath);
  71. $langDetails = Translator::getLanguage($log);
  72. $this->assertSame('en', $langDetails['code']);
  73. $this->assertSame('English', $langDetails['name']);
  74. $this->assertSame('English', $langDetails['name_english']);
  75. }
  76. }