Skip to content

Commit 1dfb85e

Browse files
committed
MQE-34:[Data Input] Data Generator
- add relevant Entity and Field Group Objects - add interface for persistence layer - add entity schema for data parser - add sample xml file
1 parent a324431 commit 1dfb85e

File tree

17 files changed

+585
-7
lines changed

17 files changed

+585
-7
lines changed

bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
defined('FW_BP') || define('FW_BP', str_replace('\\', '/', (__DIR__)));
44
defined('TESTS_BP') || define('TESTS_BP', dirname(dirname(dirname(FW_BP))));
5+
putenv("HOSTNAME=127.0.0.1");
6+
putenv("PORT=8080");
57

68
require_once __DIR__ . '/vendor/autoload.php';
79

entryPoint.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
require_once 'bootstrap.php';
99

1010
/** @var Magento\AcceptanceTestFramework\Dummy $dummy */
11-
$dummy = $objectManager->create(\Magento\AcceptanceTestFramework\Dummy::class);
12-
$dummy->readPageObjects();
11+
$dummy = new Magento\AcceptanceTestFramework\DataGenerator\DataHandler('test');
12+
13+
$result = $dummy->persistData(['CustomerEntityTwo'], 'API');
14+
15+
print_r($result);

etc/di.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,36 @@
9595
<argument name="reader" xsi:type="object">Magento\AcceptanceTestFramework\Config\Reader\Page</argument>
9696
</arguments>
9797
</virtualType>
98+
<virtualType name="Magento\AcceptanceTestFramework\DataProfile\Config\Data" type="Magento\AcceptanceTestFramework\Config\Data">
99+
<arguments>
100+
<argument name="reader" xsi:type="object">Magento\AcceptanceTestFramework\Config\Reader\DataProfile</argument>
101+
</arguments>
102+
</virtualType>
103+
104+
<virtualType name="Magento\AcceptanceTestFramework\Config\SchemaLocator\DataProfile" type="Magento\AcceptanceTestFramework\Config\SchemaLocator">
105+
<arguments>
106+
<argument name="schemaPath" xsi:type="string">Magento/AcceptanceTestFramework/DataGenerator/etc/DataProfileSchema.xsd</argument>
107+
</arguments>
108+
</virtualType>
109+
110+
<virtualType name="Magento\AcceptanceTestFramework\Config\Reader\DataProfile" type="Magento\AcceptanceTestFramework\Config\Reader\Filesystem">
111+
<arguments>
112+
<argument name="fileResolver" xsi:type="object">Magento\AcceptanceTestFramework\Config\FileResolver\Module</argument>
113+
<argument name="converter" xsi:type="object">Magento\AcceptanceTestFramework\Config\Converter</argument>
114+
<argument name="schemaLocator" xsi:type="object">Magento\AcceptanceTestFramework\Config\SchemaLocator\DataProfile</argument>
115+
<argument name="idAttributes" xsi:type="array">
116+
<item name="/config/entities/entity" xsi:type="string">name</item>
117+
<item name="/config/entities/entity/dataObjects/dataObject" xsi:type="string">name</item>
118+
<item name="/config/entities/entity/dataObjects/dataObject/dataArray/data" xsi:type="string">key</item>
119+
</argument>
120+
<argument name="fileName" xsi:type="string">data.xml</argument>
121+
<argument name="defaultScope" xsi:type="string">*/etc</argument>
122+
</arguments>
123+
</virtualType>
124+
125+
<type name="Magento\AcceptanceTestFramework\DataProfileSchemaParser">
126+
<arguments>
127+
<argument name="dataProfiles" xsi:type="object">Magento\AcceptanceTestFramework\DataProfile\Config\Data</argument>
128+
</arguments>
129+
</type>
98130
</config>

src/Magento/AcceptanceTestFramework/Config/FileResolver/Module.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function get($filename, $scope)
4141
$paths = [];
4242
foreach ($modulesPath as $modulePath) {
4343
$path = $modulePath . DIRECTORY_SEPARATOR . $scope . DIRECTORY_SEPARATOR . $filename;
44-
if (is_readable($path)) {
45-
$paths[] = $path;
46-
}
44+
$paths = array_merge($paths, glob($path));
4745
}
4846

