aboutsummaryrefslogtreecommitdiff
path: root/tests/jsonapi/JSONAPIHelperTrait.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2024-05-21 11:41:55 +0000
committerDavid Siegfried <david.siegfried@uni-vechta.de>2024-05-21 11:41:55 +0000
commitc21817bfe8bd8695f612e47c2a1d8de03148fb88 (patch)
tree791a588cd0c276f117e1bf8f70a4b56d9c83013f /tests/jsonapi/JSONAPIHelperTrait.php
parent98ee46ee0cb82934ff111a72d5cb7a93b68219d8 (diff)
implement jsonapi for clipboard and clipboard items and replace old clipboards route with new ones, fixes #4198
Closes #4198 Merge request studip/studip!3029
Diffstat (limited to 'tests/jsonapi/JSONAPIHelperTrait.php')
-rw-r--r--tests/jsonapi/JSONAPIHelperTrait.php117
1 files changed, 117 insertions, 0 deletions
diff --git a/tests/jsonapi/JSONAPIHelperTrait.php b/tests/jsonapi/JSONAPIHelperTrait.php
new file mode 100644
index 0000000..666e198
--- /dev/null
+++ b/tests/jsonapi/JSONAPIHelperTrait.php
@@ -0,0 +1,117 @@
+<?php
+
+use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;
+use WoohooLabs\Yang\JsonApi\Schema\Document;
+use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject;
+
+trait JSONAPIHelperTrait
+{
+ protected JSONAPITester $tester;
+
+ protected function _before()
+ {
+ DBManager::getInstance()->setConnection(
+ 'studip',
+ $this->getModule('\\Helper\\StudipDb')->dbh
+ );
+ }
+
+ protected function withStudipEnv(array $credentials, callable $fn)
+ {
+ // Create global template factory if neccessary
+ $has_template_factory = isset($GLOBALS['template_factory']);
+ if (!$has_template_factory) {
+ $GLOBALS['template_factory'] = new Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
+ }
+
+ $result = $this->tester->withPHPLib($credentials, $fn);
+
+ if (!$has_template_factory) {
+ unset($GLOBALS['template_factory']);
+ }
+
+ return $result;
+ }
+
+ protected function sendMockRequest(string $route, string $handler, array $credentials, array $variables = [], array $options = []): JsonApiResponse
+ {
+ $options = array_merge([
+ 'method' => 'GET',
+ 'considered_successful' => [200],
+ 'json_body' => null,
+ ], $options);
+
+ $app = $this->tester->createApp(
+ $credentials,
+ strtolower($options['method']),
+ $route,
+ $handler
+ );
+
+ $evaluated_route = preg_replace_callback(
+ '/\{(.+?)(:[^}]+)?}/',
+ function ($match) use ($variables) {
+ $key = $match[1];
+ if (!isset($variables[$key])) {
+ throw new Exception("No variable '{$key}' defined");
+ }
+ return $variables[$key];
+ },
+ $route
+ );
+
+ $requestBuilder = $this->tester->createRequestBuilder($credentials);
+ $requestBuilder->setUri($evaluated_route)->setMethod(strtoupper($options['method']));
+
+ if (isset($options['json_body'])) {
+ $requestBuilder->setJsonApiBody($options['json_body']);
+
+ }
+
+ /** @var JsonApiResponse $response */
+ $response = $this->withStudipEnv($credentials, function () use ($app, $requestBuilder) {
+ return $this->tester->sendMockRequest($app, $requestBuilder->getRequest());
+ });
+
+ if ($options['considered_successful']) {
+ $this->assertTrue(
+ $response->isSuccessful($options['considered_successful']),
+ 'Actual status code is ' . $response->getStatusCode()
+ );
+ }
+
+ return $response;
+ }
+
+ protected function getSingleResourceDocument(JsonApiResponse $response): Document
+ {
+ $this->assertTrue($response->hasDocument());
+
+ $document = $response->document();
+ $this->assertTrue($document->isSingleResourceDocument());
+
+ return $document;
+ }
+
+ protected function getResourceCollectionDocument(JsonApiResponse $response): Document
+ {
+ $this->assertTrue($response->hasDocument());
+
+ $document = $response->document();
+ $this->assertTrue($document->isResourceCollectionDocument());
+
+ return $document;
+ }
+
+ protected function assertHasRelations(ResourceObject $resource, ...$relations)
+ {
+ foreach ($relations as $relation) {
+ $this->assertTrue($resource->hasRelationship($relation));
+ }
+ }
+
+ protected function getResourceFromResponse(JsonApiResponse $response): ResourceObject
+ {
+ return $this->getSingleResourceDocument($response)->primaryResource();
+ }
+}