From 0f5c26ee5c15b083369dc36678cf44e743e7dd3d Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Mon, 18 Sep 2023 16:46:23 +0545 Subject: [PATCH 1/5] 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 7890ae02b03..1c1149182e7 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); } } From ff84c449d0148437278875200ffb72d047a77703 Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Tue, 19 Sep 2023 09:30:40 +0545 Subject: [PATCH 2/5] set resource location from response --- .../acceptance/features/bootstrap/TUSContext.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index 707fb41fee2..0f7639acf39 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -69,6 +69,10 @@ public function createNewTUSResourceWithHeaders(string $user, TableNode $headers false, $password ); + $locationHeader = $response->getHeader('Location'); + if (\sizeof($locationHeader) > 0) { + $this->resourceLocation = $locationHeader[0]; + } return $response; } @@ -87,10 +91,6 @@ public function createNewTUSResourceWithHeaders(string $user, TableNode $headers 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]; - } } /** @@ -109,10 +109,6 @@ public function createNewTUSResource(string $user, TableNode $headers): void { $rows[] = ['Tus-Resumable', '1.0.0']; $response = $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $response); - $locationHeader = $response->getHeader('Location'); - if (\sizeof($locationHeader) > 0) { - $this->resourceLocation = $locationHeader[0]; - } } /** @@ -395,10 +391,6 @@ public function userCreatesWithUpload( ): 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]; - } } /** From 164a7b11bdeceb38c911e432841d7f233386f47c Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Thu, 21 Sep 2023 16:04:54 +0545 Subject: [PATCH 3/5] addressed the reviews --- .../features/bootstrap/SettingsContext.php | 11 +++++++++-- tests/acceptance/features/bootstrap/TUSContext.php | 14 ++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/acceptance/features/bootstrap/SettingsContext.php b/tests/acceptance/features/bootstrap/SettingsContext.php index 336cefba160..f9b3d7ab6c5 100644 --- a/tests/acceptance/features/bootstrap/SettingsContext.php +++ b/tests/acceptance/features/bootstrap/SettingsContext.php @@ -390,7 +390,13 @@ public function sendRequestToSwitchSystemLanguage(string $user, string $language ], JSON_THROW_ON_ERROR ); - return $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,10 +411,11 @@ public function sendRequestToSwitchSystemLanguage(string $user, string $language * @throws GuzzleException */ public function theUserHasSwitchedSystemLanguage(string $user, string $language): void { + $response = $this->sendRequestToSwitchSystemLanguage($user, $language); $this->featureContext->theHTTPStatusCodeShouldBe( 201, "Expected response status code should be 201", - $this->sendRequestToSwitchSystemLanguage($user, $language) + $response ); } } diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index 0f7639acf39..d890de01292 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -104,7 +104,7 @@ public function userCreateNewTUSResourceWithHeaders(string $user, TableNode $hea * @throws Exception * @throws GuzzleException */ - public function createNewTUSResource(string $user, TableNode $headers): void { + public function userHasCreatedNewTUSResourceWithHeaders(string $user, TableNode $headers): void { $rows = $headers->getRows(); $rows[] = ['Tus-Resumable', '1.0.0']; $response = $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); @@ -125,7 +125,7 @@ public function createNewTUSResource(string $user, TableNode $headers): void { public function sendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data, string $checksum = ''): ResponseInterface { $user = $this->featureContext->getActualUsername($user); $password = $this->featureContext->getUserPassword($user); - $response = HttpRequestHelper::sendRequest( + return HttpRequestHelper::sendRequest( $this->resourceLocation, $this->featureContext->getStepLineRef(), 'PATCH', @@ -139,24 +139,22 @@ public function sendsAChunkToTUSLocationWithOffsetAndData(string $user, string $ ], $data ); - return $response; } /** - * @When /^user "([^"]*)" sends a chunk to the last created TUS Location with offset "([^"]*)" and data "([^"]*)" using the WebDAV API$/ + * @When user :user sends a chunk to the last created TUS Location with offset :offset and data :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); + public function userSendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data): void { + $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data); $this->featureContext->setResponse($response); WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } @@ -511,7 +509,7 @@ public function userHasUploadedChunkFileWithChecksum(string $user, string $offse * @throws Exception */ public function userOverwritesFileWithChecksum(string $user, string $offset, string $data, string $checksum, TableNode $headers): void { - $this->createNewTUSResource($user, $headers); + $this->userHasCreatedNewTUSResourceWithHeaders($user, $headers); $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->setResponse($response); } From 676e735c7c6ca111b77d19db6558bcdd1bd50631 Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Fri, 22 Sep 2023 15:57:30 +0545 Subject: [PATCH 4/5] reset spaceidfromocis --- tests/acceptance/features/bootstrap/TUSContext.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index d890de01292..9aede101399 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -438,6 +438,7 @@ public function userUploadsFileWithChecksum( ): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); $this->featureContext->setResponse($response); + WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -459,6 +460,7 @@ public function userHasUploadedFileWithChecksum( ): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); + WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -475,6 +477,7 @@ public function userHasUploadedFileWithChecksum( public function userUploadsChunkFileWithChecksum(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 = ''; } /** @@ -491,6 +494,7 @@ public function userUploadsChunkFileWithChecksum(string $user, string $offset, s public function userHasUploadedChunkFileWithChecksum(string $user, string $offset, string $data, string $checksum): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); + WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -512,5 +516,6 @@ public function userOverwritesFileWithChecksum(string $user, string $offset, str $this->userHasCreatedNewTUSResourceWithHeaders($user, $headers); $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->setResponse($response); + WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } } From f7f5f4223d9b341cafae61631fdd1dd5cd16c26f Mon Sep 17 00:00:00 2001 From: KarunAtreya Date: Mon, 25 Sep 2023 10:10:14 +0545 Subject: [PATCH 5/5] reset after scenario using after hook --- tests/acceptance/features/bootstrap/FeatureContext.php | 1 + tests/acceptance/features/bootstrap/TUSContext.php | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/acceptance/features/bootstrap/FeatureContext.php b/tests/acceptance/features/bootstrap/FeatureContext.php index e7903b1a952..b437bda4ceb 100644 --- a/tests/acceptance/features/bootstrap/FeatureContext.php +++ b/tests/acceptance/features/bootstrap/FeatureContext.php @@ -3391,6 +3391,7 @@ public function clearSpaceId(): void { if (\count(WebDavHelper::$spacesIdRef) > 0) { WebDavHelper::$spacesIdRef = []; } + WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index 9aede101399..57b912d3bb1 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -156,7 +156,6 @@ public function sendsAChunkToTUSLocationWithOffsetAndData(string $user, string $ public function userSendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data); $this->featureContext->setResponse($response); - WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -438,7 +437,6 @@ public function userUploadsFileWithChecksum( ): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); $this->featureContext->setResponse($response); - WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -460,7 +458,6 @@ public function userHasUploadedFileWithChecksum( ): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); - WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -477,7 +474,6 @@ public function userHasUploadedFileWithChecksum( public function userUploadsChunkFileWithChecksum(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 = ''; } /** @@ -494,7 +490,6 @@ public function userUploadsChunkFileWithChecksum(string $user, string $offset, s public function userHasUploadedChunkFileWithChecksum(string $user, string $offset, string $data, string $checksum): void { $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); - WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } /** @@ -516,6 +511,5 @@ public function userOverwritesFileWithChecksum(string $user, string $offset, str $this->userHasCreatedNewTUSResourceWithHeaders($user, $headers); $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->setResponse($response); - WebDavHelper::$SPACE_ID_FROM_OCIS = ''; } }