Skip to content

Commit 9e6df12

Browse files
committed
Merge branch 'MQE-638' into sprint-develop
2 parents 4be6d41 + d8bd2ce commit 9e6df12

File tree

2 files changed

+67
-83
lines changed

2 files changed

+67
-83
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Page/Handlers/SectionObjectHandlerTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1212
use Magento\FunctionalTestingFramework\Page\Handlers\SectionObjectHandler;
1313
use Magento\FunctionalTestingFramework\XmlParser\SectionParser;
14-
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
1514
use PHPUnit\Framework\TestCase;
1615

1716
class SectionObjectHandlerTest extends TestCase
@@ -35,7 +34,9 @@ public function testGetSectionObject()
3534
"selector" => "#element"
3635
]
3736
]
38-
]];
37+
]
38+
];
39+
3940
$this->setMockParserOutput($mockData);
4041

4142
// get sections
@@ -52,20 +53,19 @@ public function testGetSectionObject()
5253
}
5354

5455
/**
55-
* Function used to set mock for parser return and force init method to run between tests.
56+
* Set the mock parser return value
5657
*
5758
* @param array $data
5859
*/
5960
private function setMockParserOutput($data)
6061
{
6162
// clear section object handler value to inject parsed content
62-
$property = new \ReflectionProperty(SectionObjectHandler::class, 'SECTION_DATA_PROCESSOR');
63+
$property = new \ReflectionProperty(SectionObjectHandler::class, "INSTANCE");
6364
$property->setAccessible(true);
6465
$property->setValue(null);
6566

6667
$mockSectionParser = AspectMock::double(SectionParser::class, ["getData" => $data])->make();
67-
$instance = AspectMock::double(ObjectManager::class, ['get' => $mockSectionParser])->make();
68-
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
69-
68+
$instance = AspectMock::double(ObjectManager::class, ["get" => $mockSectionParser])->make();
69+
AspectMock::double(ObjectManagerFactory::class, ["getObjectManager" => $instance]);
7070
}
7171
}

src/Magento/FunctionalTestingFramework/Page/Handlers/SectionObjectHandler.php

+60-76
Original file line numberDiff line numberDiff line change
@@ -12,121 +12,105 @@
1212
use Magento\FunctionalTestingFramework\Page\Objects\SectionObject;
1313
use Magento\FunctionalTestingFramework\XmlParser\SectionParser;
1414

15-
/**
16-
* Class SectionObjectHandler
17-
*/
1815
class SectionObjectHandler implements ObjectHandlerInterface
1916
{
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';
2724

2825
/**
29-
* Singleton variable instance of class
26+
* Singleton instance of this class
3027
*
3128
* @var SectionObjectHandler
3229
*/
33-
private static $SECTION_DATA_PROCESSOR;
30+
private static $INSTANCE;
3431

3532
/**
36-
* Array containing all Section Objects
33+
* All section objects. Set during initialize().
3734
*
38-
* @var array
35+
* @var SectionObject[]
3936
*/
40-
private $sectionData = [];
37+
private $sectionObjects = [];
4138

4239
/**
43-
* Singleton method to return SectionArrayProcesor.
40+
* Constructor
4441
*
45-
* @return SectionObjectHandler
42+
* @constructor
4643
*/
47-
public static function getInstance()
44+
private function __construct()
4845
{
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;
5252
}
5353

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+
}
5576
}
5677

5778
/**
58-
* SectionObjectHandler constructor.
59-
* @constructor
79+
* Initialize and/or return the singleton instance of this class
80+
*
81+
* @return SectionObjectHandler
6082
*/
61-
private function __construct()
83+
public static function getInstance()
6284
{
63-
// private constructor
85+
if (!self::$INSTANCE) {
86+
self::$INSTANCE = new SectionObjectHandler();
87+
}
88+
89+
return self::$INSTANCE;
6490
}
6591

6692
/**
67-
* Returns the corresponding section array parsed from xml.
93+
* Get a SectionObject by name
6894
*
69-
* @param string $sectionName
95+
* @param string $name The section name
7096
* @return SectionObject | null
7197
*/
72-
public function getObject($sectionName)
98+
public function getObject($name)
7399
{
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];
76102
}
77103

78104
return null;
79105
}
80106

81107
/**
82-
* Returns all section arrays parsed from section xml.
108+
* Get all SectionObjects
83109
*
84-
* @return array
110+
* @return SectionObject[]
85111
*/
86112
public function getAllObjects()
87113
{
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;
131115
}
132116
}

0 commit comments

Comments
 (0)