Skip to content

Commit 7669e57

Browse files
committed
MQE-326: allow data persistence to custom stores.
1 parent bd0996a commit 7669e57

File tree

6 files changed

+91
-53
lines changed

6 files changed

+91
-53
lines changed

src/Magento/FunctionalTestingFramework/DataGenerator/Api/ApiExecutor.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,25 @@ class ApiExecutor
5757
*/
5858
private static $entitySequences = [];
5959

60+
/**
61+
* Store code in web api rest url.
62+
*
63+
* @var string
64+
*/
65+
private $storeCode;
66+
6067
/**
6168
* ApiSubObject constructor.
6269
* @param string $operation
6370
* @param EntityDataObject $entityObject
6471
* @param array $dependentEntities
72+
* @param string $storeCode
6573
*/
66-
public function __construct($operation, $entityObject, $dependentEntities = null)
74+
public function __construct($operation, $entityObject, $dependentEntities = null, $storeCode = 'default')
6775
{
6876
$this->operation = $operation;
6977
$this->entityObject = $entityObject;
78+
$this->storeCode = $storeCode;
7079
if ($dependentEntities != null) {
7180
foreach ($dependentEntities as $entity) {
7281
$this->dependentEntities[$entity->getName()] = $entity;
@@ -86,6 +95,7 @@ public function __construct($operation, $entityObject, $dependentEntities = null
8695
*/
8796
public function executeRequest()
8897
{
98+
$this->jsonDefinition->setStoreCode($this->storeCode);
8999
$apiClientUrl = $this->jsonDefinition->getApiUrl();
90100

91101
$matchedParams = [];

src/Magento/FunctionalTestingFramework/DataGenerator/Api/EntityApiHandler.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class EntityApiHandler
3434
*/
3535
private $dependentObjects = [];
3636

37+
/**
38+
* Store code in web api rest url.
39+
*
40+
* @var string
41+
*/
42+
private $storeCode;
43+
3744
/**
3845
* ApiPersistenceHandler constructor.
3946
* @param EntityDataObject $entityObject
@@ -43,15 +50,21 @@ public function __construct($entityObject, $dependentObjects = null)
4350
{
4451
$this->entityObject = clone $entityObject;
4552
$this->dependentObjects = $dependentObjects;
53+
$this->storeCode = 'default';
4654
}
4755

4856
/**
4957
* Function which executes a create request based on specific operation metadata
58+
*
59+
* @param string $storeCode
5060
* @return void
5161
*/
52-
public function createEntity()
62+
public function createEntity($storeCode = null)
5363
{
54-
$apiExecutor = new ApiExecutor('create', $this->entityObject, $this->dependentObjects);
64+
if (!$storeCode) {
65+
$storeCode = $this->storeCode;
66+
}
67+
$apiExecutor = new ApiExecutor('create', $this->entityObject, $this->dependentObjects, $storeCode);
5568
$result = $apiExecutor->executeRequest();
5669

5770
$this->createdObject = new EntityDataObject(
@@ -66,11 +79,15 @@ public function createEntity()
6679
/**
6780
* Function which executes a delete request based on specific operation metadata
6881
*
82+
* @param string $storeCode
6983
* @return string | false
7084
*/
71-
public function deleteEntity()
85+
public function deleteEntity($storeCode = null)
7286
{
73-
$apiExecutor = new ApiExecutor('delete', $this->createdObject);
87+
if (!$storeCode) {
88+
$storeCode = $this->storeCode;
89+
}
90+
$apiExecutor = new ApiExecutor('delete', $this->createdObject, null, $storeCode);
7491
$result = $apiExecutor->executeRequest();
7592

7693
return $result;

src/Magento/FunctionalTestingFramework/DataGenerator/Objects/JsonDefinition.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ class JsonDefinition
4040
private $apiMethod;
4141

4242
/**
43-
* Base URL for the request
43+
* Api request url.
4444
*
4545
* @var string
4646
*/
47-
private $baseUrl;
47+
private $apiUrl;
4848

4949
/**
5050
* Resource specific URI for the request
5151
*
5252
* @var string
5353
*/
54-
private $apiUrl;
54+
private $apiUri;
5555

5656
/**
5757
* Authorization path for retrieving a token
@@ -81,38 +81,49 @@ class JsonDefinition
8181
*/
8282
private $jsonMetadata = [];
8383

84+
/**
85+
* Store code in api url.
86+
*
87+
* @var string
88+
*/
89+
private $apiStoreCode;
90+
8491
/**
8592
* JsonDefinition constructor.
8693
* @param string $name
8794
* @param string $operation
8895
* @param string $dataType
8996
* @param string $apiMethod
90-
* @param string $apiUrl
97+
* @param string $apiUri
9198
* @param string $auth
9299
* @param array $headers
93100
* @param array $params
94101
* @param array $jsonMetadata
102+
* @param string $apiStoreCode
95103
*/
96104
public function __construct(
97105
$name,
98106
$operation,
99107
$dataType,
100108
$apiMethod,
101-
$apiUrl,
109+
$apiUri,
102110
$auth,
103111
$headers,
104112
$params,
105-
$jsonMetadata
113+
$jsonMetadata,
114+
$apiStoreCode = 'default'
106115
) {
107116
$this->name = $name;
108117
$this->operation = $operation;
109118
$this->dataType = $dataType;
110119
$this->apiMethod = $apiMethod;
111-
$this->baseUrl = $apiUrl;
120+
$this->apiUri = $apiUri;
112121
$this->auth = $auth;
113122
$this->headers = $headers;
114123
$this->params = $params;
115124
$this->jsonMetadata = $jsonMetadata;
125+
$this->apiStoreCode = $apiStoreCode;
126+
$this->apiUrl = null;
116127
}
117128

118129
/**
@@ -152,7 +163,7 @@ public function getApiMethod()
152163
*/
153164
public function getApiUrl()
154165
{
155-
$this->cleanApiUrl();
166+
$this->apiUrl = '/rest/' . $this->apiStoreCode . '/' . trim($this->apiUri, '/');
156167

157168
if (array_key_exists('path', $this->params)) {
158169
$this->addPathParam();
@@ -172,7 +183,7 @@ public function getApiUrl()
172183
*/
173184
public function getAuth()
174185
{
175-
return $this->auth;
186+
return '/rest/' . $this->apiStoreCode . '/' . trim($this->auth, '/');
176187
}
177188

178189
/**
@@ -196,17 +207,14 @@ public function getJsonMetadata()
196207
}
197208

198209
/**
199-
* Function to validate api format and add "/" char where necessary
210+
* Set store code.
200211
*
212+
* @param $newStoreCode
201213
* @return void
202214
*/
203-
private function cleanApiUrl()
215+
public function setStoreCode($newStoreCode)
204216
{
205-
if (substr($this->baseUrl, -1) == "/") {
206-
$this->apiUrl = rtrim($this->baseUrl, "/");
207-
} else {
208-
$this->apiUrl = $this->baseUrl;
209-
}
217+
$this->apiStoreCode = $newStoreCode;
210218
}
211219

212220
/**
@@ -228,15 +236,14 @@ private function addPathParam()
228236
*/
229237
private function addQueryParams()
230238
{
231-
232239
foreach ($this->params['query'] as $paramName => $paramValue) {
233-
if (!stringContains("?", $this->apiUrl)) {
240+
if (strpos($this->apiUrl, '?') == false) {
234241
$this->apiUrl = $this->apiUrl . "?";
235242
} else {
236243
$this->apiUrl = $this->apiUrl . "&";
237244
}
238245

239-
$this->apiUrl = $paramName . "=" . $paramValue;
246+
$this->apiUrl = $this->apiUrl . $paramName . "=" . $paramValue;
240247
}
241248
}
242249
}

src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataOperation.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<xs:attribute type="xs:string" name="url"/>
1919
<xs:attribute type="xs:string" name="auth"/>
2020
<xs:attribute type="xs:string" name="method"/>
21+
<xs:attribute type="xs:string" name="storeCode" default="default"/>
2122
</xs:complexType>
2223
</xs:element>
2324
</xs:sequence>

src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
<xs:attribute type="xs:boolean" name="remove" default="false"/>
362362
<xs:attribute type="xs:string" name="before"/>
363363
<xs:attribute type="xs:string" name="after"/>
364+
<xs:attribute type="xs:string" name="storeCode"/>
364365
</xs:complexType>
365366
<xs:complexType name="requiredEntityType">
366367
<xs:simpleContent>
@@ -380,6 +381,7 @@
380381
<xs:attribute type="xs:boolean" name="remove" default="false"/>
381382
<xs:attribute type="xs:string" name="before"/>
382383
<xs:attribute type="xs:string" name="after"/>
384+
<xs:attribute type="xs:string" name="storeCode"/>
383385
</xs:extension>
384386
</xs:simpleContent>
385387
</xs:complexType>

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,29 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
430430
}
431431
}
432432
}
433+
434+
if ($hookObject) {
435+
$createEntityFunctionCall = sprintf("\t\t\$this->%s->createEntity(", $key);
436+
$entityApiHandlerFunctionCall = sprintf(
437+
"\t\t\$this->%s = new EntityApiHandler($%s",
438+
$key,
439+
$entity
440+
);
441+
} else {
442+
$createEntityFunctionCall = sprintf("\t\t\$%s->createEntity(", $key);
443+
$entityApiHandlerFunctionCall = sprintf(
444+
"\t\t$%s = new EntityApiHandler($%s",
445+
$key,
446+
$entity
447+
);
448+
}
449+
450+
if (isset($customActionAttributes['storeCode'])) {
451+
$createEntityFunctionCall .= sprintf("\"%s\");\n", $customActionAttributes['storeCode']);
452+
} else {
453+
$createEntityFunctionCall .= ");\n";
454+
}
455+
433456
//If required-entities are defined, reassign dataObject to not overwrite the static definition.
434457
//Also, EntityApiHandler needs to be defined with customData array.
435458
if (!empty($requiredEntities)) {
@@ -445,37 +468,15 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
445468
$entity
446469
);
447470

448-
if ($hookObject) {
449-
$testSteps .= sprintf(
450-
"\t\t\$this->%s = new EntityApiHandler($%s, [%s]);\n",
451-
$key,
452-
$entity,
453-
implode(', ', $requiredEntityObjects)
454-
);
455-
$testSteps .= sprintf("\t\t\$this->%s->createEntity();\n", $key);
456-
} else {
457-
$testSteps .= sprintf(
458-
"\t\t$%s = new EntityApiHandler($%s, [%s]);\n",
459-
$key,
460-
$entity,
461-
implode(', ', $requiredEntityObjects)
462-
);
463-
$testSteps .= sprintf("\t\t$%s->createEntity();\n", $key);
464-
}
471+
$entityApiHandlerFunctionCall .= sprintf(
472+
", [%s]);\n",
473+
implode(', ', $requiredEntityObjects)
474+
);
465475
} else {
466-
if ($hookObject) {
467-
$testSteps .= sprintf(
468-
"\t\t\$this->%s = new EntityApiHandler($%s);\n",
469-
$key,
470-
$entity
471-
);
472-
$testSteps .= sprintf("\t\t\$this->%s->createEntity();\n", $key);
473-
} else {
474-
$testSteps .= sprintf("\t\t$%s = new EntityApiHandler($%s);\n", $key, $entity);
475-
$testSteps .= sprintf("\t\t$%s->createEntity();\n", $key);
476-
}
476+
$entityApiHandlerFunctionCall .= ");\n";
477477
}
478-
478+
$testSteps .= $entityApiHandlerFunctionCall;
479+
$testSteps .= $createEntityFunctionCall;
479480
break;
480481
case "deleteData":
481482
$key = $customActionAttributes['createDataKey'];

0 commit comments

Comments
 (0)