Skip to content

Commit

Permalink
refacor given when and then steps in checksum and favorite context
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KarunAtreya committed Sep 12, 2023
1 parent 47eb710 commit 1c680fa
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 61 deletions.
120 changes: 83 additions & 37 deletions tests/acceptance/features/bootstrap/ChecksumContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -375,35 +403,56 @@ 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
* @param string $data
* @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);
}

Expand All @@ -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);
}

/**
Expand Down
43 changes: 20 additions & 23 deletions tests/acceptance/features/bootstrap/FavoritesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -97,29 +96,29 @@ 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
);
}

/**
* @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);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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
);
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/bootstrap/SpacesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 1c680fa

Please sign in to comment.