Skip to content

Commit ec05ce8

Browse files
committed
MQE-388: Write store settings before tests
- change output deletion behavior - fix codesniffer method - add new metadata generator util - add new example input file - change dir setup util to exposer public recursive delete
1 parent 0718542 commit ec05ce8

File tree

7 files changed

+231
-1
lines changed

7 files changed

+231
-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: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
* The relative filepath for the *meta.xml file to be generated.
55+
*
56+
* @var string
57+
*/
58+
private $filepath;
59+
60+
/**
61+
* MetadataGenUtil constructor.
62+
*
63+
* @param string $operationName
64+
* @param string $operationDataType
65+
* @param string $operationUrl
66+
* @param string $inputString
67+
*/
68+
public function __construct($operationName, $operationDataType, $operationUrl, $inputString)
69+
{
70+
$this->operationName = $operationName;
71+
$this->operationDataType = $operationDataType;
72+
$this->operationUrl = $operationUrl;
73+
$this->inputString = $inputString;
74+
75+
$this->filepath = self::OUTPUT_DIR . DIRECTORY_SEPARATOR . $this->operationDataType . "-meta.xml";
76+
}
77+
78+
/**
79+
* Function which takes params from constructor, transforms into data array and outputs a representative metadata
80+
* file for MFTF to consume and send requests.
81+
*
82+
* @return void
83+
*/
84+
public function generateMetadataFile()
85+
{
86+
$this->initMustacheTemplates();
87+
88+
// parse the string params into an array
89+
parse_str($this->inputString, $results);
90+
$data = $this->convertResultToEntry($results, $this->operationDataType);
91+
$data = $this->appendParentParams($data);
92+
$output = $this->mustache_engine->render('operation', $data);
93+
$this->cleanAndCreateOutputDir();
94+
file_put_contents(
95+
$this->filepath,
96+
$output
97+
);
98+
}
99+
100+
/**
101+
* Function which initializes mustache templates for file generation.
102+
*
103+
* @return void
104+
*/
105+
private function initMustacheTemplates()
106+
{
107+
$this->mustache_engine = new Mustache_Engine(
108+
['loader' => new Mustache_Loader_FilesystemLoader("views"),
109+
'partials_loader' => new Mustache_Loader_FilesystemLoader(
110+
"views" . DIRECTORY_SEPARATOR . "partials"
111+
)]
112+
);
113+
}
114+
115+
/**
116+
* Function which takes the top level params from the user and returns an array appended with the needed config.
117+
*
118+
* @param array $data
119+
* @return array
120+
*/
121+
private function appendParentParams($data)
122+
{
123+
$result = $data;
124+
$result['operationName'] = $this->operationName;
125+
$result['operationDataType'] = $this->operationDataType;
126+
$result['operationUrl'] = $this->operationUrl;
127+
128+
return $result;
129+
}
130+
131+
/**
132+
* Function which is called recursively to generate the mustache array for the template enging. Makes decisions
133+
* about type and format based on parameter array.
134+
*
135+
* @param array $results
136+
* @param string $defaultDataType
137+
* @return array
138+
*/
139+
private function convertResultToEntry($results, $defaultDataType)
140+
{
141+
$data = [];
142+
143+
foreach ($results as $key => $result) {
144+
$entry = [];
145+
if (is_array($result)) {
146+
$entry = array_merge($entry, ['objectName' => $key]);
147+
$res = $this->convertResultToEntry($result, $defaultDataType);
148+
if (!array_key_exists('objects', $res)) {
149+
$entry = array_merge($entry, ['objects' => null]);
150+
$entry = array_merge($entry, ['dataType' => $key]);
151+
} else {
152+
$entry = array_merge($entry, ['hasChildObj' => true]);
153+
$entry = array_merge($entry, ['dataType' => $defaultDataType]);
154+
}
155+
$data['objects'][] = array_merge($entry, $res);
156+
} else {
157+
$data['fields'][] = ['fieldName' => $key];
158+
}
159+
}
160+
161+
return $data;
162+
}
163+
164+
/**
165+
* Function which cleans any previously created fileand creates the _output dir.
166+
*
167+
* @return void
168+
*/
169+
private function cleanAndCreateOutputDir()
170+
{
171+
if (!file_exists(self::OUTPUT_DIR)) {
172+
mkdir(self::OUTPUT_DIR);
173+
}
174+
175+
if (file_exists($this->filepath)) {
176+
unlink($this->filepath);
177+
}
178+
}
179+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require_once '../../../../../vendor/autoload.php';
8+
9+
const INPUT_TXT_FILE = 'input.yml';
10+
11+
// parse the input.yml file for context
12+
$inputCfg = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(INPUT_TXT_FILE));
13+
14+
// create new MetadataGenUtil Object
15+
$metadataGenUtil = new Magento\FunctionalTestingFramework\Util\MetadataGenerator\MetadataGenUtil(
16+
$inputCfg['operationName'],
17+
$inputCfg['operationDataType'],
18+
$inputCfg['operationUrl'],
19+
$inputCfg['inputString']
20+
);
21+
22+
//generate the metadata file in the _output dir
23+
$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)