|
12 | 12 | use Magento\FunctionalTestingFramework\Page\Objects\SectionObject; |
13 | 13 | use Magento\FunctionalTestingFramework\XmlParser\SectionParser; |
14 | 14 |
|
15 | | -/** |
16 | | - * Class SectionObjectHandler |
17 | | - */ |
18 | 15 | class SectionObjectHandler implements ObjectHandlerInterface |
19 | 16 | { |
20 | | - const TYPE = 'section'; |
21 | | - const SUB_TYPE = 'element'; |
22 | | - const ELEMENT_TYPE_ATTR = 'type'; |
23 | | - const ELEMENT_SELECTOR_ATTR = 'selector'; |
24 | | - const ELEMENT_LOCATOR_FUNC_ATTR = 'locatorFunction'; |
25 | | - const ELEMENT_TIMEOUT_ATTR = 'timeout'; |
26 | | - const ELEMENT_PARAMETERIZED = 'parameterized'; |
| 17 | + const SECTION = 'section'; |
| 18 | + const ELEMENT = 'element'; |
| 19 | + const TYPE = 'type'; |
| 20 | + const SELECTOR = 'selector'; |
| 21 | + const LOCATOR_FUNCTION = 'locatorFunction'; |
| 22 | + const TIMEOUT = 'timeout'; |
| 23 | + const PARAMETERIZED = 'parameterized'; |
27 | 24 |
|
28 | 25 | /** |
29 | | - * Singleton variable instance of class |
| 26 | + * Singleton instance of this class |
30 | 27 | * |
31 | 28 | * @var SectionObjectHandler |
32 | 29 | */ |
33 | | - private static $SECTION_DATA_PROCESSOR; |
| 30 | + private static $INSTANCE; |
34 | 31 |
|
35 | 32 | /** |
36 | | - * Array containing all Section Objects |
| 33 | + * All section objects. Set during initialize(). |
37 | 34 | * |
38 | | - * @var array |
| 35 | + * @var SectionObject[] |
39 | 36 | */ |
40 | | - private $sectionData = []; |
| 37 | + private $sectionObjects = []; |
41 | 38 |
|
42 | 39 | /** |
43 | | - * Singleton method to return SectionArrayProcesor. |
| 40 | + * Constructor |
44 | 41 | * |
45 | | - * @return SectionObjectHandler |
| 42 | + * @constructor |
46 | 43 | */ |
47 | | - public static function getInstance() |
| 44 | + private function __construct() |
48 | 45 | { |
49 | | - if (! self::$SECTION_DATA_PROCESSOR) { |
50 | | - self::$SECTION_DATA_PROCESSOR = new SectionObjectHandler(); |
51 | | - self::$SECTION_DATA_PROCESSOR->initSectionObjects(); |
| 46 | + $objectManager = ObjectManagerFactory::getObjectManager(); |
| 47 | + $parser = $objectManager->get(SectionParser::class); |
| 48 | + $parserOutput = $parser->getData(self::SECTION); |
| 49 | + |
| 50 | + if (!$parserOutput) { |
| 51 | + return; |
52 | 52 | } |
53 | 53 |
|
54 | | - return self::$SECTION_DATA_PROCESSOR; |
| 54 | + foreach ($parserOutput as $sectionName => $sectionData) { |
| 55 | + $elements = []; |
| 56 | + |
| 57 | + foreach ($sectionData[SectionObjectHandler::ELEMENT] as $elementName => $elementData) { |
| 58 | + $elementType = $elementData[SectionObjectHandler::TYPE]; |
| 59 | + $elementSelector = $elementData[SectionObjectHandler::SELECTOR] ?? null; |
| 60 | + $elementLocatorFunc = $elementData[SectionObjectHandler::LOCATOR_FUNCTION] ?? null; |
| 61 | + $elementTimeout = $elementData[SectionObjectHandler::TIMEOUT] ?? null; |
| 62 | + $elementParameterized = $elementData[SectionObjectHandler::PARAMETERIZED] ?? false; |
| 63 | + |
| 64 | + $elements[$elementName] = new ElementObject( |
| 65 | + $elementName, |
| 66 | + $elementType, |
| 67 | + $elementSelector, |
| 68 | + $elementLocatorFunc, |
| 69 | + $elementTimeout, |
| 70 | + $elementParameterized |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + $this->sectionObjects[$sectionName] = new SectionObject($sectionName, $elements); |
| 75 | + } |
55 | 76 | } |
56 | 77 |
|
57 | 78 | /** |
58 | | - * SectionObjectHandler constructor. |
59 | | - * @constructor |
| 79 | + * Initialize and/or return the singleton instance of this class |
| 80 | + * |
| 81 | + * @return SectionObjectHandler |
60 | 82 | */ |
61 | | - private function __construct() |
| 83 | + public static function getInstance() |
62 | 84 | { |
63 | | - // private constructor |
| 85 | + if (!self::$INSTANCE) { |
| 86 | + self::$INSTANCE = new SectionObjectHandler(); |
| 87 | + } |
| 88 | + |
| 89 | + return self::$INSTANCE; |
64 | 90 | } |
65 | 91 |
|
66 | 92 | /** |
67 | | - * Returns the corresponding section array parsed from xml. |
| 93 | + * Get a SectionObject by name |
68 | 94 | * |
69 | | - * @param string $sectionName |
| 95 | + * @param string $name The section name |
70 | 96 | * @return SectionObject | null |
71 | 97 | */ |
72 | | - public function getObject($sectionName) |
| 98 | + public function getObject($name) |
73 | 99 | { |
74 | | - if (array_key_exists($sectionName, $this->getAllObjects())) { |
75 | | - return $this->getAllObjects()[$sectionName]; |
| 100 | + if (array_key_exists($name, $this->getAllObjects())) { |
| 101 | + return $this->getAllObjects()[$name]; |
76 | 102 | } |
77 | 103 |
|
78 | 104 | return null; |
79 | 105 | } |
80 | 106 |
|
81 | 107 | /** |
82 | | - * Returns all section arrays parsed from section xml. |
| 108 | + * Get all SectionObjects |
83 | 109 | * |
84 | | - * @return array |
| 110 | + * @return SectionObject[] |
85 | 111 | */ |
86 | 112 | public function getAllObjects() |
87 | 113 | { |
88 | | - return $this->sectionData; |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Parse section objects if it's not previously done. |
93 | | - * |
94 | | - * @return void |
95 | | - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
96 | | - */ |
97 | | - private function initSectionObjects() |
98 | | - { |
99 | | - $objectManager = ObjectManagerFactory::getObjectManager(); |
100 | | - /** @var $parser \Magento\FunctionalTestingFramework\XmlParser\SectionParser */ |
101 | | - $parser = $objectManager->get(SectionParser::class); |
102 | | - $parsedObjs = $parser->getData(self::TYPE); |
103 | | - |
104 | | - if (!$parsedObjs) { |
105 | | - // No *Section.xml files found so give up |
106 | | - return; |
107 | | - } |
108 | | - |
109 | | - foreach ($parsedObjs as $sectionName => $sectionData) { |
110 | | - // create elements |
111 | | - $elements = []; |
112 | | - foreach ($sectionData[SectionObjectHandler::SUB_TYPE] as $elementName => $elementData) { |
113 | | - $elementType = $elementData[SectionObjectHandler::ELEMENT_TYPE_ATTR]; |
114 | | - $elementSelector = $elementData[SectionObjectHandler::ELEMENT_SELECTOR_ATTR] ?? null; |
115 | | - $elementLocatorFunc = $elementData[SectionObjectHandler::ELEMENT_LOCATOR_FUNC_ATTR] ?? null; |
116 | | - $elementTimeout = $elementData[SectionObjectHandler::ELEMENT_TIMEOUT_ATTR] ?? null; |
117 | | - $elementParameterized = $elementData[SectionObjectHandler::ELEMENT_PARAMETERIZED] ?? false; |
118 | | - |
119 | | - $elements[$elementName] = new ElementObject( |
120 | | - $elementName, |
121 | | - $elementType, |
122 | | - $elementSelector, |
123 | | - $elementLocatorFunc, |
124 | | - $elementTimeout, |
125 | | - $elementParameterized |
126 | | - ); |
127 | | - } |
128 | | - |
129 | | - $this->sectionData[$sectionName] = new SectionObject($sectionName, $elements); |
130 | | - } |
| 114 | + return $this->sectionObjects; |
131 | 115 | } |
132 | 116 | } |
0 commit comments