From 8f290ce2eabf1aa26a38753865848fb19758de07 Mon Sep 17 00:00:00 2001 From: nirajacharya2 <122071597+nirajacharya2@users.noreply.github.com> Date: Fri, 5 Apr 2024 11:31:46 +0545 Subject: [PATCH] removing usage of a stepdefination inside another stepdefination in TUSContext (#8753) --- .../features/bootstrap/SpacesTUSContext.php | 42 +++++++++++--- .../features/bootstrap/TUSContext.php | 56 ++++++++++++++++--- 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/tests/acceptance/features/bootstrap/SpacesTUSContext.php b/tests/acceptance/features/bootstrap/SpacesTUSContext.php index 6b82c66d9..15f97d913 100644 --- a/tests/acceptance/features/bootstrap/SpacesTUSContext.php +++ b/tests/acceptance/features/bootstrap/SpacesTUSContext.php @@ -13,6 +13,7 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use GuzzleHttp\Exception\GuzzleException; use Behat\Gherkin\Node\TableNode; +use PHPUnit\Framework\Assert; require_once 'bootstrap.php'; @@ -58,7 +59,8 @@ class SpacesTUSContext implements Context { */ public function userHasUploadedFileViaTusInSpace(string $user, string $source, string $destination, string $spaceName): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userUploadsUsingTusAFileTo($user, $source, $destination); + $this->tusContext->uploadFileUsingTus($user, $source, $destination); + $this->featureContext->setLastUploadDeleteTime(\time()); } /** @@ -80,7 +82,8 @@ class SpacesTUSContext implements Context { string $spaceName ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userUploadsUsingTusAFileTo($user, $source, $destination); + $this->tusContext->uploadFileUsingTus($user, $source, $destination); + $this->featureContext->setLastUploadDeleteTime(\time()); } /** @@ -149,7 +152,18 @@ class SpacesTUSContext implements Context { string $spaceName ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userUploadsAFileWithContentToUsingTus($user, $content, $resource); + $tmpFile = $this->tusContext->writeDataToTempFile($content); + try { + $this->tusContext->uploadFileUsingTus( + $user, + \basename($tmpFile), + $resource + ); + $this->featureContext->setLastUploadDeleteTime(\time()); + } catch (Exception $e) { + Assert::assertStringContainsString('Unable to create resource', (string)$e); + } + \unlink($tmpFile); } /** @@ -213,7 +227,16 @@ class SpacesTUSContext implements Context { $mtime; } $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userUploadsFileWithContentToWithMtimeUsingTUS($user, $source, $destination, $mtime); + $mtime = new DateTime($mtime); + $mtime = $mtime->format('U'); + $user = $this->featureContext->getActualUsername($user); + $this->tusContext->uploadFileUsingTus( + $user, + $source, + $destination, + ['mtime' => $mtime] + ); + $this->featureContext->setLastUploadDeleteTime(\time()); } /** @@ -236,7 +259,8 @@ class SpacesTUSContext implements Context { string $spaceName ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userHasUploadedFileWithChecksum($user, $checksum, $offset, $content); + $response = $this->tusContext->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $content, $checksum); + $this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response); } /** @@ -283,7 +307,8 @@ class SpacesTUSContext implements Context { string $spaceName ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userUploadsChunkFileWithChecksum($user, $offset, $data, $checksum); + $response = $this->tusContext->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->setResponse($response); } /** @@ -331,7 +356,10 @@ class SpacesTUSContext implements Context { TableNode $headers ): void { $this->spacesContext->setSpaceIDByName($user, $spaceName); - $this->tusContext->userOverwritesFileWithChecksum($user, $offset, $data, $checksum, $headers); + $createResponse = $this->tusContext->createNewTUSResource($user, $headers); + $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $createResponse); + $response = $this->tusContext->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); + $this->featureContext->setResponse($response); } /** diff --git a/tests/acceptance/features/bootstrap/TUSContext.php b/tests/acceptance/features/bootstrap/TUSContext.php index 99404723f..6d980e1fc 100644 --- a/tests/acceptance/features/bootstrap/TUSContext.php +++ b/tests/acceptance/features/bootstrap/TUSContext.php @@ -112,10 +112,20 @@ class TUSContext implements Context { * @throws GuzzleException */ public function userHasCreatedNewTUSResourceWithHeaders(string $user, TableNode $headers): void { + $response = $this->createNewTUSResource($user, $headers); + $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $response); + } + + /** + * @param string $user + * @param TableNode $headers + * + * @return ResponseInterface + */ + public function createNewTUSResource(string $user, TableNode $headers):ResponseInterface { $rows = $headers->getRows(); $rows[] = ['Tus-Resumable', '1.0.0']; - $response = $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); - $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $response); + return $this->createNewTUSResourceWithHeaders($user, new TableNode($rows)); } /** @@ -199,6 +209,30 @@ class TUSContext implements Context { int $bytes = null, string $checksum = '' ): void { + $this->uploadFileUsingTus($user, $source, $destination, $uploadMetadata, $noOfChunks, $bytes, $checksum); + $this->featureContext->setLastUploadDeleteTime(\time()); + } + + /** + * @param string $user + * @param string $source + * @param string $destination + * @param array $uploadMetadata + * @param integer $noOfChunks + * @param integer $bytes + * @param string $checksum + * + * @return void + */ + public function uploadFileUsingTus( + ?string $user, + string $source, + string $destination, + array $uploadMetadata = [], + int $noOfChunks = 1, + int $bytes = null, + string $checksum = '' + ) { $user = $this->featureContext->getActualUsername($user); $password = $this->featureContext->getUserPassword($user); $headers = [ @@ -252,7 +286,6 @@ class TUSContext implements Context { $client->upload($bytesPerChunk); } } - $this->featureContext->setLastUploadDeleteTime(\time()); } /** @@ -273,11 +306,12 @@ class TUSContext implements Context { ): void { $tmpfname = $this->writeDataToTempFile($content); try { - $this->userUploadsUsingTusAFileTo( + $this->uploadFileUsingTus( $user, \basename($tmpfname), $destination ); + $this->featureContext->setLastUploadDeleteTime(\time()); } catch (Exception $e) { Assert::assertStringContainsString('TusPhp\Exception\FileException: Unable to create resource', (string)$e); } @@ -308,13 +342,14 @@ class TUSContext implements Context { string $destination ): void { $tmpfname = $this->writeDataToTempFile($content); - $this->userUploadsUsingTusAFileTo( + $this->uploadFileUsingTus( $user, \basename($tmpfname), $destination, [], $noOfChunks ); + $this->featureContext->setLastUploadDeleteTime(\time()); \unlink($tmpfname); } @@ -339,12 +374,13 @@ class TUSContext implements Context { $mtime = new DateTime($mtime); $mtime = $mtime->format('U'); $user = $this->featureContext->getActualUsername($user); - $this->userUploadsUsingTusAFileTo( + $this->uploadFileUsingTus( $user, $source, $destination, ['mtime' => $mtime] ); + $this->featureContext->setLastUploadDeleteTime(\time()); } /** @@ -353,7 +389,7 @@ class TUSContext implements Context { * @return string the file name * @throws Exception */ - private function writeDataToTempFile(string $content): string { + public function writeDataToTempFile(string $content): string { $tmpfname = \tempnam( $this->featureContext->acceptanceTestsDirLocation(), "tus-upload-test-" @@ -421,7 +457,7 @@ class TUSContext implements Context { string $content ): void { $tmpfname = $this->writeDataToTempFile($content); - $this->userUploadsUsingTusAFileTo( + $this->uploadFileUsingTus( $user, \basename($tmpfname), $source, @@ -429,6 +465,7 @@ class TUSContext implements Context { 1, -1 ); + $this->featureContext->setLastUploadDeleteTime(\time()); \unlink($tmpfname); } @@ -522,7 +559,8 @@ class TUSContext implements Context { * @throws Exception */ public function userOverwritesFileWithChecksum(string $user, string $offset, string $data, string $checksum, TableNode $headers): void { - $this->userHasCreatedNewTUSResourceWithHeaders($user, $headers); + $createResponse = $this->createNewTUSResource($user, $headers); + $this->featureContext->theHTTPStatusCodeShouldBe(201, "", $createResponse); $response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum); $this->featureContext->setResponse($response); }