|
| 1 | +<?php |
| 2 | +// @codingStandardsIgnoreFile |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types = 1); |
| 8 | + |
| 9 | +namespace Magento\FunctionalTestingFramework\Console; |
| 10 | + |
| 11 | +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Input\InputArgument; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class GenerateDevUrnCommand extends Command |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Configures the current command. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + protected function configure() |
| 25 | + { |
| 26 | + $this->setName('generate:urn-catalog') |
| 27 | + ->setDescription('This command generates an URN catalog to enable PHPStorm to recognize and highlight URNs.') |
| 28 | + ->addArgument('path', InputArgument::REQUIRED, 'path to PHPStorm misc.xml file (typically located in [ProjectRoot]/.idea/misc.xml)'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Executes the current command. |
| 33 | + * |
| 34 | + * @param InputInterface $input |
| 35 | + * @param OutputInterface $output |
| 36 | + * @return void |
| 37 | + * @throws \Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException |
| 38 | + */ |
| 39 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 40 | + { |
| 41 | + $miscXmlFilePath = $input->getArgument('path') . DIRECTORY_SEPARATOR . "misc.xml"; |
| 42 | + $miscXmlFile = realpath($miscXmlFilePath); |
| 43 | + |
| 44 | + if ($miscXmlFile === false) { |
| 45 | + $exceptionMessage = "misc.xml not found in given path '{$miscXmlFilePath}'"; |
| 46 | + LoggingUtil::getInstance()->getLogger(GenerateDevUrnCommand::class) |
| 47 | + ->error($exceptionMessage); |
| 48 | + throw new TestFrameworkException($exceptionMessage); |
| 49 | + } |
| 50 | + $dom = new \DOMDocument('1.0'); |
| 51 | + $dom->preserveWhiteSpace = false; |
| 52 | + $dom->formatOutput = true; |
| 53 | + $dom->loadXML(file_get_contents($miscXmlFile)); |
| 54 | + |
| 55 | + //Locate ProjectResources node, create one if none are found. |
| 56 | + $nodeForWork = null; |
| 57 | + foreach($dom->getElementsByTagName('component') as $child) { |
| 58 | + if ($child->getAttribute('name') === 'ProjectResources') { |
| 59 | + $nodeForWork = $child; |
| 60 | + } |
| 61 | + } |
| 62 | + if ($nodeForWork === null) { |
| 63 | + $project = $dom->getElementsByTagName('project')->item(0); |
| 64 | + $nodeForWork = $dom->createElement('component'); |
| 65 | + $nodeForWork->setAttribute('name', 'ProjectResources'); |
| 66 | + $project->appendChild($nodeForWork); |
| 67 | + } |
| 68 | + |
| 69 | + //Extract url=>location mappings that already exist, add MFTF URNs and reappend |
| 70 | + $resources = []; |
| 71 | + $resourceNodes = $nodeForWork->getElementsByTagName('resource'); |
| 72 | + $resourceCount = $resourceNodes->length; |
| 73 | + for ($i = 0; $i < $resourceCount; $i++) { |
| 74 | + $child = $resourceNodes[0]; |
| 75 | + $resources[$child->getAttribute('url')] = $child->getAttribute('location'); |
| 76 | + $child->parentNode->removeChild($child); |
| 77 | + } |
| 78 | + |
| 79 | + $resources = array_merge($resources, $this->generateResourcesArray()); |
| 80 | + |
| 81 | + foreach ($resources as $url => $location) { |
| 82 | + $resourceNode = $dom->createElement('resource'); |
| 83 | + $resourceNode->setAttribute('url', $url); |
| 84 | + $resourceNode->setAttribute('location', $location); |
| 85 | + $nodeForWork->appendChild($resourceNode); |
| 86 | + } |
| 87 | + |
| 88 | + //Save output |
| 89 | + $dom->save($miscXmlFile); |
| 90 | + $output->writeln("MFTF URN mapping successfully added to {$miscXmlFile}."); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Generates urn => location array for all MFTF schema. |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + private function generateResourcesArray() |
| 98 | + { |
| 99 | + $resourcesArray = [ |
| 100 | + 'urn:magento:mftf:DataGenerator/etc/dataOperation.xsd' => |
| 101 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataOperation.xsd'), |
| 102 | + 'urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd' => |
| 103 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataProfileSchema.xsd'), |
| 104 | + 'urn:magento:mftf:Page/etc/PageObject.xsd' => |
| 105 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd'), |
| 106 | + 'urn:magento:mftf:Page/etc/SectionObject.xsd' => |
| 107 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd'), |
| 108 | + 'urn:magento:mftf:Test/etc/actionGroupSchema.xsd' => |
| 109 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd'), |
| 110 | + 'urn:magento:mftf:Test/etc/testSchema.xsd' => |
| 111 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd'), |
| 112 | + 'urn:magento:mftf:Suite/etc/suiteSchema.xsd' => |
| 113 | + realpath(FW_BP . '/src/Magento/FunctionalTestingFramework/Suite/etc/suiteSchema.xsd') |
| 114 | + ]; |
| 115 | + return $resourcesArray; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments