Skip to content

Commit

Permalink
refactor given when then steps in settings and tus contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
KarunAtreya committed Sep 21, 2023
1 parent 1047ea3 commit b17f049
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 55 deletions.
16 changes: 7 additions & 9 deletions tests/acceptance/features/bootstrap/SettingsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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());
}

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

/**
Expand Down
132 changes: 87 additions & 45 deletions tests/acceptance/features/bootstrap/TUSContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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];
Expand All @@ -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 = '';
}

Expand Down Expand Up @@ -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];
}
}

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

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

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

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

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

0 comments on commit b17f049

Please sign in to comment.