refactor given/when/then steps in noification context (#7358)

refactor given steps

refactor use of helper function in cross steps

return using inline variable
This commit is contained in:
Karun Atreya
2023-09-29 12:07:45 +05:45
committed by GitHub
parent c48b7e44ae
commit b7e3f2a718
@@ -93,16 +93,14 @@ class NotificationContext implements Context {
} }
/** /**
* @When /^user "([^"]*)" lists all notifications$/
*
* @param string $user * @param string $user
* *
* @return void * @return ResponseInterface
*/ */
public function userListAllNotifications(string $user):void { public function listAllNotifications(string $user):ResponseInterface {
$this->setUserRecipient($user); $this->setUserRecipient($user);
$headers = ["accept-language" => $this->settingsContext->getSettingLanguageValue($user)]; $headers = ["accept-language" => $this->settingsContext->getSettingLanguageValue($user)];
$response = OcsApiHelper::sendRequest( return OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(), $this->featureContext->getBaseUrl(),
$this->featureContext->getActualUsername($user), $this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user), $this->featureContext->getPasswordForUser($user),
@@ -113,10 +111,40 @@ class NotificationContext implements Context {
2, 2,
$headers $headers
); );
}
/**
* @When /^user "([^"]*)" lists all notifications$/
*
* @param string $user
*
* @return void
*/
public function userListAllNotifications(string $user):void {
$response = $this->listAllNotifications($user);
$this->featureContext->setResponse($response); $this->featureContext->setResponse($response);
$this->featureContext->pushToLastHttpStatusCodesArray(); $this->featureContext->pushToLastHttpStatusCodesArray();
} }
/**
* @param string $user
*
* @return ResponseInterface
* @throws GuzzleException
* @throws JsonException
*/
public function deleteAllNotifications(string $user):ResponseInterface {
$response = $this->listAllNotifications($user);
if (isset($this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data)) {
$responseBody = $this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data;
foreach ($responseBody as $value) {
// set notificationId
$this->notificationIds[] = $value->notification_id;
}
}
return $this->userDeletesNotification($user);
}
/** /**
* @When user :user deletes all notifications * @When user :user deletes all notifications
* *
@@ -127,15 +155,8 @@ class NotificationContext implements Context {
* @throws JsonException * @throws JsonException
*/ */
public function userDeletesAllNotifications(string $user):void { public function userDeletesAllNotifications(string $user):void {
$this->userListAllNotifications($user); $response = $this->deleteAllNotifications($user);
if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { $this->featureContext->setResponse($response);
$responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data;
foreach ($responseBody as $value) {
// set notificationId
$this->notificationIds[] = $value->notification_id;
}
}
$this->featureContext->setResponse($this->userDeletesNotification($user));
} }
/** /**
@@ -148,8 +169,8 @@ class NotificationContext implements Context {
* @throws JsonException * @throws JsonException
*/ */
public function userHasDeletedAllNotifications(string $user):void { public function userHasDeletedAllNotifications(string $user):void {
$this->userDeletesAllNotifications($user); $response = $this->deleteAllNotifications($user);
$this->featureContext->thenTheHTTPStatusCodeShouldBe(200); $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
} }
/** /**
@@ -164,8 +185,8 @@ class NotificationContext implements Context {
* @throws JsonException * @throws JsonException
*/ */
public function userDeletesNotificationOfResourceAndSubject(string $user, string $resource, string $subject):void { public function userDeletesNotificationOfResourceAndSubject(string $user, string $resource, string $subject):void {
$this->userListAllNotifications($user); $response = $this->listAllNotifications($user);
$this->filterResponseByNotificationSubjectAndResource($subject, $resource); $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response);
$this->featureContext->setResponse($this->userDeletesNotification($user)); $this->featureContext->setResponse($this->userDeletesNotification($user));
} }
@@ -174,7 +195,7 @@ class NotificationContext implements Context {
* *
* @param string $user * @param string $user
* *
* @return void * @return ResponseInterface
* @throws GuzzleException * @throws GuzzleException
* @throws JsonException * @throws JsonException
*/ */
@@ -281,13 +302,14 @@ class NotificationContext implements Context {
* filter notification according to subject * filter notification according to subject
* *
* @param string $subject * @param string $subject
* @param ResponseInterface|null $response
* *
* @return object * @return object
*/ */
public function filterResponseAccordingToNotificationSubject(string $subject): object { public function filterResponseAccordingToNotificationSubject(string $subject, ?ResponseInterface $response = null): object {
$responseBody = null; $response = $response ?? $this->featureContext->getResponse();
if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { if (isset($this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data)) {
$responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data;
foreach ($responseBody as $value) { foreach ($responseBody as $value) {
if (isset($value->subject) && $value->subject === $subject) { if (isset($value->subject) && $value->subject === $subject) {
$responseBody = $value; $responseBody = $value;
@@ -297,7 +319,7 @@ class NotificationContext implements Context {
} }
} }
} else { } else {
$responseBody = $this->featureContext->getJsonDecodedResponseBodyContent(); $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent($response);
} }
return $responseBody; return $responseBody;
} }
@@ -307,18 +329,20 @@ class NotificationContext implements Context {
* *
* @param string $subject * @param string $subject
* @param string $resource * @param string $resource
* @param ResponseInterface|null $response
* *
* @return array * @return array
*/ */
public function filterResponseByNotificationSubjectAndResource(string $subject, string $resource): array { public function filterResponseByNotificationSubjectAndResource(string $subject, string $resource, ?ResponseInterface $response = null): array {
$responseBodyArray = []; $responseBodyArray = [];
$statusCode = $this->featureContext->getResponse()->getStatusCode(); $response = $response ?? $this->featureContext->getResponse();
$statusCode = $response->getStatusCode();
if ($statusCode !== 200) { if ($statusCode !== 200) {
$response = $this->featureContext->getResponse()->getBody()->getContents(); $response = $response->getBody()->getContents();
Assert::fail($response . " Response should contain status code 200"); Assert::fail($response . " Response should contain status code 200");
} }
if (isset($this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data)) { if (isset($this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data)) {
$responseBody = $this->featureContext->getJsonDecodedResponseBodyContent()->ocs->data; $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data;
foreach ($responseBody as $value) { foreach ($responseBody as $value) {
if (isset($value->subject) && $value->subject === $subject && isset($value->messageRichParameters->resource->name) && $value->messageRichParameters->resource->name === $resource) { if (isset($value->subject) && $value->subject === $subject && isset($value->messageRichParameters->resource->name) && $value->messageRichParameters->resource->name === $resource) {
$this->notificationIds[] = $value->notification_id; $this->notificationIds[] = $value->notification_id;
@@ -326,7 +350,7 @@ class NotificationContext implements Context {
} }
} }
} else { } else {
$responseBodyArray[] = $this->featureContext->getJsonDecodedResponseBodyContent(); $responseBodyArray[] = $this->featureContext->getJsonDecodedResponseBodyContent($response);
Assert::fail("Response should contain notification but found: $responseBodyArray"); Assert::fail("Response should contain notification but found: $responseBodyArray");
} }
return $responseBodyArray; return $responseBodyArray;
@@ -351,12 +375,12 @@ class NotificationContext implements Context {
\sleep(1); \sleep(1);
} }
$this->featureContext->setResponse(null); $this->featureContext->setResponse(null);
$this->userListAllNotifications($user); $response = $this->listAllNotifications($user);
$this->featureContext->theHTTPStatusCodeShouldBe(200); $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
++$count; ++$count;
} while (!isset($this->filterResponseAccordingToNotificationSubject($subject)->message) && $count <= 5); } while (!isset($this->filterResponseAccordingToNotificationSubject($subject, $response)->message) && $count <= 5);
if (isset($this->filterResponseAccordingToNotificationSubject($subject)->message)) { if (isset($this->filterResponseAccordingToNotificationSubject($subject, $response)->message)) {
$actualMessage = str_replace(["\r", "\n"], " ", $this->filterResponseAccordingToNotificationSubject($subject)->message); $actualMessage = str_replace(["\r", "\n"], " ", $this->filterResponseAccordingToNotificationSubject($subject, $response)->message);
} else { } else {
throw new \Exception("Notification was not found even after retrying for 5 seconds."); throw new \Exception("Notification was not found even after retrying for 5 seconds.");
} }
@@ -380,8 +404,8 @@ class NotificationContext implements Context {
* @throws Exception * @throws Exception
*/ */
public function userShouldGetNotificationForResourceWithMessage(string $user, string $resource, string $subject, TableNode $table):void { public function userShouldGetNotificationForResourceWithMessage(string $user, string $resource, string $subject, TableNode $table):void {
$this->userListAllNotifications($user); $response = $this->listAllNotifications($user);
$notification = $this->filterResponseByNotificationSubjectAndResource($subject, $resource); $notification = $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response);
if (\count($notification) === 1) { if (\count($notification) === 1) {
$actualMessage = str_replace(["\r", "\r"], " ", $notification[0]->message); $actualMessage = str_replace(["\r", "\r"], " ", $notification[0]->message);
@@ -410,9 +434,9 @@ class NotificationContext implements Context {
* @return void * @return void
*/ */
public function userShouldNotHaveANotificationRelatedToResourceWithSubject(string $user, string $resource, string $subject):void { public function userShouldNotHaveANotificationRelatedToResourceWithSubject(string $user, string $resource, string $subject):void {
$this->userListAllNotifications($user); $response = $this->listAllNotifications($user);
$response = $this->filterResponseByNotificationSubjectAndResource($subject, $resource); $filteredResponse = $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response);
Assert::assertCount(0, $response, "Response should not contain notification related to resource '$resource' with subject '$subject' but found" . print_r($response, true)); Assert::assertCount(0, $filteredResponse, "Response should not contain notification related to resource '$resource' with subject '$subject' but found" . print_r($filteredResponse, true));
} }
/** /**
@@ -524,7 +548,7 @@ class NotificationContext implements Context {
* @param string|null $deprovision_date * @param string|null $deprovision_date
* @param string|null $deprovision_date_format * @param string|null $deprovision_date_format
* *
* @return void * @return ResponseInterface
* *
* @throws GuzzleException * @throws GuzzleException
* *
@@ -589,8 +613,8 @@ class NotificationContext implements Context {
* @return void * @return void
*/ */
public function userHasCreatedDeprovisioningNotification():void { public function userHasCreatedDeprovisioningNotification():void {
$this->userCreatesDeprovisioningNotification(); $response = $this->userCreatesDeprovisioningNotification();
$this->featureContext->thenTheHTTPStatusCodeShouldBe(200); $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
} }
/** /**