]> BookStack Code Mirror - bookstack/blob - tests/Api/TestsApi.php
Apply fixes from StyleCI
[bookstack] / tests / Api / TestsApi.php
1 <?php
2
3 namespace Tests\Api;
4
5 trait TestsApi
6 {
7     protected $apiTokenId = 'apitoken';
8     protected $apiTokenSecret = 'password';
9
10     /**
11      * Set the API editor role as the current user via the API driver.
12      */
13     protected function actingAsApiEditor()
14     {
15         $this->actingAs($this->getEditor(), 'api');
16
17         return $this;
18     }
19
20     /**
21      * Format the given items into a standardised error format.
22      */
23     protected function errorResponse(string $message, int $code): array
24     {
25         return ['error' => ['code' => $code, 'message' => $message]];
26     }
27
28     /**
29      * Format the given (field_name => ["messages"]) array
30      * into a standard validation response format.
31      */
32     protected function validationResponse(array $messages): array
33     {
34         $err = $this->errorResponse('The given data was invalid.', 422);
35         $err['error']['validation'] = $messages;
36
37         return $err;
38     }
39
40     /**
41      * Get an approved API auth header.
42      */
43     protected function apiAuthHeader(): array
44     {
45         return [
46             'Authorization' => "Token {$this->apiTokenId}:{$this->apiTokenSecret}",
47         ];
48     }
49 }