Skip to content

Commit 47820ec

Browse files
committed
MQE-1963: Update XSD Schema to verify that file has only single entity
1 parent e09facc commit 47820ec

File tree

1 file changed

+11
-48
lines changed

1 file changed

+11
-48
lines changed

src/Magento/FunctionalTestingFramework/Upgrade/SplitMultipleEntitiesFiles.php

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,20 @@ private function processXmlFiles($xmlFiles, $type, $urn)
9797
{
9898
foreach ($xmlFiles as $file) {
9999
$contents = $file->getContents();
100-
$entityContents = $this->getEntityContents($contents, $type);
101-
if (count($entityContents) > 1) {
100+
$domDocument = new \DOMDocument();
101+
$domDocument->loadXML($contents);
102+
$entities = $domDocument->getElementsByTagName(lcfirst($type));
103+
104+
if ($entities->length > 1) {
102105
$filename = $file->getRealPath();
103106
if ($this->output->isVerbose()) {
104107
$this->output->writeln('Processing file:' . $filename);
105108
}
106-
foreach ($entityContents as $entityName => $entityContent) {
109+
foreach ($entities as $entity) {
110+
/** @var \DOMElement $entity*/
111+
$entityName = $entity->getAttribute('name');
112+
$entityContent = $entity->ownerDocument->saveXML($entity);
113+
107114
$dir = dirname($file);
108115
$dir .= DIRECTORY_SEPARATOR . ucfirst(basename($file, '.xml'));
109116
$splitFileName = $this->formatName($entityName, $type);
@@ -128,50 +135,6 @@ private function processXmlFiles($xmlFiles, $type, $urn)
128135
}
129136
}
130137

131-
/**
132-
* Parse contents and return array of single entity content according to entity type
133-
*
134-
* @param string $contents
135-
* @param string $type
136-
* @return array
137-
*/
138-
private function getEntityContents($contents, $type)
139-
{
140-
$nonEmptyTag = '/[\S\s]+\n(?<entity>[\s]*<' . lcfirst($type)
141-
. '[^\<\>]+name[\s]*=[\s]*"(?<name>[^\"\'\<\>\&]+)"[^\<\>]*>[\S\s]+<\/'
142-
. lcfirst($type) . '[\s]*>)+[\S\s]+/';
143-
$entityContents = $this->scanForPattern($contents, $type, $nonEmptyTag);
144-
145-
$emptyTag = '/[\S\s]+\n(?<entity>[\s]*<' . lcfirst($type)
146-
. '[^\<\>]+name[\s]*=[\s]*"(?<name>[^\"\'\<\>\&]+)"[^\<\>]*\/>)/';
147-
$entityContents = array_merge($entityContents, $this->scanForPattern($contents, $type, $emptyTag));
148-
149-
return $entityContents;
150-
}
151-
152-
/**
153-
* Scan contents to match given pattern
154-
*
155-
* @param string $contents
156-
* @param string $type
157-
* @param string $pattern
158-
* @return array
159-
*/
160-
private function scanForPattern($contents, $type, $pattern)
161-
{
162-
preg_match($pattern, $contents, $matches);
163-
if (isset($matches['entity']) && isset($matches['name'])) {
164-
$contents = str_replace($matches['entity'], '', $contents);
165-
$entityContents[trim($matches['name'])] = $matches['entity'];
166-
$restEntityContents = $this->scanForPattern($contents, $type, $pattern);
167-
if (!empty($restEntityContents)) {
168-
$entityContents = array_merge($entityContents, $restEntityContents);
169-
}
170-
return $entityContents;
171-
} else {
172-
return [];
173-
}
174-
}
175138

176139
/**
177140
* Create file with contents and create dir if needed
@@ -198,7 +161,7 @@ private function filePutContents($fullPath, $type, $urn, $contents)
198161
. '<' . lcfirst($type) . 's '
199162
. self::XML_NAMESPACE
200163
. self::XML_SCHEMA_LOCATION . $urn . '">' . PHP_EOL
201-
. $contents . PHP_EOL
164+
. ' ' . $contents . PHP_EOL
202165
. '</' . lcfirst($type) . 's>' . PHP_EOL;
203166

204167
file_put_contents($fullPath, $fullContents);

0 commit comments

Comments
 (0)