|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2022 Cloud Creativity Limited |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace CloudCreativity\JsonApi\Testing\Tests\Assertions; |
| 21 | + |
| 22 | +use CloudCreativity\JsonApi\Testing\HttpMessage; |
| 23 | +use CloudCreativity\JsonApi\Testing\Tests\TestCase; |
| 24 | + |
| 25 | +class ErrorsTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + private array $error422 = [ |
| 31 | + 'detail' => 'Expecting a string.', |
| 32 | + 'source' => ['pointer' => '/data/attributes/title'], |
| 33 | + 'status' => '422', |
| 34 | + 'title' => 'Unprocessable Entity', |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + private array $error400 = [ |
| 41 | + 'detail' => 'Unexpected filter "foo".', |
| 42 | + 'source' => ['parameter' => 'filter.foo'], |
| 43 | + 'status' => '400', |
| 44 | + 'title' => 'Bad Request', |
| 45 | + ]; |
| 46 | + |
| 47 | + public function testErrorStatus(): void |
| 48 | + { |
| 49 | + $http = $this->createError(422, $this->error422); |
| 50 | + $http->assertErrorStatus($this->error422); |
| 51 | + |
| 52 | + $partial = $this->error422; |
| 53 | + unset($partial['source']); |
| 54 | + |
| 55 | + $invalid = $this->error422; |
| 56 | + $invalid['detail'] = 'Something went wrong.'; |
| 57 | + |
| 58 | + $http->assertErrorStatus($partial); |
| 59 | + |
| 60 | + $this->assertThatItFails( |
| 61 | + 'status 400 is 422', |
| 62 | + fn() => $http->withStatusCode(400)->assertErrorStatus($this->error422), |
| 63 | + ); |
| 64 | + |
| 65 | + $this->assertThatItFails( |
| 66 | + 'status 422 is 400', |
| 67 | + fn() => $http->assertErrorStatus($this->error400), |
| 68 | + ); |
| 69 | + |
| 70 | + $this->assertThatItFails( |
| 71 | + 'array at [/errors] only contains the subsets', |
| 72 | + fn() => $http->assertErrorStatus($invalid), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public function testExactErrorStatus(): void |
| 77 | + { |
| 78 | + $http = $this->createError(422, $this->error422); |
| 79 | + $http->assertExactErrorStatus($this->error422); |
| 80 | + |
| 81 | + $partial = $this->error422; |
| 82 | + unset($partial['source']); |
| 83 | + |
| 84 | + $invalid = $this->error422; |
| 85 | + $invalid['detail'] = 'Something went wrong.'; |
| 86 | + |
| 87 | + $this->assertThatItFails( |
| 88 | + 'list at [/errors] only contains the values', |
| 89 | + fn() => $http->assertExactErrorStatus($partial), |
| 90 | + ); |
| 91 | + |
| 92 | + $this->assertThatItFails( |
| 93 | + 'status 400 is 422', |
| 94 | + fn() => $http->withStatusCode(400)->assertExactErrorStatus($this->error422), |
| 95 | + ); |
| 96 | + |
| 97 | + $this->assertThatItFails( |
| 98 | + 'status 422 is 400', |
| 99 | + fn() => $http->assertExactErrorStatus($this->error400), |
| 100 | + ); |
| 101 | + |
| 102 | + $this->assertThatItFails( |
| 103 | + 'list at [/errors] only contains the values', |
| 104 | + fn() => $http->assertExactErrorStatus($invalid), |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public function testErrorInvalidContentType(): void |
| 109 | + { |
| 110 | + $http = $this |
| 111 | + ->createError(400, $this->error400) |
| 112 | + ->withContentType('application/json'); |
| 113 | + |
| 114 | + $this->assertThatItFails( |
| 115 | + 'media type', |
| 116 | + fn() => $http->assertErrorStatus($this->error400), |
| 117 | + ); |
| 118 | + |
| 119 | + $this->assertThatItFails( |
| 120 | + 'media type', |
| 121 | + fn() => $http->assertExactErrorStatus($this->error400), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + public function testErrors(): void |
| 126 | + { |
| 127 | + $http = $this->createErrors(); |
| 128 | + $expected = [$this->error400, $this->error422]; // order not significant. |
| 129 | + |
| 130 | + $partial = $this->error400; |
| 131 | + unset($partial['detail']); |
| 132 | + |
| 133 | + $invalid = $this->error422; |
| 134 | + $invalid['detail'] = 'Oops! Something went wrong.'; |
| 135 | + |
| 136 | + $http->assertErrors(400, $expected); |
| 137 | + $http->assertErrors(400, [$partial, $this->error422]); |
| 138 | + |
| 139 | + $this->assertThatItFails( |
| 140 | + 'status 400 is 422', |
| 141 | + fn() => $http->assertErrors(422, $expected), |
| 142 | + ); |
| 143 | + |
| 144 | + $this->assertThatItFails( |
| 145 | + 'array at [/errors] only contains the subsets', |
| 146 | + fn() => $http->assertErrors(400, [$this->error400, $invalid]), |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + public function testExactErrors(): void |
| 151 | + { |
| 152 | + $http = $this->createErrors(); |
| 153 | + $expected = [$this->error400, $this->error422]; // order not significant. |
| 154 | + |
| 155 | + $partial = $this->error400; |
| 156 | + unset($partial['detail']); |
| 157 | + |
| 158 | + $invalid = $this->error422; |
| 159 | + $invalid['detail'] = 'Oops! Something went wrong.'; |
| 160 | + |
| 161 | + $http->assertExactErrors(400, $expected); |
| 162 | + |
| 163 | + $this->assertThatItFails( |
| 164 | + 'list at [/errors] only contains the values', |
| 165 | + fn() => $http->assertExactErrors(400, [$partial, $this->error422]), |
| 166 | + ); |
| 167 | + |
| 168 | + $this->assertThatItFails( |
| 169 | + 'status 400 is 422', |
| 170 | + fn() => $http->assertExactErrors(422, $expected), |
| 171 | + ); |
| 172 | + |
| 173 | + $this->assertThatItFails( |
| 174 | + 'list at [/errors] only contains the values', |
| 175 | + fn() => $http->assertExactErrors(400, [$this->error400, $invalid]), |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + public function testErrorsInvalidContentType(): void |
| 180 | + { |
| 181 | + $expected = [$this->error422, $this->error400]; |
| 182 | + |
| 183 | + $http = $this |
| 184 | + ->createErrors() |
| 185 | + ->withContentType('application/json'); |
| 186 | + |
| 187 | + $this->assertThatItFails( |
| 188 | + 'media type', |
| 189 | + fn() => $http->assertErrors(400, $expected), |
| 190 | + ); |
| 191 | + |
| 192 | + $this->assertThatItFails( |
| 193 | + 'media type', |
| 194 | + fn() => $http->assertExactErrors(400, $expected), |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @param int $status |
| 200 | + * @param array $error |
| 201 | + * @return HttpMessage |
| 202 | + */ |
| 203 | + private function createError(int $status, array $error): HttpMessage |
| 204 | + { |
| 205 | + return new HttpMessage( |
| 206 | + $status, |
| 207 | + 'application/vnd.api+json', |
| 208 | + json_encode(['errors' => [$error]]), |
| 209 | + ['Content-Type' => 'application/vnd.api+json', 'Accept' => 'application/vnd.api+json'], |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * @return HttpMessage |
| 215 | + */ |
| 216 | + private function createErrors(): HttpMessage |
| 217 | + { |
| 218 | + return new HttpMessage( |
| 219 | + 400, |
| 220 | + 'application/vnd.api+json', |
| 221 | + json_encode(['errors' => [$this->error422, $this->error400]]), |
| 222 | + ['Content-Type' => 'application/vnd.api+json', 'Accept' => 'application/vnd.api+json'], |
| 223 | + ); |
| 224 | + } |
| 225 | +} |
0 commit comments