refacor given when and then steps in checksum and favorite context (#7268)

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
This commit is contained in:
Karun Atreya
2023-09-13 16:25:00 +05:45
committed by GitHub
parent 71daf3f6ba
commit fa36fd4ec2
3 changed files with 113 additions and 70 deletions

View File

@@ -23,6 +23,7 @@ use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PHPUnit\Framework\Assert;
use TestHelpers\WebDavHelper;
use Psr\Http\Message\ResponseInterface;
require_once 'bootstrap.php';
@@ -32,6 +33,32 @@ require_once 'bootstrap.php';
class ChecksumContext implements Context {
private FeatureContext $featureContext;
/**
* @param string $user
* @param string $source
* @param string $destination
* @param string $checksum
*
* @return ResponseInterface
*/
public function uploadFileToWithChecksumUsingTheAPI(
string $user,
string $source,
string $destination,
string $checksum
):ResponseInterface {
$file = \file_get_contents(
$this->featureContext->acceptanceTestsDirLocation() . $source
);
return $this->featureContext->makeDavRequest(
$user,
'PUT',
$destination,
['OC-Checksum' => $checksum],
$file
);
}
/**
* @When user :user uploads file :source to :destination with checksum :checksum using the WebDAV API
*
@@ -48,17 +75,7 @@ class ChecksumContext implements Context {
string $destination,
string $checksum
):void {
$file = \file_get_contents(
$this->featureContext->acceptanceTestsDirLocation() . $source
);
$response = $this->featureContext->makeDavRequest(
$user,
'PUT',
$destination,
['OC-Checksum' => $checksum],
$file
);
$this->featureContext->setResponse($response);
$this->featureContext->setResponse($this->uploadFileToWithChecksumUsingTheAPI($user, $source, $destination, $checksum));
}
/**
@@ -78,13 +95,36 @@ class ChecksumContext implements Context {
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);
}
/**
* @param string $user
* @param string $content
* @param string $checksum
* @param string $destination
*
* @return ResponseInterface
*/
public function uploadFileWithContentAndChecksumToUsingTheAPI(
string $user,
string $content,
string $checksum,
string $destination
):ResponseInterface {
return $this->featureContext->makeDavRequest(
$user,
'PUT',
$destination,
['OC-Checksum' => $checksum],
$content
);
}
/**
@@ -103,14 +143,7 @@ class ChecksumContext implements Context {
string $checksum,
string $destination
):void {
$response = $this->featureContext->makeDavRequest(
$user,
'PUT',
$destination,
['OC-Checksum' => $checksum],
$content
);
$this->featureContext->setResponse($response);
$this->featureContext->setResponse($this->uploadFileWithContentAndChecksumToUsingTheAPI($user, $content, $checksum, $destination));
}
/**
@@ -130,13 +163,8 @@ class ChecksumContext implements Context {
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);
}
/**
@@ -374,6 +402,36 @@ class ChecksumContext implements Context {
);
}
/**
* @param string $user
* @param int $num
* @param int $total
* @param string $data
* @param string $destination
* @param string $expectedChecksum
*
* @return ResponseInterface
*/
public function uploadChunkFileOfWithToWithChecksum(
string $user,
int $num,
int $total,
string $data,
string $destination,
string $expectedChecksum
):ResponseInterface {
$user = $this->featureContext->getActualUsername($user);
$num -= 1;
$file = "$destination-chunking-42-$total-$num";
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
*
@@ -394,16 +452,7 @@ class ChecksumContext implements Context {
string $destination,
string $expectedChecksum
):void {
$user = $this->featureContext->getActualUsername($user);
$num -= 1;
$file = "$destination-chunking-42-$total-$num";
$response = $this->featureContext->makeDavRequest(
$user,
'PUT',
$file,
['OC-Checksum' => $expectedChecksum, 'OC-Chunked' => '1'],
$data
);
$response = $this->uploadChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination, $expectedChecksum);
$this->featureContext->setResponse($response);
}
@@ -427,15 +476,12 @@ class ChecksumContext implements Context {
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);
}
/**

View File

@@ -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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
* @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 @@ class FavoritesContext implements Context {
/**
* @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 @@ class FavoritesContext implements Context {
* @return void
*/
public function theUserUnfavoritesElementUsingWebDavApi(string $path):void {
$this->theUserUnfavoritesElement($path);
$this->featureContext->setResponse($this->theUserUnfavoritesElement($path));
}
/**
@@ -240,8 +238,7 @@ class FavoritesContext implements Context {
* @return void
*/
public function theUserHasUnfavoritedElementUsingWebDavApi(string $path):void {
$this->theUserUnfavoritesElement($path);
$this->featureContext->theHTTPStatusCodeShouldBeSuccess();
$this->featureContext->theHTTPStatusCodeShouldBe(207, '', $this->theUserUnfavoritesElement($path));
}
/**

View File

@@ -3038,7 +3038,7 @@ class SpacesContext implements Context {
*/
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));
}
/**