diff options
| author | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
|---|---|---|
| committer | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
| commit | 4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch) | |
| tree | 5c07151ae61276d334e88f6309c30d439a85c12e /tests/jsonapi/ConsultationHelper.php | |
| parent | da0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff) | |
| parent | 97a188592c679890a25c37ab78463add76a52ff7 (diff) | |
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'tests/jsonapi/ConsultationHelper.php')
| -rw-r--r-- | tests/jsonapi/ConsultationHelper.php | 111 |
1 files changed, 2 insertions, 109 deletions
diff --git a/tests/jsonapi/ConsultationHelper.php b/tests/jsonapi/ConsultationHelper.php index a46cb47..f84820b 100644 --- a/tests/jsonapi/ConsultationHelper.php +++ b/tests/jsonapi/ConsultationHelper.php @@ -1,22 +1,9 @@ <?php -use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse; -use WoohooLabs\Yang\JsonApi\Schema\Document; -use WoohooLabs\Yang\JsonApi\Schema\Resource\ResourceObject; - -// Required for consultation mailer -require_once 'vendor/flexi/flexi.php'; +require_once __DIR__ . '/JSONAPIHelperTrait.php'; trait ConsultationHelper { - /** - * @var \UnitTester - */ - protected $tester; - - protected function _before() - { - \DBManager::getInstance()->setConnection('studip', $this->getModule('\\Helper\\StudipDb')->dbh); - } + use JSONAPIHelperTrait; protected static $BLOCK_DATA = [ 'room' => 'Testraum', @@ -91,23 +78,6 @@ trait ConsultationHelper return $block->slots->first(); } - 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_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates'); - } - - $result = $this->tester->withPHPLib($credentials, $fn); - - if (!$has_template_factory) { - unset($GLOBALS['template_factory']); - } - - return $result; - } - protected function createBookingForSlot(array $credentials, ConsultationSlot $slot, User $user): ConsultationBooking { return $this->withStudipEnv( @@ -125,81 +95,4 @@ trait ConsultationHelper } ); } - - 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)); - } - } } |
