Пример #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     try {
         $domXml = $this->domFactory->create();
         $domXsl = $this->domFactory->create();
         $xsltProcessor = $this->xsltProcessorFactory->create();
         $xmlFile = $input->getArgument(self::XML_FILE_ARGUMENT);
         $domXml->preserveWhiteSpace = true;
         $domXml->load($xmlFile);
         $domXsl->preserveWhiteSpace = true;
         $domXsl->load($input->getArgument(self::PROCESSOR_ARGUMENT));
         $xsltProcessor->registerPHPFunctions();
         $xsltProcessor->importStylesheet($domXsl);
         $transformedDoc = $xsltProcessor->transformToXml($domXml);
         $result = $this->formatter->format($transformedDoc);
         if ($input->getOption(self::OVERWRITE_OPTION)) {
             file_put_contents($input->getArgument(self::XML_FILE_ARGUMENT), $result);
             $output->writeln("<info>You saved converted XML into {$xmlFile}</info>");
         } else {
             $output->write($result);
         }
         return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
     } catch (\Exception $exception) {
         $errorMessage = $exception->getMessage();
         $output->writeln("<error>{$errorMessage}</error>");
         // we must have an exit code higher than zero to indicate something was wrong
         return \Magento\Framework\Console\Cli::RETURN_FAILURE;
     }
 }
 public function testExecute()
 {
     $domXml = $this->getMock('DOMDocument', [], [], '', false);
     $domXsl = clone $domXml;
     $domXml->expects($this->once())->method('load')->with('file.xml');
     $domXsl->expects($this->once())->method('load')->with('file.xsl');
     $this->domFactory->expects($this->at(0))->method('create')->willReturn($domXml);
     $this->domFactory->expects($this->at(1))->method('create')->willReturn($domXsl);
     $xsltProcessor = $this->getMock('XSLTProcessor', [], [], '', false);
     $xsltProcessor->expects($this->once())->method('transformToXml')->with($domXml)->willReturn('XML');
     $this->xsltProcessorFactory->expects($this->once())->method('create')->willReturn($xsltProcessor);
     $this->formatter->expects($this->once())->method('format')->with('XML')->willReturn('result');
     $commandTester = new CommandTester($this->command);
     $commandTester->execute([XmlConverterCommand::XML_FILE_ARGUMENT => 'file.xml', XmlConverterCommand::PROCESSOR_ARGUMENT => 'file.xsl']);
     $this->assertContains('result', $commandTester->getDisplay());
 }