From d3e8b71423dca3496d3c558df3d07e836bdc7079 Mon Sep 17 00:00:00 2001 From: Saw-jan Date: Thu, 28 Nov 2024 12:27:35 +0545 Subject: [PATCH] test: do not use unnecessary array --- .../bootstrap/CollaborationContext.php | 4 +-- .../bootstrap/NotificationContext.php | 36 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/tests/acceptance/bootstrap/CollaborationContext.php b/tests/acceptance/bootstrap/CollaborationContext.php index d6772eea2..bf44f9660 100644 --- a/tests/acceptance/bootstrap/CollaborationContext.php +++ b/tests/acceptance/bootstrap/CollaborationContext.php @@ -423,7 +423,7 @@ class CollaborationContext implements Context { $file ); $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response); - $decodedResponse[] = $this->featureContext->getJsonDecodedResponseBodyContent($response); - return $decodedResponse[0]->file_id; + $decodedResponse = $this->featureContext->getJsonDecodedResponseBodyContent($response); + return $decodedResponse->file_id; } } diff --git a/tests/acceptance/bootstrap/NotificationContext.php b/tests/acceptance/bootstrap/NotificationContext.php index 97dbf043e..0aa848730 100644 --- a/tests/acceptance/bootstrap/NotificationContext.php +++ b/tests/acceptance/bootstrap/NotificationContext.php @@ -204,7 +204,7 @@ class NotificationContext implements Context { */ public function userDeletesNotificationOfResourceAndSubject(string $user, string $resource, string $subject):void { $response = $this->listAllNotifications($user); - $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response); + $this->filterNotificationsBySubjectAndResource($subject, $resource, $response); $this->featureContext->setResponse($this->userDeletesNotification($user)); } @@ -351,27 +351,23 @@ class NotificationContext implements Context { * * @return array */ - public function filterResponseByNotificationSubjectAndResource(string $subject, string $resource, ?ResponseInterface $response = null): array { - $responseBodyArray = []; + public function filterNotificationsBySubjectAndResource(string $subject, string $resource, ?ResponseInterface $response = null): array { + $filteredNotifications = []; $response = $response ?? $this->featureContext->getResponse(); - $statusCode = $response->getStatusCode(); - if ($statusCode !== 200) { - $response = $response->getBody()->getContents(); - Assert::fail($response . " Response should contain status code 200"); + $responseObject = $this->featureContext->getJsonDecodedResponseBodyContent($response); + + if (!isset($responseObject->ocs->data)) { + Assert::fail("Response doesn't contain notification: " . print_r($responseObject, true)); } - if (isset($this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data)) { - $responseBody = $this->featureContext->getJsonDecodedResponseBodyContent($response)->ocs->data; - foreach ($responseBody as $value) { - if (isset($value->subject) && $value->subject === $subject && isset($value->messageRichParameters->resource->name) && $value->messageRichParameters->resource->name === $resource) { - $this->notificationIds[] = $value->notification_id; - $responseBodyArray[] = $value; - } + + $notifications = $responseObject->ocs->data; + foreach ($notifications as $notification) { + if (isset($notification->subject) && $notification->subject === $subject && isset($notification->messageRichParameters->resource->name) && $notification->messageRichParameters->resource->name === $resource) { + $this->notificationIds[] = $notification->notification_id; + $filteredNotifications[] = $notification; } - } else { - $responseBodyArray[] = $this->featureContext->getJsonDecodedResponseBodyContent($response); - Assert::fail("Response should contain notification but found: " . print_r($responseBodyArray, true)); } - return $responseBodyArray; + return $filteredNotifications; } /** @@ -423,7 +419,7 @@ class NotificationContext implements Context { */ public function userShouldGetNotificationForResourceWithMessage(string $user, string $resource, string $subject, TableNode $table):void { $response = $this->listAllNotifications($user); - $notification = $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response); + $notification = $this->filterNotificationsBySubjectAndResource($subject, $resource, $response); if (\count($notification) === 1) { $actualMessage = str_replace(["\r", "\r"], " ", $notification[0]->message); @@ -453,7 +449,7 @@ class NotificationContext implements Context { */ public function userShouldNotHaveANotificationRelatedToResourceWithSubject(string $user, string $resource, string $subject):void { $response = $this->listAllNotifications($user); - $filteredResponse = $this->filterResponseByNotificationSubjectAndResource($subject, $resource, $response); + $filteredResponse = $this->filterNotificationsBySubjectAndResource($subject, $resource, $response); Assert::assertCount(0, $filteredResponse, "Response should not contain notification related to resource '$resource' with subject '$subject' but found" . print_r($filteredResponse, true)); }