|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\FunctionalTestingFramework\Test\Config; |
| 8 | + |
| 9 | +use Magento\FunctionalTestingFramework\Exceptions\XmlException; |
| 10 | +use Magento\FunctionalTestingFramework\Config\Dom\NodeMergingConfig; |
| 11 | +use Magento\FunctionalTestingFramework\Config\Dom\NodePathMatcher; |
| 12 | + |
| 13 | +class Dom extends \Magento\FunctionalTestingFramework\Config\Dom |
| 14 | +{ |
| 15 | + const TEST_FILE_NAME_ENDING = 'Test'; |
| 16 | + const TEST_META_FILENAME_ATTRIBUTE = 'filename'; |
| 17 | + const TEST_META_NAME_ATTRIBUTE = 'name'; |
| 18 | + const TEST_HOOK_NAMES = ["after", "before"]; |
| 19 | + |
| 20 | + /** |
| 21 | + * TestDom constructor. |
| 22 | + * @param string $xml |
| 23 | + * @param string $filename |
| 24 | + * @param array $idAttributes |
| 25 | + * @param string $typeAttributeName |
| 26 | + * @param string $schemaFile |
| 27 | + * @param string $errorFormat |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + $xml, |
| 31 | + $filename, |
| 32 | + array $idAttributes = [], |
| 33 | + $typeAttributeName = null, |
| 34 | + $schemaFile = null, |
| 35 | + $errorFormat = self::ERROR_FORMAT_DEFAULT |
| 36 | + ) { |
| 37 | + $this->schemaFile = $schemaFile; |
| 38 | + $this->nodeMergingConfig = new NodeMergingConfig(new NodePathMatcher(), $idAttributes); |
| 39 | + $this->typeAttributeName = $typeAttributeName; |
| 40 | + $this->errorFormat = $errorFormat; |
| 41 | + $this->dom = $this->initDom($xml, $filename); |
| 42 | + $this->rootNamespace = $this->dom->lookupNamespaceUri($this->dom->namespaceURI); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Takes a dom element from xml and appends the filename based on location |
| 47 | + * |
| 48 | + * @param string $xml |
| 49 | + * @param string|null $filename |
| 50 | + * @return \DOMDocument |
| 51 | + */ |
| 52 | + public function initDom($xml, $filename = null) |
| 53 | + { |
| 54 | + $dom = parent::initDom($xml); |
| 55 | + |
| 56 | + if (strpos($filename, self::TEST_FILE_NAME_ENDING)) { |
| 57 | + $testNodes = $dom->getElementsByTagName('test'); |
| 58 | + foreach ($testNodes as $testNode) { |
| 59 | + /** @var \DOMElement $testNode */ |
| 60 | + $testNode->setAttribute(self::TEST_META_FILENAME_ATTRIBUTE, $filename); |
| 61 | + $this->validateTestDomStepKeys($testNode, $filename); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $dom; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Redirects any merges into the init method for appending xml filename |
| 70 | + * |
| 71 | + * @param string $xml |
| 72 | + * @param string|null $filename |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function merge($xml, $filename = null) |
| 76 | + { |
| 77 | + $dom = $this->initDom($xml, $filename); |
| 78 | + $this->mergeNode($dom->documentElement, ''); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Parses an individual DOM structure for repeated stepKey attributes |
| 83 | + * |
| 84 | + * @param \DOMElement $testNode |
| 85 | + * @param string $filename |
| 86 | + * @throws XmlException |
| 87 | + */ |
| 88 | + private function validateTestDomStepKeys($testNode, $filename) |
| 89 | + { |
| 90 | + $childNodes = $testNode->childNodes; |
| 91 | + |
| 92 | + $keyValues = []; |
| 93 | + for ($i = 0; $i < $childNodes->length; $i++) { |
| 94 | + $currentNode = $childNodes->item($i); |
| 95 | + |
| 96 | + if (!is_a($currentNode, \DOMElement::class)) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if (in_array($currentNode->nodeName, self::TEST_HOOK_NAMES)) { |
| 101 | + $this->validateTestDomStepKeys($currentNode, $filename); |
| 102 | + } |
| 103 | + |
| 104 | + if ($currentNode->hasAttribute('stepKey')) { |
| 105 | + $keyValues[] = $currentNode->getAttribute('stepKey'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + $withoutDuplicates = array_unique($keyValues); |
| 110 | + $duplicates = array_diff_assoc($keyValues, $withoutDuplicates); |
| 111 | + |
| 112 | + if (count($duplicates) > 0) { |
| 113 | + $stepKeyError = ""; |
| 114 | + foreach ($duplicates as $duplicateKey => $duplicateValue) { |
| 115 | + $stepKeyError .= "\tstepKey: {$duplicateValue} is used more than once.\n"; |
| 116 | + } |
| 117 | + |
| 118 | + throw new XmlException("Tests cannot use stepKey more than once!\t\n{$stepKeyError}\tin file: {$filename}"); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments