Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests-Only] Improve acceptance test error handling #37868

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 97 additions & 17 deletions tests/TestHelpers/AppConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static function wasCapabilitySet(
* @return string
*/
public static function getOCSResponse($response) {
return HttpRequestHelper::getResponseXml($response)->meta[0]->statuscode;
return HttpRequestHelper::getResponseXml($response, __METHOD__)->meta[0]->statuscode;
}

/**
Expand All @@ -225,7 +225,15 @@ public static function getCapabilities($baseUrl, $user, $password) {
'/cloud/capabilities',
null
);
self::assertEquals(200, $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
return $response;
}

Expand All @@ -235,7 +243,7 @@ public static function getCapabilities($baseUrl, $user, $password) {
* @return string retrieved capabilities in XML format
*/
public static function getCapabilitiesXml($response) {
return HttpRequestHelper::getResponseXml($response)->data->capabilities;
return HttpRequestHelper::getResponseXml($response, __METHOD__)->data->capabilities;
}

/**
Expand Down Expand Up @@ -264,10 +272,22 @@ public static function modifyAppConfig(
$body,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}
}
Expand Down Expand Up @@ -303,10 +323,22 @@ public static function modifyAppConfigs(
$body,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}
}
Expand Down Expand Up @@ -334,10 +366,22 @@ public static function deleteAppConfig(
$body,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}
}
Expand All @@ -364,10 +408,22 @@ public static function deleteAppConfigs(
$body,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}
}
Expand All @@ -393,14 +449,26 @@ public static function getAppConfigs(
null,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}

$responseXml = HttpRequestHelper::getResponseXml($response)->data[0];
$responseXml = HttpRequestHelper::getResponseXml($response, __METHOD__)->data[0];
$response = \json_decode(\json_encode($responseXml), true)['element'];
return $response;
}
Expand All @@ -427,14 +495,26 @@ public static function getAppConfig(
null,
$ocsApiVersion
);
self::assertEquals("200", $response->getStatusCode());

$expectedHttpStatus = 200;
$actualHttpStatus = $response->getStatusCode();

self::assertEquals(
$expectedHttpStatus,
$actualHttpStatus,
__METHOD__ . " expected HTTP status $expectedHttpStatus but got $actualHttpStatus"
);
if ($ocsApiVersion === 1) {
$expectedOcsStatus = "100";
$actualOcsStatus = self::getOCSResponse($response);
self::assertEquals(
"100", self::getOCSResponse($response)
$expectedOcsStatus,
$actualOcsStatus,
__METHOD__ . " expected OCS status $expectedOcsStatus but got $actualOcsStatus"
);
}

$responseXml = HttpRequestHelper::getResponseXml($response)->data[0];
$responseXml = HttpRequestHelper::getResponseXml($response, __METHOD__)->data[0];
$response = \json_decode(\json_encode($responseXml), true)['element'];
return $response;
}
Expand Down
13 changes: 9 additions & 4 deletions tests/TestHelpers/Asserts/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ class WebDav extends \PHPUnit\Framework\Assert {
* @param string $element exception|message|reason
* @param string $expectedValue
* @param array $responseXml
* @param string $extraErrorText
*
* @return void
* @throws \Exception
*/
public static function assertDavResponseElementIs(
$element, $expectedValue, $responseXml
$element, $expectedValue, $responseXml, $extraErrorText = ''
) {
if ($extraErrorText !== '') {
$extraErrorText = $extraErrorText . " ";
}
self::assertArrayHasKey(
'value', $responseXml, '$responseXml seems not to be a valid array'
'value',
$responseXml,
$extraErrorText . "responseXml does not have key 'value'"
);
if ($element === "exception") {
$result = $responseXml['value'][0]['value'];
Expand All @@ -54,7 +59,7 @@ public static function assertDavResponseElementIs(

self::assertEquals(
$expectedValue, $result,
"Expected '$expectedValue' in element $element got '$result'"
__METHOD__ . " " . $extraErrorText . "Expected '$expectedValue' in element $element got '$result'"
);
}

Expand Down
10 changes: 7 additions & 3 deletions tests/TestHelpers/HttpRequestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ public static function delete(
* | ocs | http://open-collaboration-services.org/ns |
*
* @param ResponseInterface $response
* @param string $exceptionText text to put at the front of exception messages
*
* @return SimpleXMLElement
* @throws \Exception
*/
public static function getResponseXml($response) {
public static function getResponseXml($response, $exceptionText = '') {
// rewind just to make sure we can re-parse it in case it was parsed already...
$response->getBody()->rewind();
$contents = $response->getBody()->getContents();
Expand All @@ -354,10 +355,13 @@ public static function getResponseXml($response) {
);
return $responseXmlObject;
} catch (\Exception $e) {
if ($exceptionText !== '') {
$exceptionText = $exceptionText . ' ';
}
if ($contents === '') {
throw new \Exception("Received empty response where XML was expected");
throw new \Exception($exceptionText . "Received empty response where XML was expected");
}
$message = "Exception parsing response body: \"" . $contents . "\"";
$message = $exceptionText . "Exception parsing response body: \"" . $contents . "\"";
throw new \Exception($message, 0, $e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/LoggingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function getLogFileContent(
"could not get logfile content " . $result->getReasonPhrase()
);
}
return HttpRequestHelper::getResponseXml($result)->data->element;
return HttpRequestHelper::getResponseXml($result, __METHOD__)->data->element;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestHelpers/SetupHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static function getSysInfo(
"could not get sysinfo " . $result->getReasonPhrase()
);
}
return HttpRequestHelper::getResponseXml($result)->data;
return HttpRequestHelper::getResponseXml($result, __METHOD__)->data;
}

/**
Expand Down Expand Up @@ -505,7 +505,7 @@ public static function readFileFromServer(
$response->getStatusCode(),
"Failed to read the file {$fileInCore}"
);
$localContent = HttpRequestHelper::getResponseXml($response);
$localContent = HttpRequestHelper::getResponseXml($response, __METHOD__);
$localContent = (string)$localContent->data->element->contentUrlEncoded;
return \urldecode($localContent);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ public static function readSkeletonFile(
$response->getStatusCode(),
"Failed to read the file {$fileInSkeletonFolder}"
);
$localContent = HttpRequestHelper::getResponseXml($response);
$localContent = HttpRequestHelper::getResponseXml($response, __METHOD__);
$localContent = (string)$localContent->data->element->contentUrlEncoded;
$localContent = \urldecode($localContent);
return $localContent;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/TagsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function requestTagsForUser(
$response = WebDavHelper::propfind(
$baseUrl, $user, $password, '/systemtags/', $properties, 1, "systemtags"
);
return HttpRequestHelper::getResponseXml($response);
return HttpRequestHelper::getResponseXml($response, __METHOD__);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestHelpers/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static function getGroupsAsArray(
$baseUrl, $adminUser, $adminPassword, $search =""
) {
$result = self::getGroups($baseUrl, $adminUser, $adminPassword, $search);
$groups = HttpRequestHelper::getResponseXml($result)->xpath(".//groups")[0];
$groups = HttpRequestHelper::getResponseXml($result, __METHOD__)->xpath(".//groups")[0];
$return = [];
foreach ($groups as $group) {
$return[] = $group->__toString();
Expand Down
10 changes: 8 additions & 2 deletions tests/TestHelpers/WebDavHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ public static function getMtimeOfFileinPublicLinkShare(
null,
$davVersionToUse
);
$responseXmlObject = HttpRequestHelper::getResponseXml($response);
$responseXmlObject = HttpRequestHelper::getResponseXml(
$response,
__METHOD__
);
$xmlPart = $responseXmlObject->xpath("//d:getlastmodified");

return $xmlPart[0]->__toString();
Expand All @@ -538,7 +541,10 @@ public static function getMtimeOfResource($user,
$response = self::propfind(
$baseUrl, $user, $password, $resource, ["getlastmodified"]
);
$responseXmlObject = HttpRequestHelper::getResponseXml($response);
$responseXmlObject = HttpRequestHelper::getResponseXml(
$response,
__METHOD__
);
$xmlpart = $responseXmlObject->xpath("//d:getlastmodified");
$mtime = new DateTime($xmlpart[0]->__toString());
return $mtime->format('U');
Expand Down
11 changes: 8 additions & 3 deletions tests/acceptance/features/bootstrap/AppConfigurationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function theCapabilitiesSettingOfAppParameterShouldBe(
*/
public function getAppParameter($capabilitiesApp, $capabilitiesPath) {
$answeredValue = $this->getParameterValueFromXml(
$this->getCapabilitiesXml(),
$this->getCapabilitiesXml(__METHOD__),
$capabilitiesApp,
$capabilitiesPath
);
Expand Down Expand Up @@ -189,10 +189,15 @@ public function theAdministratorGetsCapabilitiesCheckResponse() {
}

/**
* @param string $exceptionText text to put at the front of exception messages
*
* @return string latest retrieved capabilities in XML format
*/
public function getCapabilitiesXml() {
return $this->featureContext->getResponseXml()->data->capabilities;
public function getCapabilitiesXml($exceptionText = '') {
if ($exceptionText === '') {
$exceptionText = __METHOD__;
}
return $this->featureContext->getResponseXml(null, $exceptionText)->data->capabilities;
}

/**
Expand Down
Loading