Move to behat helper library

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2021-05-25 18:18:07 +02:00
parent 7eb4cdaecb
commit 1b02e7fb25
13 changed files with 128 additions and 307 deletions

View File

@@ -3,12 +3,13 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use JuliusHaertl\NextcloudBehat\Context\ServerContext;
use JuliusHaertl\NextcloudBehat\Context\SharingContext;
use PHPUnit\Framework\Assert;
require_once __DIR__ . '/../../vendor/autoload.php';
class BoardContext implements Context {
use RequestTrait;
/** @var array Last board response */
private $board = null;
@@ -19,12 +20,16 @@ class BoardContext implements Context {
/** @var ServerContext */
private $serverContext;
/** @var SharingContext */
private $sharingContext;
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->serverContext = $environment->getContext('ServerContext');
$this->serverContext = $environment->getContext(ServerContext::class);
$this->sharingContext = $environment->getContext(SharingContext::class);
}
public function getLastUsedCard() {
@@ -35,21 +40,21 @@ class BoardContext implements Context {
* @Given /^creates a board named "([^"]*)" with color "([^"]*)"$/
*/
public function createsABoardNamedWithColor($title, $color) {
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/boards', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/boards', [
'title' => $title,
'color' => $color
]);
$this->getResponse()->getBody()->seek(0);
$this->board = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->board = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
* @When /^fetches the board named "([^"]*)"$/
*/
public function fetchesTheBoardNamed($boardName) {
$this->requestContext->sendJSONrequest('GET', '/index.php/apps/deck/boards/' . $this->board['id'], []);
$this->getResponse()->getBody()->seek(0);
$this->board = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->sendJSONrequest('GET', '/index.php/apps/deck/boards/' . $this->board['id'], []);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->board = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
@@ -63,7 +68,7 @@ class BoardContext implements Context {
];
$tableRows = isset($permissions) ? $permissions->getRowsHash() : [];
$result = array_merge($defaults, $tableRows);
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/boards/' . $this->board['id'] . '/acl', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/boards/' . $this->board['id'] . '/acl', [
'type' => 0,
'participant' => $user,
'permissionEdit' => $result['permissionEdit'] === '1',
@@ -83,7 +88,7 @@ class BoardContext implements Context {
];
$tableRows = isset($permissions) ? $permissions->getRowsHash() : [];
$result = array_merge($defaults, $tableRows);
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/boards/' . $this->board['id'] . '/acl', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/boards/' . $this->board['id'] . '/acl', [
'type' => 1,
'participant' => $group,
'permissionEdit' => $result['permissionEdit'] === '1',
@@ -97,38 +102,38 @@ class BoardContext implements Context {
* @When /^fetching the board list$/
*/
public function fetchingTheBoardList() {
$this->requestContext->sendJSONrequest('GET', '/index.php/apps/deck/boards');
$this->serverContext->sendJSONrequest('GET', '/index.php/apps/deck/boards');
}
/**
* @When /^fetching the board with id "([^"]*)"$/
*/
public function fetchingTheBoardWithId($id) {
$this->requestContext->sendJSONrequest('GET', '/index.php/apps/deck/boards/' . $id);
$this->serverContext->sendJSONrequest('GET', '/index.php/apps/deck/boards/' . $id);
}
/**
* @Given /^create a stack named "([^"]*)"$/
*/
public function createAStackNamed($name) {
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/stacks', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/stacks', [
'title' => $name,
'boardId' => $this->board['id']
]);
$this->requestContext->getResponse()->getBody()->seek(0);
$this->stack = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->stack = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
* @Given /^create a card named "([^"]*)"$/
*/
public function createACardNamed($name) {
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/cards', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/cards', [
'title' => $name,
'stackId' => $this->stack['id']
]);
$this->requestContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
@@ -164,31 +169,31 @@ class BoardContext implements Context {
['shareType', 12],
['shareWith', (string)$this->card['id']],
]);
$this->serverContext->creatingShare($table);
$this->sharingContext->createAShareWith($table);
}
/**
* @Given /^set the description to "([^"]*)"$/
*/
public function setTheDescriptionTo($description) {
$this->requestContext->sendJSONrequest('PUT', '/index.php/apps/deck/cards/' . $this->card['id'], array_merge(
$this->serverContext->sendJSONrequest('PUT', '/index.php/apps/deck/cards/' . $this->card['id'], array_merge(
$this->card,
['description' => $description]
));
$this->requestContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
* @Given /^set the card attribute "([^"]*)" to "([^"]*)"$/
*/
public function setCardAttribute($attribute, $value) {
$this->requestContext->sendJSONrequest('PUT', '/index.php/apps/deck/cards/' . $this->card['id'], array_merge(
$this->serverContext->sendJSONrequest('PUT', '/index.php/apps/deck/cards/' . $this->card['id'], array_merge(
$this->card,
[$attribute => $value]
));
$this->requestContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->getResponse()->getBody(), true);
$this->serverContext->getResponse()->getBody()->seek(0);
$this->card = json_decode((string)$this->serverContext->getResponse()->getBody(), true);
}
/**
@@ -214,11 +219,11 @@ class BoardContext implements Context {
}
private function assignToCard($participant, $type) {
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/cards/' . $this->card['id'] .'/assign', [
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/cards/' . $this->card['id'] .'/assign', [
'userId' => $participant,
'type' => $type
]);
$this->requestContext->getResponse()->getBody()->seek(0);
$this->serverContext->getResponse()->getBody()->seek(0);
}
/**
@@ -229,7 +234,7 @@ class BoardContext implements Context {
return $label['title'] === $tag;
});
$label = array_shift($filteredLabels);
$this->requestContext->sendJSONrequest('POST', '/index.php/apps/deck/cards/' . $this->card['id'] .'/label/' . $label['id']);
$this->requestContext->getResponse()->getBody()->seek(0);
$this->serverContext->sendJSONrequest('POST', '/index.php/apps/deck/cards/' . $this->card['id'] .'/label/' . $label['id']);
$this->serverContext->getResponse()->getBody()->seek(0);
}
}

View File

@@ -2,12 +2,14 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use JuliusHaertl\NextcloudBehat\Context\ServerContext;
require_once __DIR__ . '/../../vendor/autoload.php';
class CommentContext implements Context {
use RequestTrait;
/** @var ServerContext */
protected $serverContext;
/** @var BoardContext */
protected $boardContext;
@@ -15,6 +17,7 @@ class CommentContext implements Context {
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->serverContext = $environment->getContext(ServerContext::class);
$this->boardContext = $environment->getContext('BoardContext');
}
@@ -23,7 +26,7 @@ class CommentContext implements Context {
*/
public function postACommentWithContentOnTheCard($content) {
$card = $this->boardContext->getLastUsedCard();
$this->requestContext->sendOCSRequest('POST', '/apps/deck/api/v1.0/cards/' . $card['id'] . '/comments', [
$this->serverContext->sendOCSRequest('POST', '/apps/deck/api/v1.0/cards/' . $card['id'] . '/comments', [
'message' => $content,
'parentId' => null
]);

View File

@@ -21,26 +21,21 @@
*
*/
declare(strict_types=1);
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Context\Context;
require_once __DIR__ . '/../../vendor/autoload.php';
class PendingContext implements Context {
trait RequestTrait {
/** @var RequestContext */
protected $requestContext;
/** @var \JuliusHaertl\NextcloudBehat\Context\ServerContext */
private $serverContext;
/** @BeforeScenario */
public function gatherRequestTraitContext(BeforeScenarioScope $scope) {
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->requestContext = $environment->getContext('RequestContext');
$this->serverContext = $environment->getContext(\JuliusHaertl\NextcloudBehat\Context\ServerContext::class);
}
public function getResponse() {
return $this->requestContext->getResponse();
}
}

View File

@@ -1,140 +0,0 @@
<?php
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\Assert;
use Behat\Behat\Context\Context;
use Psr\Http\Message\ResponseInterface;
require_once __DIR__ . '/../../vendor/autoload.php';
class RequestContext implements Context {
private $response;
/** @var ServerContext */
private $serverContext;
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->serverContext = $environment->getContext('ServerContext');
}
private function getBaseUrl() {
}
/**
* @Then the response should have a status code :code
* @param string $code
* @throws InvalidArgumentException
*/
public function theResponseShouldHaveStatusCode($code) {
$currentCode = $this->response->getStatusCode();
if ($currentCode !== (int)$code) {
throw new InvalidArgumentException(
sprintf(
'Expected %s as code got %s',
$code,
$currentCode
)
);
}
}
/**
* @Then /^the response Content-Type should be "([^"]*)"$/
* @param string $contentType
*/
public function theResponseContentTypeShouldbe($contentType) {
Assert::assertEquals($contentType, $this->response->getHeader('Content-Type')[0]);
}
/**
* @Then the response should be a JSON array with the following mandatory values
* @param TableNode $table
* @throws InvalidArgumentException
*/
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table) {
$this->response->getBody()->seek(0);
$expectedValues = $table->getColumnsHash();
$realResponseArray = json_decode($this->response->getBody()->getContents(), true);
foreach ($expectedValues as $value) {
if ((string)$realResponseArray[$value['key']] !== (string)$value['value']) {
throw new InvalidArgumentException(
sprintf(
'Expected %s for key %s got %s',
(string)$value['value'],
$value['key'],
(string)$realResponseArray[$value['key']]
)
);
}
}
}
/**
* @Then the response should be a JSON array with a length of :length
* @param int $length
* @throws InvalidArgumentException
*/
public function theResponseShouldBeAJsonArrayWithALengthOf($length) {
$this->response->getBody()->seek(0);
$realResponseArray = json_decode($this->response->getBody()->getContents(), true);
if ((int)count($realResponseArray) !== (int)$length) {
throw new InvalidArgumentException(
sprintf(
'Expected %d as length got %d',
$length,
count($realResponseArray)
)
);
}
}
public function sendJSONrequest($method, $url, $data = []) {
$client = new Client;
try {
$this->response = $client->request(
$method,
rtrim($this->serverContext->getBaseUrl(), '/') . '/' . ltrim($url, '/'),
[
'cookies' => $this->serverContext->getCookieJar(),
'json' => $data,
'headers' => [
'requesttoken' => $this->serverContext->getReqestToken(),
]
]
);
} catch (ClientException $e) {
$this->response = $e->getResponse();
}
}
public function sendOCSRequest($method, $url, $data = []) {
$client = new Client;
try {
$this->response = $client->request(
$method,
rtrim($this->serverContext->getBaseUrl(), '/') . '/ocs/v2.php/' . ltrim($url, '/'),
[
'cookies' => $this->serverContext->getCookieJar(),
'json' => $data,
'headers' => [
'requesttoken' => $this->serverContext->getReqestToken(),
'OCS-APIREQUEST' => 'true',
'Accept' => 'application/json'
]
]
);
} catch (ClientException $e) {
$this->response = $e->getResponse();
}
}
public function getResponse(): ResponseInterface {
return $this->response;
}
}

View File

@@ -3,12 +3,14 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PHPUnit\Framework\Assert;
use JuliusHaertl\NextcloudBehat\Context\ServerContext;
require_once __DIR__ . '/../../vendor/autoload.php';
class SearchContext implements Context {
use RequestTrait;
/** @var ServerContext */
protected $serverContext;
/** @var BoardContext */
protected $boardContext;
@@ -19,6 +21,7 @@ class SearchContext implements Context {
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->serverContext = $environment->getContext(ServerContext::class);
$this->boardContext = $environment->getContext('BoardContext');
}
@@ -27,10 +30,10 @@ class SearchContext implements Context {
* @param string $term
*/
public function searchingFor(string $term) {
$this->requestContext->sendOCSRequest('GET', '/apps/deck/api/v1.0/search?term=' . urlencode($term), []);
$this->requestContext->getResponse()->getBody()->seek(0);
$data = (string)$this->getResponse()->getBody();
$this->searchResults = json_decode($data, true);
$this->serverContext->sendOCSRequest('GET', '/apps/deck/api/v1.0/search?term=' . urlencode($term), []);
$this->serverContext->getResponse()->getBody()->seek(0);
$data = (string)$this->serverContext->getResponse()->getBody();
$this->searchResults = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
}
/**
@@ -39,9 +42,9 @@ class SearchContext implements Context {
* https://cloud.nextcloud.com/ocs/v2.php/search/providers/talk-conversations/search?term=an&from=%2Fapps%2Fdashboard%2F
*/
public function searchingForComments(string $term) {
$this->requestContext->sendOCSRequest('GET', '/search/providers/deck-comment/search?term=' . urlencode($term), []);
$this->requestContext->getResponse()->getBody()->seek(0);
$data = (string)$this->getResponse()->getBody();
$this->serverContext->sendOCSRequest('GET', '/search/providers/deck-comment/search?term=' . urlencode($term), []);
$this->serverContext->getResponse()->getBody()->seek(0);
$data = (string)$this->serverContext->getResponse()->getBody();
$this->unifiedSearchResult = json_decode($data, true);
}

View File

@@ -1,49 +0,0 @@
<?php
use Behat\Behat\Context\Context;
use GuzzleHttp\Cookie\CookieJar;
require_once __DIR__ . '/../../vendor/autoload.php';
class ServerContext implements Context {
use WebDav {
WebDav::__construct as private __tConstruct;
}
public function __construct($baseUrl) {
$this->rawBaseUrl = $baseUrl;
$this->__tConstruct($baseUrl . '/index.php/ocs/', ['admin', 'admin'], '123456');
}
/** @var string */
private $mappedUserId;
private $lastInsertIds = [];
/**
* @BeforeSuite
*/
public static function addFilesToSkeleton() {
}
/**
* @Given /^acting as user "([^"]*)"$/
*/
public function actingAsUser($user) {
$this->cookieJar = new CookieJar();
$this->loggingInUsingWebAs($user);
$this->asAn($user);
}
public function getBaseUrl(): string {
return $this->rawBaseUrl;
}
public function getCookieJar(): CookieJar {
return $this->cookieJar;
}
public function getReqestToken(): string {
return $this->requestToken;
}
}