|
7 | 7 |
|
8 | 8 | use Magento\FunctionalTestingFramework\Config\Dom\NodeMergingConfig;
|
9 | 9 | use Magento\FunctionalTestingFramework\Config\Dom\NodePathMatcher;
|
| 10 | +use Magento\FunctionalTestingFramework\Exceptions\Collector\ExceptionCollector; |
| 11 | +use Magento\FunctionalTestingFramework\Util\Validation\DuplicateNodeValidationUtil; |
10 | 12 |
|
11 | 13 | /**
|
12 |
| - * Magento configuration XML DOM utility |
| 14 | + * MFTF actionGroup.xml configuration XML DOM utility |
| 15 | + * @package Magento\FunctionalTestingFramework\DataGenerator\Config |
13 | 16 | */
|
14 |
| -class Dom extends \Magento\FunctionalTestingFramework\Config\Dom |
| 17 | +class Dom extends \Magento\FunctionalTestingFramework\Config\MftfDom |
15 | 18 | {
|
| 19 | + const DATA_FILE_NAME_ENDING = "Data"; |
| 20 | + const DATA_META_FILENAME_ATTRIBUTE = "filename"; |
16 | 21 |
|
17 | 22 | /**
|
18 |
| - * Array of non keyed mergeable paths |
19 |
| - * |
20 |
| - * @var array |
| 23 | + * NodeValidationUtil |
| 24 | + * @var DuplicateNodeValidationUtil |
21 | 25 | */
|
22 |
| - private $mergeablePaths; |
| 26 | + private $validationUtil; |
23 | 27 |
|
24 | 28 | /**
|
25 |
| - * Build DOM with initial XML contents and specifying identifier attributes for merging. Overridden to include new |
26 |
| - * mergeablePaths argument which can be matched for non keyed mergeable xml elements. |
27 |
| - * |
28 |
| - * Format of $idAttributes: array('/xpath/to/some/node' => 'id_attribute_name') |
29 |
| - * The path to ID attribute name should not include any attribute notations or modifiers -- only node names |
30 |
| - * |
| 29 | + * Entity Dom constructor. |
31 | 30 | * @param string $xml
|
| 31 | + * @param string $filename |
| 32 | + * @param ExceptionCollector $exceptionCollector |
32 | 33 | * @param array $idAttributes
|
33 |
| - * @param array $mergeablePaths |
34 | 34 | * @param string $typeAttributeName
|
35 | 35 | * @param string $schemaFile
|
36 | 36 | * @param string $errorFormat
|
37 | 37 | */
|
38 | 38 | public function __construct(
|
39 | 39 | $xml,
|
| 40 | + $filename, |
| 41 | + $exceptionCollector, |
40 | 42 | array $idAttributes = [],
|
41 |
| - array $mergeablePaths = [], |
42 | 43 | $typeAttributeName = null,
|
43 | 44 | $schemaFile = null,
|
44 | 45 | $errorFormat = self::ERROR_FORMAT_DEFAULT
|
45 | 46 | ) {
|
46 |
| - $this->schemaFile = $schemaFile; |
47 |
| - $this->nodeMergingConfig = new NodeMergingConfig(new NodePathMatcher(), $idAttributes); |
48 |
| - $this->mergeablePaths = $mergeablePaths; |
49 |
| - $this->typeAttributeName = $typeAttributeName; |
50 |
| - $this->errorFormat = $errorFormat; |
51 |
| - $this->dom = $this->initDom($xml); |
52 |
| - $this->rootNamespace = $this->dom->lookupNamespaceUri($this->dom->namespaceURI); |
53 |
| - } |
54 |
| - |
55 |
| - /** |
56 |
| - * Recursive merging of the \DOMElement into the original document. Overridden to include a call to |
57 |
| - * |
58 |
| - * Algorithm: |
59 |
| - * 1. Find the same node in original document |
60 |
| - * 2. Extend and override original document node attributes and scalar value if found |
61 |
| - * 3. Append new node if original document doesn't have the same node |
62 |
| - * |
63 |
| - * @param \DOMElement $node |
64 |
| - * @param string $parentPath path to parent node |
65 |
| - * @return void |
66 |
| - */ |
67 |
| - public function mergeNode(\DOMElement $node, $parentPath) |
68 |
| - { |
69 |
| - $path = $this->getNodePathByParent($node, $parentPath); |
70 |
| - $isMergeablePath = $this->validateIsPathMergeable($path); |
71 |
| - |
72 |
| - $matchedNode = $this->getMatchedNode($path, $isMergeablePath); |
73 |
| - |
74 |
| - /* Update matched node attributes and value */ |
75 |
| - if ($matchedNode && !$isMergeablePath) { |
76 |
| - //different node type |
77 |
| - $this->mergeMatchingNode($node, $parentPath, $matchedNode, $path); |
78 |
| - } else { |
79 |
| - /* Add node as is to the document under the same parent element */ |
80 |
| - $parentMatchedNode = $this->getMatchedNode($parentPath); |
81 |
| - $newNode = $this->dom->importNode($node, true); |
82 |
| - $parentMatchedNode->appendChild($newNode); |
83 |
| - } |
| 47 | + $this->validationUtil = new DuplicateNodeValidationUtil('key', $exceptionCollector); |
| 48 | + parent::__construct( |
| 49 | + $xml, |
| 50 | + $filename, |
| 51 | + $exceptionCollector, |
| 52 | + $idAttributes, |
| 53 | + $typeAttributeName, |
| 54 | + $schemaFile, |
| 55 | + $errorFormat |
| 56 | + ); |
84 | 57 | }
|
85 | 58 |
|
86 | 59 | /**
|
87 |
| - * Getter for node by path, overridden to include validation flag for mergeable entries |
88 |
| - * An exception is possible if original document contains multiple nodes for identifier |
| 60 | + * Takes a dom element from xml and appends the filename based on location |
89 | 61 | *
|
90 |
| - * @param string $nodePath |
91 |
| - * @param boolean $isMergeablePath |
92 |
| - * @throws \Exception |
93 |
| - * @return \DOMElement|null |
| 62 | + * @param string $xml |
| 63 | + * @param string|null $filename |
| 64 | + * @return \DOMDocument |
94 | 65 | */
|
95 |
| - public function getMatchedNode($nodePath, $isMergeablePath = false) |
| 66 | + public function initDom($xml, $filename = null) |
96 | 67 | {
|
97 |
| - $xPath = new \DOMXPath($this->dom); |
98 |
| - if ($this->rootNamespace) { |
99 |
| - $xPath->registerNamespace(self::ROOT_NAMESPACE_PREFIX, $this->rootNamespace); |
100 |
| - } |
101 |
| - $matchedNodes = $xPath->query($nodePath); |
102 |
| - $node = null; |
| 68 | + $dom = parent::initDom($xml); |
103 | 69 |
|
104 |
| - if ($matchedNodes->length > 1 && !$isMergeablePath) { |
105 |
| - throw new \Exception("More than one node matching the query: {$nodePath}"); |
106 |
| - } elseif ($matchedNodes->length == 1) { |
107 |
| - $node = $matchedNodes->item(0); |
| 70 | + if (strpos($filename, self::DATA_FILE_NAME_ENDING)) { |
| 71 | + $entityNodes = $dom->getElementsByTagName('entity'); |
| 72 | + foreach ($entityNodes as $entityNode) { |
| 73 | + /** @var \DOMElement $entityNode */ |
| 74 | + $entityNode->setAttribute(self::DATA_META_FILENAME_ATTRIBUTE, $filename); |
| 75 | + $this->validationUtil->validateChildUniqueness( |
| 76 | + $entityNode, |
| 77 | + $filename |
| 78 | + ); |
| 79 | + } |
108 | 80 | }
|
109 |
| - return $node; |
110 |
| - } |
111 | 81 |
|
112 |
| - /** |
113 |
| - * Function which simplifies and xpath match in dom and compares with listed known mergeable paths |
114 |
| - * |
115 |
| - * @param string $path |
116 |
| - * @return boolean |
117 |
| - */ |
118 |
| - private function validateIsPathMergeable($path) |
119 |
| - { |
120 |
| - $simplifiedPath = $this->nodeMergingConfig->getNodePathMatcher()->simplifyXpath($path); |
121 |
| - return array_key_exists($simplifiedPath, $this->mergeablePaths); |
| 82 | + return $dom; |
122 | 83 | }
|
123 | 84 | }
|
0 commit comments