-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathSectionObjectHandler.php
169 lines (149 loc) · 5.96 KB
/
SectionObjectHandler.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Page\Handlers;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectHandlerInterface;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\Page\Objects\ElementObject;
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Magento\FunctionalTestingFramework\Util\Validation\NameValidationUtil;
use Magento\FunctionalTestingFramework\XmlParser\SectionParser;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
class SectionObjectHandler implements ObjectHandlerInterface
{
const SECTION = 'section';
const ELEMENT = 'element';
const TYPE = 'type';
const SELECTOR = 'selector';
const LOCATOR_FUNCTION = 'locatorFunction';
const TIMEOUT = 'timeout';
const PARAMETERIZED = 'parameterized';
const FILENAME = 'filename';
const SECTION_NAME_ERROR_MSG = "Section names cannot contain non alphanumeric characters.\tSection='%s'";
const ELEMENT_NAME_ERROR_MSG = "Element names cannot contain non alphanumeric characters.\tElement='%s'";
/**
* Singleton instance of this class
*
* @var SectionObjectHandler
*/
private static $INSTANCE;
/**
* All section objects. Set during initialize().
*
* @var SectionObject[]
*/
private $sectionObjects = [];
/**
* Constructor
*
* @constructor
* @throws XmlException
*/
private function __construct()
{
$objectManager = ObjectManagerFactory::getObjectManager();
$parser = $objectManager->get(SectionParser::class);
$parserOutput = $parser->getData(self::SECTION);
if (!$parserOutput) {
return;
}
$sectionNameValidator = new NameValidationUtil();
$elementNameValidator = new NameValidationUtil();
foreach ($parserOutput as $sectionName => $sectionData) {
$elements = [];
if (preg_match('/[^a-zA-Z0-9_]/', $sectionName)) {
throw new XmlException(sprintf(self::SECTION_NAME_ERROR_MSG, $sectionName));
}
$filename = $sectionData[self::FILENAME] ?? null;
$sectionNameValidator->validateAffixes($sectionName, NameValidationUtil::SECTION, $filename);
try {
foreach ($sectionData[SectionObjectHandler::ELEMENT] as $elementName => $elementData) {
if (preg_match('/[^a-zA-Z0-9_]/', $elementName)) {
throw new XmlException(sprintf(self::ELEMENT_NAME_ERROR_MSG, $elementName, $sectionName));
}
$elementNameValidator->validateCamelCase(
$elementName,
NameValidationUtil::SECTION_ELEMENT_NAME,
$filename
);
$elementType = $elementData[SectionObjectHandler::TYPE] ?? null;
$elementSelector = $elementData[SectionObjectHandler::SELECTOR] ?? null;
$elementLocatorFunc = $elementData[SectionObjectHandler::LOCATOR_FUNCTION] ?? null;
$elementTimeout = $elementData[SectionObjectHandler::TIMEOUT] ?? null;
$elementParameterized = $elementData[SectionObjectHandler::PARAMETERIZED] ?? false;
$elementDeprecated = $elementData[self::OBJ_DEPRECATED] ?? null;
if ($elementDeprecated !== null) {
LoggingUtil::getInstance()->getLogger(ElementObject::class)->deprecation(
$elementDeprecated,
["elementName" => $elementName, "deprecatedElement" => $elementDeprecated]
);
}
$elements[$elementName] = new ElementObject(
$elementName,
$elementType,
$elementSelector,
$elementLocatorFunc,
$elementTimeout,
$elementParameterized,
$elementDeprecated
);
}
} catch (XmlException $exception) {
throw new XmlException($exception->getMessage() . " in Section '{$sectionName}'");
}
$sectionDeprecated = $sectionData[self::OBJ_DEPRECATED] ?? null;
if ($sectionDeprecated !== null) {
LoggingUtil::getInstance()->getLogger(SectionObject::class)->deprecation(
$sectionDeprecated,
["sectionName" => $filename, "deprecatedSection" => $sectionDeprecated]
);
}
$this->sectionObjects[$sectionName] = new SectionObject(
$sectionName,
$elements,
$filename,
$sectionDeprecated
);
}
$sectionNameValidator->summarize(NameValidationUtil::SECTION . " name");
$elementNameValidator->summarize(NameValidationUtil::SECTION_ELEMENT_NAME);
}
/**
* Initialize and/or return the singleton instance of this class
*
* @return SectionObjectHandler
* @throws XmlException
*/
public static function getInstance()
{
if (!self::$INSTANCE) {
self::$INSTANCE = new SectionObjectHandler();
}
return self::$INSTANCE;
}
/**
* Get a SectionObject by name
*
* @param string $name The section name.
* @return SectionObject | null
*/
public function getObject($name)
{
if (array_key_exists($name, $this->getAllObjects())) {
return $this->getAllObjects()[$name];
}
return null;
}
/**
* Get all SectionObjects
*
* @return SectionObject[]
*/
public function getAllObjects()
{
return $this->sectionObjects;
}
}