From b17f0493616dfaa6e2ea7591e832727404ff1127 Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Mon, 18 Sep 2023 16:46:23 +0545 Subject: [PATCH] refactor given when then steps in settings and tus contexts --- .../features/bootstrap/SettingsContext.php | 16 +-- .../features/bootstrap/SpacesTUSContext.php | 3 +- .../features/bootstrap/TUSContext.php | 132 ++++++++++++------ 3 files changed, 96 insertions(+), 55 deletions(-) diff --git a/tests/acceptance/features/bootstrap/SettingsContext.php b/tests/acceptance/features/bootstrap/SettingsContext.php index 01c847166bd..336cefba160 100644 --- a/tests/acceptance/features/bootstrap/SettingsContext.php +++ b/tests/acceptance/features/bootstrap/SettingsContext.php @@ -349,12 +349,12 @@ public function getSettingLanguageValue(string $user): string { * @param string $user * @param string $language * - * @return void + * @return ResponseInterface * * @throws GuzzleException * @throws Exception */ - public function sendRequestToSwitchSystemLanguage(string $user, string $language): void { + public function sendRequestToSwitchSystemLanguage(string $user, string $language): ResponseInterface { $profileBundlesList = $this->getBundlesList($user, "Profile"); Assert::assertNotEmpty($profileBundlesList, "bundles list is empty"); @@ -390,10 +390,7 @@ public function sendRequestToSwitchSystemLanguage(string $user, string $language ], JSON_THROW_ON_ERROR ); - - $this->featureContext->setResponse( - $this->spacesContext->sendPostRequestToUrl($fullUrl, $user, $this->featureContext->getPasswordForUser($user), $body, $this->featureContext->getStepLineRef()) - ); + return $this->spacesContext->sendPostRequestToUrl($fullUrl, $user, $this->featureContext->getPasswordForUser($user), $body, $this->featureContext->getStepLineRef()); } /** @@ -405,12 +402,13 @@ public function sendRequestToSwitchSystemLanguage(string $user, string $language * @return void * * @throws Exception + * @throws GuzzleException */ - public function theUserHasSwitchedSysemLanguage(string $user, string $language): void { - $this->sendRequestToSwitchSystemLanguage($user, $language); + public function theUserHasSwitchedSystemLanguage(string $user, string $language): void { $this->featureContext->theHTTPStatusCodeShouldBe( 201, - "Expected response status code should be 201" + "Expected response status code should be 201", + $this->sendRequestToSwitchSystemLanguage($user, $language) ); } } diff --git a/tests/acceptance/features/bootstrap/SpacesTUSContext.php b/tests/acceptance/features/bootstrap/SpacesTUSContext.php index 9b53a034546..d8234f27cbf 100644 --- a/tests/acceptance/features/bootstrap/SpacesTUSContext.php +++ b/tests/acceptance/features/bootstrap/SpacesTUSContext.php @@ -126,7 +126,8 @@ public function userCreatesANewTusResourceForTheSpaceUsingTheWebdavApiWithTheseH TableNode $headers ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, $content); + $response = $this->tusContext->createNewTUSResourceWithHeaders($user, $headers, $content); + $this->featureContext->setResponse($response); } /** diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index eca3ea69ca1..707fb41fee2 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -30,6 +30,7 @@ use TusPhp\Exception\TusException; use TusPhp\Tus\Client; use PHPUnit\Framework\Assert; +use Psr\Http\Message\ResponseInterface; require_once 'bootstrap.php'; @@ -42,36 +43,50 @@ class TUSContext implements Context { private ?string $resourceLocation = null; /** - * @When user :user creates a new TUS resource on the WebDAV API with these headers: - * * @param string $user * @param TableNode $headers * @param string $content * - * @return void + * @return ResponseInterface * * @throws Exception * @throws GuzzleException */ - public function createNewTUSResourceWithHeaders(string $user, TableNode $headers, string $content = ''): void { + public function createNewTUSResourceWithHeaders(string $user, TableNode $headers, string $content = ''): ResponseInterface { $this->featureContext->verifyTableNodeColumnsCount($headers, 2); $user = $this->featureContext->getActualUsername($user); $password = $this->featureContext->getUserPassword($user); $this->resourceLocation = null; - $this->featureContext->setResponse( - $this->featureContext->makeDavRequest( - $user, - "POST", - null, - $headers->getRowsHash(), - $content, - "files", - null, - false, - $password - ) + $response = $this->featureContext->makeDavRequest( + $user, + "POST", + null, + $headers->getRowsHash(), + $content, + "files", + null, + false, + $password ); + return $response; + } + + /** + * @When user :user creates a new TUS resource on the WebDAV API with these headers: + * + * @param string $user + * @param TableNode $headers + * @param string $content + * + * @return void + * + * @throws Exception + * @throws GuzzleException + */ + public function userCreateNewTUSResourceWithHeaders(string $user, TableNode $headers, string $content = ''): void { + $response = $this->createNewTUSResourceWithHeaders($user, $headers, $content); + $this->featureContext->setResponse($response); $locationHeader = $this->featureContext->getResponse()->getHeader('Location'); if (\sizeof($locationHeader) > 0) { $this->resourceLocation = $locationHeader[0]; @@ -92,42 +107,61 @@ public function createNewTUSResourceWithHeaders(string $user, TableNode $headers public function createNewTUSResource(string $user, TableNode $headers): void { $rows = $headers->getRows(); $rows[] = ['Tus-Resumable', '1.0.0']; - $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); - $this->featureContext->theHTTPStatusCodeShouldBe(201); + $response = $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); + $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $response); + $locationHeader = $response->getHeader('Location'); + if (\sizeof($locationHeader) > 0) { + $this->resourceLocation = $locationHeader[0]; + } } /** - * @When /^user "([^"]*)" sends a chunk to the last created TUS Location with offset "([^"]*)" and data "([^"]*)" using the WebDAV API$/ - * * @param string $user * @param string $offset * @param string $data * @param string $checksum * - * @return void + * @return ResponseInterface * * @throws GuzzleException * @throws JsonException */ - public function sendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data, string $checksum = ''): void { + public function sendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data, string $checksum = ''): ResponseInterface { $user = $this->featureContext->getActualUsername($user); $password = $this->featureContext->getUserPassword($user); - $this->featureContext->setResponse( - HttpRequestHelper::sendRequest( - $this->resourceLocation, - $this->featureContext->getStepLineRef(), - 'PATCH', - $user, - $password, - [ - 'Content-Type' => 'application/offset+octet-stream', - 'Tus-Resumable' => '1.0.0', - 'Upload-Checksum' => $checksum, - 'Upload-Offset' => $offset - ], - $data - ) + $response = HttpRequestHelper::sendRequest( + $this->resourceLocation, + $this->featureContext->getStepLineRef(), + 'PATCH', + $user, + $password, + [ + 'Content-Type' => 'application/offset+octet-stream', + 'Tus-Resumable' => '1.0.0', + 'Upload-Checksum' => $checksum, + 'Upload-Offset' => $offset + ], + $data ); + return $response; + } + + /** + * @When /^user "([^"]*)" sends a chunk to the last created TUS Location with offset "([^"]*)" and data "([^"]*)" using the WebDAV API$/ + * + * @param string $user + * @param string $offset + * @param string $data + * @param string $checksum + * + * @return void + * + * @throws GuzzleException + * @throws JsonException + */ + public function userSendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data, string $checksum = ''): void { + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->setResponse($response); WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } @@ -359,7 +393,12 @@ public function userCreatesWithUpload( string $content, TableNode $headers ): void { - $this->createNewTUSResourceWithHeaders($user, $headers, $content); + $response = $this->createNewTUSResourceWithHeaders($user, $headers, $content); + $this->featureContext->setResponse($response); + $locationHeader = $this->featureContext->getResponse()->getHeader('Location'); + if (\sizeof($locationHeader) > 0) { + $this->resourceLocation = $locationHeader[0]; + } } /** @@ -407,7 +446,8 @@ public function userUploadsFileWithChecksum( string $offset, string $content ): void { - $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); + $this->featureContext->setResponse($response); } /** @@ -427,8 +467,8 @@ public function userHasUploadedFileWithChecksum( string $offset, string $content ): void { - $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); - $this->featureContext->theHTTPStatusCodeShouldBe(204); + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); + $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); } /** @@ -443,7 +483,8 @@ public function userHasUploadedFileWithChecksum( * @throws Exception */ public function userUploadsChunkFileWithChecksum(string $user, string $offset, string $data, string $checksum): void { - $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->setResponse($response); } /** @@ -458,8 +499,8 @@ public function userUploadsChunkFileWithChecksum(string $user, string $offset, s * @throws Exception */ public function userHasUploadedChunkFileWithChecksum(string $user, string $offset, string $data, string $checksum): void { - $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); - $this->featureContext->theHTTPStatusCodeShouldBe(204); + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); } /** @@ -479,6 +520,7 @@ public function userHasUploadedChunkFileWithChecksum(string $user, string $offse */ public function userOverwritesFileWithChecksum(string $user, string $offset, string $data, string $checksum, TableNode $headers): void { $this->createNewTUSResource($user, $headers); - $this->userHasUploadedChunkFileWithChecksum($user, $offset, $data, $checksum); + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->setResponse($response); } }