Skip to content

Commit 8d698c4

Browse files
committed
MQE-388: Write store settings before tests
- add new metadata generator util - add new example input file - change dir setup util to exposer public recursive delete
1 parent 7961c6d commit 8d698c4

File tree

7 files changed

+215
-1
lines changed

7 files changed

+215
-1
lines changed

src/Magento/FunctionalTestingFramework/Util/Filesystem/DirSetupUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function createGroupDir($fullPath)
3232
* @param string $directory
3333
* @return void
3434
*/
35-
private static function rmdirRecursive($directory)
35+
public static function rmdirRecursive($directory)
3636
{
3737
$it = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
3838

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
require_once '../../../../../vendor/autoload.php';
3+
4+
const INPUT_TXT_FILE = 'input.yml';
5+
6+
// parse the input.yml file for context
7+
$inputCfg = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(INPUT_TXT_FILE));
8+
9+
// create new MetadataGenUtil Object
10+
$metadataGenUtil = new Magento\FunctionalTestingFramework\Util\MetadataGenerator\MetadataGenUtil(
11+
$inputCfg['operationName'],
12+
$inputCfg['operationDataType'],
13+
$inputCfg['operationUrl'],
14+
$inputCfg['inputString']
15+
);
16+
17+
//generate the metadata file in the _output dir
18+
$metadataGenUtil->generateMetadataFile();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
operationName: createMyEntity
2+
operationDataType: myEntityType
3+
operationUrl: /admin/system_config/save/someEntity
4+
inputString: entity[param1]=value1&entity[param2]=value2&entity[param3]=value3&entityField=field1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../DataGenerator/etc/dataOperation.xsd">
10+
<operation name="{{operationName}}" dataType="{{operationDataType}}" type="create" auth="adminFormKey" url="{{operationUrl}}" method="POST">
11+
{{>object}}
12+
</operation>
13+
</config>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<field key="{{fieldName}}">string</field>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{#objects}}
2+
<object key="{{objectName}}" dataType="{{dataType}}">
3+
{{#fields}}
4+
{{> field}}
5+
{{/fields}}
6+
{{#hasChildObj}}
7+
{{> object}}
8+
{{/hasChildObj}}
9+
</object>
10+
{{/objects}}

0 commit comments

Comments
 (0)