|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2016 Cloud Creativity Limited |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace CloudCreativity\LaravelJsonApi\Testing; |
| 20 | + |
| 21 | +use CloudCreativity\JsonApi\Testing\ResourcesTester; |
| 22 | +use CloudCreativity\JsonApi\Testing\ResourceTester; |
| 23 | +use Illuminate\Contracts\Routing\UrlRoutable; |
| 24 | +use Illuminate\Http\Response; |
| 25 | +use Illuminate\Support\Collection; |
| 26 | +use InvalidArgumentException; |
| 27 | +use Neomerx\JsonApi\Contracts\Document\DocumentInterface as Keys; |
| 28 | +use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
| 29 | +use Neomerx\JsonApi\Contracts\Http\Query\QueryParametersParserInterface as Params; |
| 30 | + |
| 31 | +/** |
| 32 | + * Class InteractsWithResources |
| 33 | + * @package CloudCreativity\LaravelJsonApi |
| 34 | + * |
| 35 | + * This trait MUST be used on a class that uses this trait: |
| 36 | + * Illuminate\Foundation\Testing\Concerns\MakesHttpRequests |
| 37 | + */ |
| 38 | +trait InteractsWithResources |
| 39 | +{ |
| 40 | + |
| 41 | + use MakesJsonApiRequests; |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the resource type that this test case is testing. |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + abstract protected function getResourceType(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the route prefix that should be added to the resource type to create the route name. |
| 52 | + * |
| 53 | + * Test case classes should overload this method if they are registering resource types |
| 54 | + * under a group with a route name. E.g. if your resource `posts` is registered under a route |
| 55 | + * group name alias of `api::` then this method needs to return `api::` as the route name |
| 56 | + * for the `posts` resource will be `api::posts`. |
| 57 | + * |
| 58 | + * @return string|null |
| 59 | + */ |
| 60 | + protected function getRoutePrefix() |
| 61 | + { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array $params |
| 67 | + * @param array $headers |
| 68 | + * @return $this |
| 69 | + */ |
| 70 | + protected function doSearch(array $params = [], array $headers = []) |
| 71 | + { |
| 72 | + $params = $this->addDefaultRouteParams($params); |
| 73 | + $route = $this->resolveRouteName(); |
| 74 | + $uri = $this->linkTo()->index($route, $params); |
| 75 | + |
| 76 | + return $this->jsonApi('GET', $uri, [], $headers); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param array|Collection $ids |
| 81 | + * the ids - may contain UrlRoutable objects (includes Models) |
| 82 | + * @param array $params |
| 83 | + * @param array $headers |
| 84 | + * @return $this |
| 85 | + */ |
| 86 | + protected function doSearchById($ids, array $params = [], array $headers = []) |
| 87 | + { |
| 88 | + if (!isset($params[Params::PARAM_FILTER])) { |
| 89 | + $params[Params::PARAM_FILTER] = []; |
| 90 | + } |
| 91 | + |
| 92 | + $params[Params::PARAM_FILTER][Keys::KEYWORD_ID] = $this->normalizeIds($ids); |
| 93 | + |
| 94 | + return $this->doSearch($params, $headers); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param array $data |
| 99 | + * @param array $params |
| 100 | + * @param array $headers |
| 101 | + * @return $this |
| 102 | + */ |
| 103 | + protected function doCreate(array $data, array $params = [], array $headers = []) |
| 104 | + { |
| 105 | + $params = $this->addDefaultRouteParams($params); |
| 106 | + $route = $this->resolveRouteName(); |
| 107 | + $uri = $this->linkTo()->index($route, $params); |
| 108 | + |
| 109 | + return $this->jsonApi('POST', $uri, ['data' => $data], $headers); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param mixed $resourceId |
| 114 | + * @param array $params |
| 115 | + * @param array $headers |
| 116 | + * @return $this |
| 117 | + */ |
| 118 | + protected function doRead($resourceId, array $params = [], array $headers = []) |
| 119 | + { |
| 120 | + $params = $this->addDefaultRouteParams($params); |
| 121 | + $route = $this->resolveRouteName(); |
| 122 | + $uri = $this->linkTo()->resource($route, $resourceId, $params); |
| 123 | + |
| 124 | + return $this->jsonApi('GET', $uri, $headers); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param array $data |
| 129 | + * @param array $params |
| 130 | + * @param array $headers |
| 131 | + * @return $this |
| 132 | + */ |
| 133 | + protected function doUpdate(array $data, array $params = [], array $headers = []) |
| 134 | + { |
| 135 | + $id = isset($data[Keys::KEYWORD_ID]) ? $data[Keys::KEYWORD_ID] : null; |
| 136 | + |
| 137 | + if (empty($id)) { |
| 138 | + throw new InvalidArgumentException('Expecting data to contain a resource id'); |
| 139 | + } |
| 140 | + |
| 141 | + $params = $this->addDefaultRouteParams($params); |
| 142 | + $route = $this->resolveRouteName(); |
| 143 | + $uri = $this->linkTo()->resource($route, $id, $params); |
| 144 | + |
| 145 | + return $this->jsonApi('PATCH', $uri, ['data' => $data], $headers); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param $resourceId |
| 150 | + * @param array $params |
| 151 | + * @param array $headers |
| 152 | + * @return $this |
| 153 | + */ |
| 154 | + protected function doDelete($resourceId, array $params = [], array $headers = []) |
| 155 | + { |
| 156 | + $params = $this->addDefaultRouteParams($params); |
| 157 | + $route = $this->resolveRouteName(); |
| 158 | + $uri = $this->linkTo()->resource($route, $resourceId, $params); |
| 159 | + |
| 160 | + return $this->jsonApi('DELETE', $uri, [], $headers); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Assert that a search response is a collection only containing the expected resource type. |
| 165 | + * |
| 166 | + * @param string $contentType |
| 167 | + * @return ResourcesTester |
| 168 | + */ |
| 169 | + protected function assertSearchResponse($contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE) |
| 170 | + { |
| 171 | + $this->assertJsonApiResponse(Response::HTTP_OK, $contentType); |
| 172 | + |
| 173 | + return $this |
| 174 | + ->seeDocument() |
| 175 | + ->assertResourceCollection() |
| 176 | + ->assertTypes($this->getResourceType()); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Assert that a search response contains a singleton resource with the expected id. |
| 181 | + * |
| 182 | + * @param string|int|UrlRoutable $expectedId |
| 183 | + * @param string $contentType |
| 184 | + * @return ResourceTester |
| 185 | + * @todo needs to support `null` responses |
| 186 | + */ |
| 187 | + protected function assertSearchOneResponse($expectedId, $contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE) |
| 188 | + { |
| 189 | + if ($expectedId instanceof UrlRoutable) { |
| 190 | + $expectedId = $expectedId->getRouteKey(); |
| 191 | + } |
| 192 | + |
| 193 | + $this->assertJsonApiResponse(Response::HTTP_OK, $contentType); |
| 194 | + |
| 195 | + return $this |
| 196 | + ->seeDocument() |
| 197 | + ->assertResource() |
| 198 | + ->assertIs($this->getResourceType(), $expectedId); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Assert that the response to a search by id(s) request contains the expected ids. |
| 203 | + * |
| 204 | + * @param array|Collection $expectedIds |
| 205 | + * the ids - may contain UrlRoutable objects (e.g. Models) |
| 206 | + * @param string $contentType |
| 207 | + * @return ResourcesTester |
| 208 | + */ |
| 209 | + protected function assertSearchByIdResponse($expectedIds, $contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE) |
| 210 | + { |
| 211 | + $this->assertJsonApiResponse(Response::HTTP_OK, $contentType); |
| 212 | + |
| 213 | + return $this |
| 214 | + ->seeDocument() |
| 215 | + ->assertResourceCollection() |
| 216 | + ->assertContainsOnly([ |
| 217 | + $this->getResourceType() => $this->normalizeIds($expectedIds), |
| 218 | + ]); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * @return string |
| 223 | + */ |
| 224 | + protected function resolveRouteName() |
| 225 | + { |
| 226 | + return $this->getRoutePrefix() . $this->getResourceType(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Add default parameters to those provided to a `do*` method. |
| 231 | + * |
| 232 | + * Classes can override this method if they need to add any default parameters for constructing |
| 233 | + * the route link. |
| 234 | + * |
| 235 | + * @param array $params |
| 236 | + * @return array |
| 237 | + */ |
| 238 | + protected function addDefaultRouteParams(array $params) |
| 239 | + { |
| 240 | + return $params; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Normalize ids for a find many request |
| 245 | + * |
| 246 | + * @param array|Collection $ids |
| 247 | + * @return array |
| 248 | + */ |
| 249 | + protected function normalizeIds($ids) |
| 250 | + { |
| 251 | + $ids = new Collection($ids); |
| 252 | + |
| 253 | + return $ids->map(function ($id) { |
| 254 | + return ($id instanceof UrlRoutable) ? $id->getRouteKey() : $id; |
| 255 | + })->all(); |
| 256 | + } |
| 257 | +} |
0 commit comments