4947
$iterator = new File($paths);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Magento\AcceptanceTestFramework\DataGenerator;
4+
5+
class DataGeneratorXMLConstants
6+
{
7+
const ENTITY_DATA = 'entity';
8+
const ENTITY_DATA_NAME = 'name';
9+
const ENTITY_DATA_TYPE = 'type';
10+
const ENTITY_DATA_CONFIG = 'scenario';
11+
12+
const DATA_CONFIG_VALUE = 'value';
13+
14+
const DATA_OBJECT = 'fieldGroup';
15+
const DATA_OBJECT_DATA = 'data';
16+
const DATA_ELEMENT_KEY = 'key';
17+
const DATA_ELEMENT_VALUE = 'value';
18+
19+
const DATA_OBJECT_ASSERTS = 'assert';
20+
const ASSERT_VALUE = 'value';
21+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Magento\AcceptanceTestFramework\DataGenerator;
4+
5+
use Magento\AcceptanceTestFramework\DataGenerator\Objects\EntityXmlObject;
6+
use Magento\AcceptanceTestFramework\DataProfileSchemaParser;
7+
use Magento\AcceptanceTestFramework\ObjectManagerFactory;
8+
9+
class DataHandler
10+
{
11+
private $moduleName;
12+
private $objectManager;
13+
const API_CLASS_PATH = "Magento\AcceptanceTestFramework\DataGenerator\DataModel\ApiModel";
14+
15+
public function __construct($moduleName)
16+
{
17+
$this->moduleName = $moduleName;
18+
}
19+
20+
public function generateData($mapEntities = false)
21+
{
22+
$entityObjects = array();
23+
$this->objectManager = ObjectManagerFactory::getObjectManager();
24+
$entityParser = $this->objectManager->create(DataProfileSchemaParser::class);
25+
$entities = $entityParser->readDataProfiles();
26+
27+
foreach ($entities[DataGeneratorXMLConstants::ENTITY_DATA] as $entityName => $entity) {
28+
$entityXmlObject = new EntityXmlObject(
29+
$entityName,
30+
$entity[DataGeneratorXMLConstants::ENTITY_DATA_TYPE],
31+
$entity[DataGeneratorXMLConstants::ENTITY_DATA_CONFIG],
32+
$entity[DataGeneratorXMLConstants::DATA_OBJECT]
33+
);
34+
35+
if ($mapEntities) {
36+
$entityObjects[$entityXmlObject->getName()] = $entityXmlObject;
37+
} else {
38+
$entityObjects[] = $entityXmlObject;
39+
}
40+
}
41+
42+
return $entityObjects;
43+
}
44+
45+
public function persistData($entityNames, $inputMethod)
46+
{
47+
$entityObjects = $this->generateData(true);
48+
$relevantEntities = array_intersect_key($entityObjects, array_flip($entityNames));
49+
50+
foreach ($relevantEntities as $relevantEntity) {
51+
if ($inputMethod == 'API') {
52+
return $this->createApiModel($relevantEntity)->create();
53+
}
54+
}
55+
}
56+
57+
private function createApiModel($entity)
58+
{
59+
$apiClass = self::API_CLASS_PATH . "\\" . $entity->getType();
60+
$apiObject = new $apiClass($entity);
61+
return $apiObject;
62+
}
63+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Created by PhpStorm.
5+
* User: kkozan
6+
* Date: 6/13/17
7+
* Time: 11:30 AM
8+
*/
9+
use Codeception\Lib\Connector\Guzzle;
10+
11+
class Address
12+
{
13+
14+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Magento\AcceptanceTestFramework\DataGenerator\DataModel\ApiModel;
4+
5+
use Magento\AcceptanceTestFramework\DataGenerator\DataModel\EntityPersistenceInterface;
6+
use Magento\AcceptanceTestFramework\Helper\EntityRESTApiHelper;
7+
8+
class Customer implements EntityPersistenceInterface
9+
{
10+
private $entityObject;
11+
private static $entityRESTApiHelper;
12+
private static $specialDefinitions = ["address", "customattributes"];
13+
14+
const CUSTOMER_CREATE_API_PATH = '/rest/V1/customers/1';
15+
16+
public function __construct($entityObject)
17+
{
18+
$this->entityObject = $entityObject;
19+
20+
if (!self::$entityRESTApiHelper) {
21+
self::$entityRESTApiHelper = new EntityRESTApiHelper(getenv('HOSTNAME'), getenv('PORT'));
22+
}
23+
}
24+
25+
public function create()
26+
{
27+
$response = self::$entityRESTApiHelper->submitAuthAPiRequest(
28+
'PUT',
29+
self::CUSTOMER_CREATE_API_PATH,
30+
$this->entityDataToJson(),
31+
EntityRESTApiHelper::APPLICATION_JSON_HEADER
32+
);
33+
34+
return $response;
35+
}
36+
37+
public function delete()
38+
{
39+
// TODO implement delete method via customer web-api;
40+
}
41+
42+
private function entityDataToJson()
43+
{
44+
$data = $this->entityObject->getData();
45+
46+
foreach (self::$specialDefinitions as $specialKey) {
47+
if (array_key_exists($specialKey, $data)) {
48+
// logic to handle special case (new obj?)
49+
}
50+
}
51+
52+
$entityArray = [
53+
strtolower($this->entityObject->getType()) => $data,
54+
55+
// this passwordHash param is necessary to create a new customer.
56+
'passwordHash' => 'someHash'];
57+
58+
$json = \GuzzleHttp\json_encode($entityArray);
59+
60+
return $json;
61+
}
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Magento\AcceptanceTestFramework\DataGenerator\DataModel;
4+
5+
interface EntityPersistenceInterface
6+
{
7+
/**
8+
* EntityPersistenceInterface constructor. Needs entityObject.
9+
* @param $entityObject
10+
*/
11+
public function __construct($entityObject);
12+
13+
/**
14+
* Inserts entity record.
15+
* @return mixed
16+
*/
17+
public function create();
18+
19+
/**
20+
* Deletes entity record.
21+
* @return mixed
22+
*/
23+
public function delete();
24+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Magento\AcceptanceTestFramework\DataGenerator\Objects;
4+
5+
use Magento\AcceptanceTestFramework\DataGenerator\DataGeneratorXMLConstants;
6+
7+
class EntityXmlObject
8+
{
9+
private $name;
10+
private $type;
11+
private $dataConfigs = array();
12+
private $fieldGroups = array(); //array of data objects map of DataObject Name to actual Data Name
13+
private $data = array(); //array of Data Name to Data Value
14+
15+
16+
public function __construct($entityName, $entityType, $dataConfigs, $dataObjects)
17+
{
18+
$this->name = $entityName;
19+
$this->type = $entityType;
20+
21+
foreach ($dataConfigs as $dataConfig) {
22+
$this->dataConfigs[] = $dataConfig[DataGeneratorXMLConstants::DATA_CONFIG_VALUE];
23+
}
24+
25+
foreach ($dataObjects as $fieldGroupName => $fieldGroupObject) {
26+
$dataNames = array(); // array to store names of data per fieldGroupObject
27+
$assertions = array(); // array to store assertions
28+
29+
foreach ($fieldGroupObject[DataGeneratorXMLConstants::DATA_OBJECT_DATA] as $dataElement) {
30+
$dataNames[] = $dataElement[DataGeneratorXMLConstants::DATA_ELEMENT_KEY];
31+
$this->data[$dataElement[DataGeneratorXMLConstants::DATA_ELEMENT_KEY]] =
32+
$dataElement[DataGeneratorXMLConstants::DATA_ELEMENT_VALUE];
33+
}
34+
35+
foreach ($fieldGroupObject[DataGeneratorXMLConstants::DATA_OBJECT_ASSERTS] as $assertion) {
36+
$assertions[] = $assertion[DataGeneratorXMLConstants::ASSERT_VALUE];
37+
}
38+
39+
40+
$fieldGroupXmlObject = new FieldGroupXmlObject($fieldGroupName, $assertions, $dataNames);
41+
$this->fieldGroups[$fieldGroupXmlObject->getName()] = $fieldGroupXmlObject;
42+
}
43+
}
44+
45+
public function getName()
46+
{
47+
return $this->name;
48+
}
49+
50+
public function getType()
51+
{
52+
return $this->type;
53+
}
54+
55+
public function getFieldGroups()
56+
{
57+
return $this->fieldGroups;
58+
}
59+
60+
public function getData()
61+
{
62+
return $this->data;
63+
}
64+
}

0 commit comments

Comments
 (0)