|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\FunctionalTestingFramework\Util\MetadataGenerator; |
| 8 | + |
| 9 | +use Mustache_Engine; |
| 10 | +use Mustache_Loader_FilesystemLoader; |
| 11 | + |
| 12 | +class MetadataGenUtil |
| 13 | +{ |
| 14 | + const OUTPUT_DIR = '_output'; |
| 15 | + const INPUT_TXT_FILE = 'input.yml'; |
| 16 | + |
| 17 | + /** |
| 18 | + * Mustache Engine instance for the templating. |
| 19 | + * |
| 20 | + * @var Mustache_Engine |
| 21 | + */ |
| 22 | + private $mustache_engine; |
| 23 | + |
| 24 | + /** |
| 25 | + * Name of the operation (e.g. createCategory) |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $operationName; |
| 30 | + |
| 31 | + /** |
| 32 | + * Data type of the operation (e.g. category) |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private $operationDataType; |
| 37 | + |
| 38 | + /** |
| 39 | + * Url path for the operation (e.g. /admin/system_config/save/section/payment) |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + private $operationUrl; |
| 44 | + |
| 45 | + /** |
| 46 | + * The raw parameter data to be converted into metadata |
| 47 | + * (e.g. entity[param1]=value1&entity[param2]=value2&entity[param3]=value3&entityField=field1) |
| 48 | + * |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + private $inputString; |
| 52 | + |
| 53 | + /** |
| 54 | + * MetadataGenUtil constructor. |
| 55 | + * |
| 56 | + * @param string $operationName |
| 57 | + * @param string $operationDataType |
| 58 | + * @param string $operationUrl |
| 59 | + * @param string $inputString |
| 60 | + */ |
| 61 | + public function __construct($operationName, $operationDataType, $operationUrl, $inputString) |
| 62 | + { |
| 63 | + $this->operationName = $operationName; |
| 64 | + $this->operationDataType = $operationDataType; |
| 65 | + $this->operationUrl = $operationUrl; |
| 66 | + $this->inputString = $inputString; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Function which takes params from constructor, transforms into data array and outputs a representative metadata |
| 71 | + * file for MFTF to consume and send requests. |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function generateMetadataFile() |
| 76 | + { |
| 77 | + $this->initMustacheTemplates(); |
| 78 | + |
| 79 | + // parse the string params into an array |
| 80 | + parse_str($this->inputString, $results); |
| 81 | + $data = $this->convertResultToEntry($results, $this->operationDataType); |
| 82 | + $data = $this->appendParentParams($data); |
| 83 | + $output = $this->mustache_engine->render('operation', $data); |
| 84 | + $this->cleanAndCreateOutputDir(); |
| 85 | + file_put_contents( |
| 86 | + self::OUTPUT_DIR . DIRECTORY_SEPARATOR . $this->operationDataType . "-meta.xml", |
| 87 | + $output |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Function which initializes mustache templates for file generation. |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + private function initMustacheTemplates() |
| 97 | + { |
| 98 | + $this->mustache_engine = new Mustache_Engine( |
| 99 | + ['loader' => new Mustache_Loader_FilesystemLoader("views"), |
| 100 | + 'partials_loader' => new Mustache_Loader_FilesystemLoader( |
| 101 | + "views" . DIRECTORY_SEPARATOR . "partials" |
| 102 | + )] |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Function which takes the top level params from the user and returns an array appended with the needed config. |
| 108 | + * |
| 109 | + * @param array $data |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + private function appendParentParams($data) |
| 113 | + { |
| 114 | + $result = $data; |
| 115 | + $result['operationName'] = $this->operationName; |
| 116 | + $result['operationDataType'] = $this->operationDataType; |
| 117 | + $result['operationUrl'] = $this->operationUrl; |
| 118 | + |
| 119 | + return $result; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Function which is called recursively to generate the mustache array for the template enging. Makes decisions |
| 124 | + * about type and format based on parameter array. |
| 125 | + * |
| 126 | + * @param array $results |
| 127 | + * @param string $defaultDataType |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + private function convertResultToEntry($results, $defaultDataType) |
| 131 | + { |
| 132 | + $data = []; |
| 133 | + |
| 134 | + foreach ($results as $key => $result) { |
| 135 | + $entry = []; |
| 136 | + if (is_array($result)) { |
| 137 | + $entry = array_merge($entry, ['objectName' => $key]); |
| 138 | + $res = $this->convertResultToEntry($result, $defaultDataType); |
| 139 | + if (!array_key_exists('objects', $res)) { |
| 140 | + $entry = array_merge($entry, ['objects' => null]); |
| 141 | + $entry = array_merge($entry, ['dataType' => $key]); |
| 142 | + } else { |
| 143 | + $entry = array_merge($entry, ['hasChildObj' => true]); |
| 144 | + $entry = array_merge($entry, ['dataType' => $defaultDataType]); |
| 145 | + } |
| 146 | + $data['objects'][] = array_merge($entry, $res); |
| 147 | + } else { |
| 148 | + $data['fields'][] = ['fieldName' => $key]; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return $data; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Function which cleans and creates the _output dir. |
| 157 | + * |
| 158 | + * @return void |
| 159 | + */ |
| 160 | + private function cleanAndCreateOutputDir() |
| 161 | + { |
| 162 | + if (file_exists(self::OUTPUT_DIR)) { |
| 163 | + \Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil::rmdirRecursive(self::OUTPUT_DIR); |
| 164 | + } |
| 165 | + |
| 166 | + mkdir(self::OUTPUT_DIR); |
| 167 | + } |
| 168 | +} |
0 commit comments