|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Api; |
| 4 | + |
| 5 | +use App\Entity\Comment; |
| 6 | +use App\Entity\Tweet; |
| 7 | +use App\Entity\User; |
| 8 | +use App\Exceptions\ErrorCode; |
| 9 | +use Illuminate\Foundation\Testing\TestCase as BaseTestCase; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 12 | +use Illuminate\Support\Facades\Auth; |
| 13 | +use Tests\CreatesApplication; |
| 14 | + |
| 15 | +abstract class ApiTestCase extends BaseTestCase |
| 16 | +{ |
| 17 | + use CreatesApplication; |
| 18 | + use RefreshDatabase; |
| 19 | + |
| 20 | + protected const AUTH_PREFIX = 'Bearer'; |
| 21 | + |
| 22 | + protected const ROOT_RESPONSE_KEY = 'data'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + * |
| 27 | + * User response item structure |
| 28 | + */ |
| 29 | + protected const USER_RESOURCE_STRUCTURE = [ |
| 30 | + 'id', |
| 31 | + 'name', |
| 32 | + 'nickname', |
| 33 | + 'email', |
| 34 | + 'avatar' |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array |
| 39 | + * |
| 40 | + * Comment response item structure |
| 41 | + */ |
| 42 | + protected const COMMENT_RESOURCE_STRUCTURE = [ |
| 43 | + 'id', |
| 44 | + 'body', |
| 45 | + 'author_id', |
| 46 | + 'created_at', |
| 47 | + 'updated_at', |
| 48 | + 'author' => self::USER_RESOURCE_STRUCTURE |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + private $jwtToken; |
| 55 | + |
| 56 | + protected function setUp(): void |
| 57 | + { |
| 58 | + parent::setUp(); |
| 59 | + |
| 60 | + $this->seedFakeData(); |
| 61 | + } |
| 62 | + |
| 63 | + protected function seedFakeData(int $itemsAmount = 2): void |
| 64 | + { |
| 65 | + factory(User::class, $itemsAmount)->create(); |
| 66 | + factory(Tweet::class, $itemsAmount)->create(); |
| 67 | + factory(Comment::class, $itemsAmount)->create(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Override call method |
| 72 | + * |
| 73 | + * Appends jwt auth header |
| 74 | + * |
| 75 | + * @param string $method |
| 76 | + * @param string $uri |
| 77 | + * @param array $parameters |
| 78 | + * @param array $cookies |
| 79 | + * @param array $files |
| 80 | + * @param array $server |
| 81 | + * @param null $content |
| 82 | + * @return \Illuminate\Foundation\Testing\TestResponse |
| 83 | + */ |
| 84 | + public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) |
| 85 | + { |
| 86 | + if ($this->jwtToken) { |
| 87 | + // append token auth header |
| 88 | + $this->withHeader('AUTHORIZATION', self::AUTH_PREFIX . " {$this->jwtToken}"); |
| 89 | + } |
| 90 | + |
| 91 | + return parent::call($method, $uri, $parameters, $cookies, $files, $server, $content); |
| 92 | + } |
| 93 | + |
| 94 | + private function authenticate(Authenticatable $user): void |
| 95 | + { |
| 96 | + // Auth::login() returns jwt token from JWTGuard class |
| 97 | + $this->jwtToken = Auth::login($user); |
| 98 | + } |
| 99 | + |
| 100 | + protected function actingWithToken(Authenticatable $user = null) : self |
| 101 | + { |
| 102 | + $user = $user ?? factory(User::class)->create(); |
| 103 | + |
| 104 | + $this->authenticate($user); |
| 105 | + |
| 106 | + return $this->actingAs($user, 'api'); |
| 107 | + } |
| 108 | + |
| 109 | + private function assertUriIsValid(string $uri): void |
| 110 | + { |
| 111 | + if (empty($uri)) { |
| 112 | + throw new \InvalidArgumentException('Request URI cannot be empty.'); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + protected function assertCollectionResponse(string $uri, array $jsonStructure, array $queryParams = []): void |
| 117 | + { |
| 118 | + $this->assertUriIsValid($uri); |
| 119 | + |
| 120 | + $response = $this->call('GET', $uri, $queryParams)->assertOk(); |
| 121 | + |
| 122 | + // assert response data key doesn't contain empty array |
| 123 | + $this->assertNotEmpty($response->json('data')); |
| 124 | + |
| 125 | + $response->assertJsonStructure([ |
| 126 | + // * means to assert each array item for same structure |
| 127 | + self::ROOT_RESPONSE_KEY => ['*' => $jsonStructure] |
| 128 | + ]); |
| 129 | + } |
| 130 | + |
| 131 | + protected function assertCollectionErrorResponse(string $uri, array $queryParams = []): void |
| 132 | + { |
| 133 | + $this->assertUriIsValid($uri); |
| 134 | + |
| 135 | + $this->call('GET', $uri, $queryParams)->assertJsonStructure(['errors' => []]); |
| 136 | + } |
| 137 | + |
| 138 | + protected function assertItemResponse(string $uri, array $jsonStructure): void |
| 139 | + { |
| 140 | + $this->assertUriIsValid($uri); |
| 141 | + |
| 142 | + $this->get($uri) |
| 143 | + ->assertOk() |
| 144 | + ->assertJsonStructure([self::ROOT_RESPONSE_KEY => $jsonStructure]); |
| 145 | + } |
| 146 | + |
| 147 | + protected function assertNotFoundResponse(string $uri): void |
| 148 | + { |
| 149 | + $this->assertUriIsValid($uri); |
| 150 | + |
| 151 | + $this->get($uri) |
| 152 | + ->assertNotFound() |
| 153 | + ->assertExactJson([ |
| 154 | + 'errors' => [ |
| 155 | + [ |
| 156 | + 'code' => ErrorCode::NOT_FOUND, |
| 157 | + 'message' => 'Resource not found.' |
| 158 | + ] |
| 159 | + ] |
| 160 | + ]); |
| 161 | + } |
| 162 | + |
| 163 | + protected function assertErrorResponse(string $uri, array $attributes = [], string $httpMethod = 'POST'): void |
| 164 | + { |
| 165 | + $this->assertUriIsValid($uri); |
| 166 | + |
| 167 | + $this->json($httpMethod, $uri, $attributes) |
| 168 | + ->assertStatus(400) |
| 169 | + ->assertJsonStructure(['errors' => []]); |
| 170 | + } |
| 171 | + |
| 172 | + protected function assertCreatedResponse(string $uri, array $attributes): void |
| 173 | + { |
| 174 | + $this->assertUriIsValid($uri); |
| 175 | + $this->assertAttributesIsValid($attributes); |
| 176 | + |
| 177 | + $this->json('POST', $uri, $attributes) |
| 178 | + ->assertStatus(201) |
| 179 | + ->assertJsonStructure(['data' => ['id']]); |
| 180 | + } |
| 181 | + |
| 182 | + protected function assertUpdatedResponse(string $uri, array $attributes): void |
| 183 | + { |
| 184 | + $this->assertUriIsValid($uri); |
| 185 | + $this->assertAttributesIsValid($attributes); |
| 186 | + |
| 187 | + $this->json('PUT', $uri, $attributes)->assertStatus(200); |
| 188 | + } |
| 189 | + |
| 190 | + protected function createResourceItemUri(string $uri, int $id): string |
| 191 | + { |
| 192 | + $this->assertUriIsValid($uri); |
| 193 | + |
| 194 | + return $uri . '/' . $id; |
| 195 | + } |
| 196 | + |
| 197 | + private function assertAttributesIsValid(array $attributes): void |
| 198 | + { |
| 199 | + if (empty($attributes)) { |
| 200 | + throw new \InvalidArgumentException('Request attributes are empty.'); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments