Skip to content

Commit 9ccd984

Browse files
committed
MQE-260: ApiExecutor - Logging/Error detection for failed API Calls
1 parent fa11252 commit 9ccd984

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Magento/AcceptanceTestFramework/Util/ApiClientUtil.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class ApiClientUtil
4343
*/
4444
private $jsonBody;
4545

46+
/**
47+
* A list of successful HTTP responses that will not trigger an exception
48+
*
49+
* @var int[] SUCCESSFUL_HTTP_CODES
50+
*/
51+
const SUCCESSFUL_HTTP_CODES = [200, 201, 202, 203, 204, 205];
52+
4653
/**
4754
* ApiClientUtil constructor.
4855
* @param string $apiPath
@@ -65,6 +72,7 @@ public function __construct($apiPath, $headers, $apiOperation, $jsonBody)
6572
*
6673
* @param bool $verbose
6774
* @return string|bool
75+
* @throws \Exception
6876
*/
6977
public function submit($verbose = false)
7078
{
@@ -95,20 +103,11 @@ public function submit($verbose = false)
95103
CURLOPT_URL => $url . $this->apiPath
96104
]);
97105

98-
try {
99-
$response = curl_exec($this->curl);
100-
if ($response === false) {
101-
throw new \Exception(curl_error($this->curl), curl_errno($this->curl));
102-
}
103-
} catch (\Exception $e) {
104-
trigger_error(
105-
sprintf(
106-
'Curl failed with error #%d: %s',
107-
$e->getCode(),
108-
$e->getMessage()
109-
),
110-
E_USER_ERROR
111-
);
106+
$response = curl_exec($this->curl);
107+
$http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
108+
109+
if ($response === false || !in_array($http_code, ApiClientUtil::SUCCESSFUL_HTTP_CODES)) {
110+
throw new \Exception('API returned response code: ' . $http_code . ' Response:' . $response);
112111
}
113112

114113
curl_close($this->curl);

src/Magento/AcceptanceTestFramework/Util/TestGenerator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
389389
case "createData":
390390
$entity = $customActionAttributes['entity'];
391391
$key = $steps->getMergeKey();
392+
//Add an informative statement to help the user debug test runs
393+
$testSteps .= sprintf(
394+
"\t\t$%s->amGoingTo(\"create entity that has the mergeKey: %s\");\n",
395+
$actor,
396+
$key
397+
);
392398
//Get Entity from Static data.
393399
$testSteps .= sprintf(
394400
"\t\t$%s = DataObjectHandler::getInstance()->getObject(\"%s\");\n",
@@ -462,6 +468,12 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
462468
break;
463469
case "deleteData":
464470
$key = $customActionAttributes['createDataKey'];
471+
//Add an informative statement to help the user debug test runs
472+
$testSteps .= sprintf(
473+
"\t\t$%s->amGoingTo(\"delete entity that has the createDataKey: %s\");\n",
474+
$actor,
475+
$key
476+
);
465477

466478
if ($hookObject) {
467479
$testSteps .= sprintf("\t\t\$this->%s->deleteEntity();\n", $key);

0 commit comments

Comments
 (0)