From 1c680fa3992be89a747f59e73d957b76798892e3 Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Mon, 4 Sep 2023 11:57:45 +0545 Subject: [PATCH] refacor given when and then steps in checksum and favorite context making helper function, checking and setting response in given step and when steo respectively returning response and using in given/then steps as required in favoritesContext set the returned response on SpaceContext changed to inline variable in some lines as required to check for the specific https response code --- .../features/bootstrap/ChecksumContext.php | 120 ++++++++++++------ .../features/bootstrap/FavoritesContext.php | 43 +++---- .../features/bootstrap/SpacesContext.php | 2 +- 3 files changed, 104 insertions(+), 61 deletions(-) diff --git a/tests/acceptance/features/bootstrap/ChecksumContext.php b/tests/acceptance/features/bootstrap/ChecksumContext.php index 17f8496b070..fd7f08768e7 100644 --- a/tests/acceptance/features/bootstrap/ChecksumContext.php +++ b/tests/acceptance/features/bootstrap/ChecksumContext.php @@ -23,6 +23,7 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; use PHPUnit\Framework\Assert; use TestHelpers\WebDavHelper; +use Psr\Http\Message\ResponseInterface; require_once 'bootstrap.php'; @@ -33,32 +34,48 @@ class ChecksumContext implements Context { private FeatureContext $featureContext; /** - * @When user :user uploads file :source to :destination with checksum :checksum using the WebDAV API - * * @param string $user * @param string $source * @param string $destination * @param string $checksum * - * @return void + * @return ResponseInterface */ - public function userUploadsFileToWithChecksumUsingTheAPI( + public function uploadFileToWithChecksumUsingTheAPI( string $user, string $source, string $destination, string $checksum - ):void { + ):ResponseInterface { $file = \file_get_contents( $this->featureContext->acceptanceTestsDirLocation() . $source ); - $response = $this->featureContext->makeDavRequest( + return $this->featureContext->makeDavRequest( $user, 'PUT', $destination, ['OC-Checksum' => $checksum], $file ); - $this->featureContext->setResponse($response); + } + + /** + * @When user :user uploads file :source to :destination with checksum :checksum using the WebDAV API + * + * @param string $user + * @param string $source + * @param string $destination + * @param string $checksum + * + * @return void + */ + public function userUploadsFileToWithChecksumUsingTheAPI( + string $user, + string $source, + string $destination, + string $checksum + ):void { + $this->featureContext->setResponse($this->uploadFileToWithChecksumUsingTheAPI($user, $source, $destination, $checksum)); } /** @@ -78,39 +95,55 @@ public function userHasUploadedFileToWithChecksumUsingTheAPI( string $checksum ):void { $user = $this->featureContext->getActualUsername($user); - $this->userUploadsFileToWithChecksumUsingTheAPI( + $response = $this->uploadFileToWithChecksumUsingTheAPI( $user, $source, $destination, $checksum ); - $this->featureContext->theHTTPStatusCodeShouldBeSuccess(); + $this->featureContext->theHTTPStatusCodeShouldBe([201,204], '', $response); } /** - * @When user :user uploads file with content :content and checksum :checksum to :destination using the WebDAV API - * * @param string $user * @param string $content * @param string $checksum * @param string $destination * - * @return void + * @return ResponseInterface */ - public function userUploadsFileWithContentAndChecksumToUsingTheAPI( + public function uploadFileWithContentAndChecksumToUsingTheAPI( string $user, string $content, string $checksum, string $destination - ):void { - $response = $this->featureContext->makeDavRequest( + ):ResponseInterface { + return $this->featureContext->makeDavRequest( $user, 'PUT', $destination, ['OC-Checksum' => $checksum], $content ); - $this->featureContext->setResponse($response); + } + + /** + * @When user :user uploads file with content :content and checksum :checksum to :destination using the WebDAV API + * + * @param string $user + * @param string $content + * @param string $checksum + * @param string $destination + * + * @return void + */ + public function userUploadsFileWithContentAndChecksumToUsingTheAPI( + string $user, + string $content, + string $checksum, + string $destination + ):void { + $this->featureContext->setResponse($this->uploadFileWithContentAndChecksumToUsingTheAPI($user, $content, $checksum, $destination)); } /** @@ -130,13 +163,8 @@ public function userHasUploadedFileWithContentAndChecksumToUsingTheAPI( string $destination ):void { $user = $this->featureContext->getActualUsername($user); - $this->userUploadsFileWithContentAndChecksumToUsingTheAPI( - $user, - $content, - $checksum, - $destination - ); - $this->featureContext->theHTTPStatusCodeShouldBeSuccess(); + $response = $this->uploadFileWithContentAndChecksumToUsingTheAPI($user, $content, $checksum, $destination); + $this->featureContext->theHTTPStatusCodeShouldBe(201, '', $response); } /** @@ -375,8 +403,6 @@ public function theOcChecksumHeaderShouldNotBeThere():void { } /** - * @When user :user uploads chunk file :num of :total with :data to :destination with checksum :expectedChecksum using the WebDAV API - * * @param string $user * @param int $num * @param int $total @@ -384,26 +410,49 @@ public function theOcChecksumHeaderShouldNotBeThere():void { * @param string $destination * @param string $expectedChecksum * - * @return void + * @return ResponseInterface */ - public function userUploadsChunkFileOfWithToWithChecksum( + public function uploadChunkFileOfWithToWithChecksum( string $user, int $num, int $total, string $data, string $destination, string $expectedChecksum - ):void { + ):ResponseInterface { $user = $this->featureContext->getActualUsername($user); $num -= 1; $file = "$destination-chunking-42-$total-$num"; - $response = $this->featureContext->makeDavRequest( + return $this->featureContext->makeDavRequest( $user, 'PUT', $file, ['OC-Checksum' => $expectedChecksum, 'OC-Chunked' => '1'], $data ); + } + + /** + * @When user :user uploads chunk file :num of :total with :data to :destination with checksum :expectedChecksum using the WebDAV API + * + * @param string $user + * @param int $num + * @param int $total + * @param string $data + * @param string $destination + * @param string $expectedChecksum + * + * @return void + */ + public function userUploadsChunkFileOfWithToWithChecksum( + string $user, + int $num, + int $total, + string $data, + string $destination, + string $expectedChecksum + ):void { + $response = $this->uploadChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination, $expectedChecksum); $this->featureContext->setResponse($response); } @@ -427,15 +476,12 @@ public function userHasUploadedChunkFileOfWithToWithChecksum( string $destination, string $expectedChecksum ):void { - $this->userUploadsChunkFileOfWithToWithChecksum( - $user, - $num, - $total, - $data, - $destination, - $expectedChecksum + $response = $this->uploadChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination, $expectedChecksum); + $this->featureContext->theHTTPStatusCodeShouldBe( + [201, 206], + '', + $response ); - $this->featureContext->theHTTPStatusCodeShouldBeOr(201, 206); } /** diff --git a/tests/acceptance/features/bootstrap/FavoritesContext.php b/tests/acceptance/features/bootstrap/FavoritesContext.php index 80eeb7b061a..7eaad105675 100644 --- a/tests/acceptance/features/bootstrap/FavoritesContext.php +++ b/tests/acceptance/features/bootstrap/FavoritesContext.php @@ -39,15 +39,14 @@ class FavoritesContext implements Context { * @param string$user * @param string $path * - * @return void + * @return ResponseInterface */ - public function userFavoritesElement(string $user, string $path):void { - $response = $this->changeFavStateOfAnElement( + public function userFavoritesElement(string $user, string $path):ResponseInterface { + return $this->changeFavStateOfAnElement( $user, $path, 1 ); - $this->featureContext->setResponse($response); } /** @@ -59,7 +58,7 @@ public function userFavoritesElement(string $user, string $path):void { * @return void */ public function userFavoritesElementUsingWebDavApi(string $user, string $path):void { - $this->userFavoritesElement($user, $path); + $this->featureContext->setResponse($this->userFavoritesElement($user, $path)); } /** @@ -71,8 +70,7 @@ public function userFavoritesElementUsingWebDavApi(string $user, string $path):v * @return void */ public function userHasFavoritedElementUsingWebDavApi(string $user, string $path):void { - $this->userFavoritesElement($user, $path); - $this->featureContext->theHTTPStatusCodeShouldBeSuccess(); + $this->featureContext->theHTTPStatusCodeShouldBe(207, '', $this->userFavoritesElement($user, $path)); } /** @@ -83,10 +81,11 @@ public function userHasFavoritedElementUsingWebDavApi(string $user, string $path * @return void */ public function theUserFavoritesElement(string $path):void { - $this->userFavoritesElement( + $response = $this->userFavoritesElement( $this->featureContext->getCurrentUser(), $path ); + $this->featureContext->setResponse($response); } /** @@ -97,13 +96,14 @@ public function theUserFavoritesElement(string $path):void { * @return void */ public function theUserHasFavoritedElement(string $path):void { - $this->userFavoritesElement( + $response = $this->userFavoritesElement( $this->featureContext->getCurrentUser(), $path ); $this->featureContext->theHTTPStatusCodeShouldBe( 207, - "Expected response status code to be 207 (Multi-status), but not found! " + "Expected response status code to be 207 (Multi-status), but not found! ", + $response ); } @@ -111,15 +111,14 @@ public function theUserHasFavoritedElement(string $path):void { * @param string $user * @param string $path * - * @return void + * @return ResponseInterface */ - public function userUnfavoritesElement(string $user, string $path):void { - $response = $this->changeFavStateOfAnElement( + public function userUnfavoritesElement(string $user, string $path):ResponseInterface { + return $this->changeFavStateOfAnElement( $user, $path, 0 ); - $this->featureContext->setResponse($response); } /** @@ -131,7 +130,7 @@ public function userUnfavoritesElement(string $user, string $path):void { * @return void */ public function userUnfavoritesElementUsingWebDavApi(string $user, string $path):void { - $this->userUnfavoritesElement($user, $path); + $this->featureContext->setResponse($this->userUnfavoritesElement($user, $path)); } /** @@ -143,8 +142,7 @@ public function userUnfavoritesElementUsingWebDavApi(string $user, string $path) * @return void */ public function userHasUnfavoritedElementUsingWebDavApi(string $user, string $path):void { - $this->userUnfavoritesElement($user, $path); - $this->featureContext->theHTTPStatusCodeShouldBeSuccess(); + $this->featureContext->theHTTPStatusCodeShouldBe(207, '', $this->userUnfavoritesElement($user, $path)); } /** @@ -212,10 +210,10 @@ public function userListsFavorites(string $user, ?int $limit = null):void { /** * @param string $path * - * @return void + * @return ResponseInterface */ - public function theUserUnfavoritesElement(string $path):void { - $this->userUnfavoritesElement( + public function theUserUnfavoritesElement(string $path):ResponseInterface { + return $this->userUnfavoritesElement( $this->featureContext->getCurrentUser(), $path ); @@ -229,7 +227,7 @@ public function theUserUnfavoritesElement(string $path):void { * @return void */ public function theUserUnfavoritesElementUsingWebDavApi(string $path):void { - $this->theUserUnfavoritesElement($path); + $this->featureContext->setResponse($this->theUserUnfavoritesElement($path)); } /** @@ -240,8 +238,7 @@ public function theUserUnfavoritesElementUsingWebDavApi(string $path):void { * @return void */ public function theUserHasUnfavoritedElementUsingWebDavApi(string $path):void { - $this->theUserUnfavoritesElement($path); - $this->featureContext->theHTTPStatusCodeShouldBeSuccess(); + $this->featureContext->theHTTPStatusCodeShouldBe(207, '', $this->theUserUnfavoritesElement($path)); } /** diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php index cc34c07e683..7f896c7bc69 100644 --- a/tests/acceptance/features/bootstrap/SpacesContext.php +++ b/tests/acceptance/features/bootstrap/SpacesContext.php @@ -3038,7 +3038,7 @@ public function asUserFileOrFolderInsideSpaceShouldOrNotBeFavorited(string $user */ public function userFavoritesElementInSpaceUsingTheWebdavApi(string $user, string $path, string $spaceName): void { $this->setSpaceIDByName($user, $spaceName); - $this->favoritesContext->userFavoritesElement($user, $path); + $this->featureContext->setResponse($this->favoritesContext->userFavoritesElement($user, $path)); } /**