-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathUpdateTestSchemaPaths.php
99 lines (90 loc) · 3.01 KB
/
UpdateTestSchemaPaths.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Upgrade;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\Script\ScriptUtil;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class UpdateTestSchemaPaths
* @package Magento\FunctionalTestingFramework\Upgrade
*/
class UpdateTestSchemaPaths implements UpgradeInterface
{
/**
* OutputInterface
*
* @var OutputInterface
*/
private $output;
/**
* Total test updated
*
* @var integer
*/
private $testsUpdated = 0;
/**
* Entity type to urn map
*
* @var array
*/
private $typeToUrns = [
'ActionGroup' => 'urn:magento:mftf:Test/etc/actionGroupSchema.xsd',
'Data' => 'urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd',
'Metadata' => 'urn:magento:mftf:DataGenerator/etc/dataOperation.xsd',
'Page' => 'urn:magento:mftf:Page/etc/PageObject.xsd',
'Section' => 'urn:magento:mftf:Page/etc/SectionObject.xsd',
'Suite' => 'urn:magento:mftf:Suite/etc/suiteSchema.xsd',
'Test' => 'urn:magento:mftf:Test/etc/testSchema.xsd',
];
/**
* Upgrades all test xml files, replacing relative schema paths to URN.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws TestFrameworkException
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$scriptUtil = new ScriptUtil();
$this->output = $output;
$this->testsUpdated = 0;
$testPaths[] = $input->getArgument('path');
if (empty($testPaths[0])) {
$testPaths = $scriptUtil->getAllModulePaths();
}
// Process module xml files
foreach ($this->typeToUrns as $type => $urn) {
$xmlFiles = $scriptUtil->getModuleXmlFilesByScope($testPaths, $type);
$this->processXmlFiles($xmlFiles, $urn);
}
return ("Schema Path updated to use MFTF URNs in {$this->testsUpdated} file(s).");
}
/**
* Convert xml schema location from non urn based to urn based
*
* @param Finder $xmlFiles
* @param string $urn
* @return void
*/
private function processXmlFiles($xmlFiles, $urn)
{
$pattern = '/xsi:noNamespaceSchemaLocation[\s]*=[\s]*"(?<urn>[^\<\>"\']*)"/';
foreach ($xmlFiles as $file) {
$filePath = $file->getRealPath();
$contents = $file->getContents();
preg_match($pattern, $contents, $matches);
if (isset($matches['urn'])) {
if (trim($matches['urn']) !== $urn) {
file_put_contents($filePath, str_replace($matches['urn'], $urn, $contents));
$this->testsUpdated++;
}
}
}
}
}