Skip to content

Commit

Permalink
refactor given when then steps in settings and tus contexts (#7357)
Browse files Browse the repository at this point in the history
set resource location from response

addressed the reviews

reset  spaceidfromocis

reset after scenario using after hook
  • Loading branch information
KarunAtreya authored Sep 27, 2023
1 parent 5ca6a98 commit cc794ab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 58 deletions.
1 change: 1 addition & 0 deletions tests/acceptance/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3391,6 +3391,7 @@ public function clearSpaceId(): void {
if (\count(WebDavHelper::$spacesIdRef) > 0) {
WebDavHelper::$spacesIdRef = [];
}
WebDavHelper::$SPACE_ID_FROM_OCIS = '';
}

/**
Expand Down
21 changes: 13 additions & 8 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,9 +390,12 @@ 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 +408,14 @@ 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 {
$response = $this->sendRequestToSwitchSystemLanguage($user, $language);
$this->featureContext->theHTTPStatusCodeShouldBe(
201,
"Expected response status code should be 201"
"Expected response status code should be 201",
$response
);
}
}
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
129 changes: 80 additions & 49 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,40 +43,54 @@ 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
);
$locationHeader = $this->featureContext->getResponse()->getHeader('Location');
$locationHeader = $response->getHeader('Location');
if (\sizeof($locationHeader) > 0) {
$this->resourceLocation = $locationHeader[0];
}
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);
}

/**
Expand All @@ -89,46 +104,58 @@ public function createNewTUSResourceWithHeaders(string $user, TableNode $headers
* @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'];
$this->createNewTUSResourceWithHeaders($user, new TableNode($rows));
$this->featureContext->theHTTPStatusCodeShouldBe(201);
$response = $this->createNewTUSResourceWithHeaders($user, new TableNode($rows));
$this->featureContext->theHTTPStatusCodeShouldBe(201, "", $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
* @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
)
return 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
);
WebDavHelper::$SPACE_ID_FROM_OCIS = '';
}

/**
* @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
*
* @return void
*
* @throws GuzzleException
* @throws JsonException
*/
public function userSendsAChunkToTUSLocationWithOffsetAndData(string $user, string $offset, string $data): void {
$response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data);
$this->featureContext->setResponse($response);
}

/**
Expand Down Expand Up @@ -359,7 +386,8 @@ public function userCreatesWithUpload(
string $content,
TableNode $headers
): void {
$this->createNewTUSResourceWithHeaders($user, $headers, $content);
$response = $this->createNewTUSResourceWithHeaders($user, $headers, $content);
$this->featureContext->setResponse($response);
}

/**
Expand Down Expand Up @@ -407,7 +435,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 +456,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 +472,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 +488,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 @@ -478,7 +508,8 @@ 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->userHasUploadedChunkFileWithChecksum($user, $offset, $data, $checksum);
$this->userHasCreatedNewTUSResourceWithHeaders($user, $headers);
$response = $this->sendsAChunkToTUSLocationWithOffsetAndData($user, $offset, $data, $checksum);
$this->featureContext->setResponse($response);
}
}

0 comments on commit cc794ab

Please sign in to comment.