/**
  * @param string[][][] $fieldsAsMultiDimensionalArrays
  * @param string[] $metadata
  * @param string[] $files
  * @param string $type
  * @param string[] $expectedFile
  * @param string[] $logLevels
  * @dataProvider dictionaryProvider
  */
 public function testSerialize(array $fieldsAsMultiDimensionalArrays, array $metadata, array $files, string $type, array $expectedFile, array $logLevels)
 {
     $expectedFile['bytes'] = $this->stripIndentsAndToCRLF($expectedFile['bytes']);
     $serializer = new InteligenceoSerializer($type);
     $serializer->setLogger($this);
     $dictionary = $this->generateDictionary($fieldsAsMultiDimensionalArrays, $metadata, $files);
     $file = $serializer->serialize($dictionary);
     if ($files) {
         $finfo = new \esperecyan\dictionary_php\fileinfo\Finfo(FILEINFO_MIME_TYPE);
         $archive = $this->generateArchive($file['bytes']);
         for ($i = 0, $l = $archive->numFiles; $i < $l; $i++) {
             $actualTypes[$archive->getNameIndex($i)] = $finfo->buffer($archive->getFromIndex($i));
         }
         $asciiTitle = (new \esperecyan\dictionary_php\validator\FilenameValidator())->convertToValidFilenameWithoutExtensionInArchives($dictionary->getTitle());
         foreach ($files as $filename => $content) {
             $expectedTypes["{$asciiTitle}/{$filename}"] = $finfo->buffer($content);
         }
         $expectedTypes["{$asciiTitle}.txt"] = 'text/plain';
         $this->assertEquals($expectedTypes, $actualTypes);
         $file['bytes'] = $archive->getFromName("{$asciiTitle}.txt");
     }
     $file['bytes'] = mb_convert_encoding($file['bytes'], 'UTF-8', 'Windows-31J');
     $this->assertEquals($expectedFile, $file);
     $this->assertEquals($logLevels, $this->logLevels);
 }
Ejemplo n.º 2
0
 /**
  * @param string $to
  * @param string[][][] $fieldsAsMultiDimensionalArrays
  * @param string[] $metadata
  * @param string[] $files
  * @param string[] $expectedFile
  * @param string[] $logLevels
  * @param bool $csvOnly
  * @dataProvider dictionaryProvider
  */
 public function testSerialize(string $to, array $fieldsAsMultiDimensionalArrays, array $metadata, array $files, array $expectedFile, array $logLevels, bool $csvOnly = false)
 {
     $dictionary = $this->generateDictionary($fieldsAsMultiDimensionalArrays, $metadata, $files);
     $expectedFile['bytes'] = $this->stripIndentsAndToCRLF($expectedFile['bytes']);
     $serializer = new Serializer($to);
     $serializer->setLogger($this);
     $file = $serializer->serialize($dictionary, $csvOnly);
     if ($to === '汎用辞書' && $files && !$csvOnly) {
         $archive = $this->generateArchive($file['bytes']);
         $finfo = new \esperecyan\dictionary_php\fileinfo\Finfo(FILEINFO_MIME_TYPE);
         for ($i = 0, $l = $archive->numFiles; $i < $l; $i++) {
             $actualTypes[$archive->getNameIndex($i)] = $finfo->buffer($archive->getFromIndex($i));
         }
         $this->assertEquals(array_map(function (string $file) use($finfo) : string {
             return $finfo->buffer($file);
         }, $files + ['dictionary.csv' => $expectedFile['bytes']]), $actualTypes);
         $file['bytes'] = $archive->getFromName('dictionary.csv');
         $this->assertEquals($expectedFile, $file);
     } else {
         if (strpos($expectedFile['type'], 'charset=Shift_JIS') !== false) {
             $file['bytes'] = mb_convert_encoding($file['bytes'], 'UTF-8', 'Windows-31J');
         }
         $this->assertEquals($expectedFile, $file);
     }
     $this->assertEquals($logLevels, $this->logLevels);
 }
 /**
  * @param string[][][] $fieldsAsMultiDimensionalArrays
  * @param string[] $metadata
  * @param string[] $files
  * @param string[] $expectedFile
  * @param string[] $logLevels
  * @dataProvider dictionaryProvider
  */
 public function testSerialize(array $fieldsAsMultiDimensionalArrays, array $metadata, array $files, array $expectedFile, array $logLevels)
 {
     $dictionary = $this->generateDictionary($fieldsAsMultiDimensionalArrays, $metadata, $files);
     $expectedFile['bytes'] = $this->stripIndentsAndToCRLF($expectedFile['bytes']);
     $serializer = new GenericDictionarySerializer();
     $serializer->setLogger($this);
     $file = $serializer->serialize($dictionary);
     if ($files) {
         $archive = $this->generateArchive($file['bytes']);
         $finfo = new \esperecyan\dictionary_php\fileinfo\Finfo(FILEINFO_MIME_TYPE);
         for ($i = 0, $l = $archive->numFiles; $i < $l; $i++) {
             $actualTypes[$archive->getNameIndex($i)] = $finfo->buffer($archive->getFromIndex($i));
         }
         $this->assertEquals(array_map(function (string $file) use($finfo) : string {
             return $finfo->buffer($file);
         }, $files + ['dictionary.csv' => $expectedFile['bytes']]), $actualTypes);
         $file['bytes'] = $archive->getFromName('dictionary.csv');
         $this->assertEquals($expectedFile, $file);
     } else {
         $this->assertEquals($expectedFile, $file);
     }
     $this->assertEquals($logLevels, $this->logLevels);
 }