|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tobyz\Tests\JsonApiServer\feature; |
| 4 | + |
| 5 | +use Tobyz\JsonApiServer\Endpoint\Create; |
| 6 | +use Tobyz\JsonApiServer\Exception\UnprocessableEntityException; |
| 7 | +use Tobyz\JsonApiServer\JsonApi; |
| 8 | +use Tobyz\JsonApiServer\Schema\Field\Attribute; |
| 9 | +use Tobyz\JsonApiServer\Schema\Type\Arr; |
| 10 | +use Tobyz\JsonApiServer\Schema\Type\Str; |
| 11 | +use Tobyz\Tests\JsonApiServer\AbstractTestCase; |
| 12 | +use Tobyz\Tests\JsonApiServer\MockResource; |
| 13 | + |
| 14 | +class ArrTest extends AbstractTestCase |
| 15 | +{ |
| 16 | + private JsonApi $api; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + $this->api = new JsonApi(); |
| 21 | + } |
| 22 | + |
| 23 | + public function test_validates_array() |
| 24 | + { |
| 25 | + $this->api->resource( |
| 26 | + new MockResource( |
| 27 | + 'customers', |
| 28 | + endpoints: [Create::make()], |
| 29 | + fields: [ |
| 30 | + Attribute::make('featureToggles') |
| 31 | + ->type(Arr::make()) |
| 32 | + ->writable(), |
| 33 | + ], |
| 34 | + ), |
| 35 | + ); |
| 36 | + |
| 37 | + $this->expectException(UnprocessableEntityException::class); |
| 38 | + |
| 39 | + $this->api->handle( |
| 40 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 41 | + 'data' => ['type' => 'customers', 'attributes' => ['featureToggles' => 1]], |
| 42 | + ]), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_invalid_min_length() |
| 47 | + { |
| 48 | + $this->api->resource( |
| 49 | + new MockResource( |
| 50 | + 'customers', |
| 51 | + endpoints: [Create::make()], |
| 52 | + fields: [ |
| 53 | + Attribute::make('featureToggles') |
| 54 | + ->type(Arr::make()->minItems(1)) |
| 55 | + ->writable(), |
| 56 | + ], |
| 57 | + ), |
| 58 | + ); |
| 59 | + |
| 60 | + $this->expectException(UnprocessableEntityException::class); |
| 61 | + |
| 62 | + $this->api->handle( |
| 63 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 64 | + 'data' => ['type' => 'customers', 'attributes' => ['featureToggles' => []]], |
| 65 | + ]), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function test_invalid_max_length() |
| 70 | + { |
| 71 | + $this->api->resource( |
| 72 | + new MockResource( |
| 73 | + 'customers', |
| 74 | + endpoints: [Create::make()], |
| 75 | + fields: [ |
| 76 | + Attribute::make('featureToggles') |
| 77 | + ->type(Arr::make()->maxItems(1)) |
| 78 | + ->writable(), |
| 79 | + ], |
| 80 | + ), |
| 81 | + ); |
| 82 | + |
| 83 | + $this->expectException(UnprocessableEntityException::class); |
| 84 | + |
| 85 | + $this->api->handle( |
| 86 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 87 | + 'data' => ['type' => 'customers', 'attributes' => ['featureToggles' => [1, 2]]], |
| 88 | + ]), |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_invalid_uniqueness() |
| 93 | + { |
| 94 | + $this->api->resource( |
| 95 | + new MockResource( |
| 96 | + 'customers', |
| 97 | + endpoints: [Create::make()], |
| 98 | + fields: [ |
| 99 | + Attribute::make('featureToggles') |
| 100 | + ->type(Arr::make()->uniqueItems()) |
| 101 | + ->writable(), |
| 102 | + ], |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + $this->expectException(UnprocessableEntityException::class); |
| 107 | + |
| 108 | + $this->api->handle( |
| 109 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 110 | + 'data' => ['type' => 'customers', 'attributes' => ['featureToggles' => [1, 1]]], |
| 111 | + ]), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public function test_valid_items_constraints() |
| 116 | + { |
| 117 | + $this->api->resource( |
| 118 | + new MockResource( |
| 119 | + 'customers', |
| 120 | + endpoints: [Create::make()], |
| 121 | + fields: [ |
| 122 | + Attribute::make('featureToggles') |
| 123 | + ->type( |
| 124 | + Arr::make() |
| 125 | + ->minItems(2) |
| 126 | + ->maxItems(4) |
| 127 | + ->uniqueItems(), |
| 128 | + ) |
| 129 | + ->writable(), |
| 130 | + ], |
| 131 | + ), |
| 132 | + ); |
| 133 | + |
| 134 | + $response = $this->api->handle( |
| 135 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 136 | + 'data' => ['type' => 'customers', 'attributes' => ['featureToggles' => [1, 2, 3]]], |
| 137 | + ]), |
| 138 | + ); |
| 139 | + |
| 140 | + $this->assertJsonApiDocumentSubset( |
| 141 | + ['data' => ['attributes' => ['featureToggles' => [1, 2, 3]]]], |
| 142 | + $response->getBody(), |
| 143 | + true, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + public function test_invalid_items() |
| 148 | + { |
| 149 | + $this->api->resource( |
| 150 | + new MockResource( |
| 151 | + 'customers', |
| 152 | + endpoints: [Create::make()], |
| 153 | + fields: [ |
| 154 | + Attribute::make('featureToggles') |
| 155 | + ->type(Arr::make()->items(Str::make()->enum(['valid']))) |
| 156 | + ->writable(), |
| 157 | + ], |
| 158 | + ), |
| 159 | + ); |
| 160 | + |
| 161 | + $this->expectException(UnprocessableEntityException::class); |
| 162 | + |
| 163 | + $this->api->handle( |
| 164 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 165 | + 'data' => [ |
| 166 | + 'type' => 'customers', |
| 167 | + 'attributes' => ['featureToggles' => ['valid', 'invalid']], |
| 168 | + ], |
| 169 | + ]), |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + public function test_valid_items() |
| 174 | + { |
| 175 | + $this->api->resource( |
| 176 | + new MockResource( |
| 177 | + 'customers', |
| 178 | + endpoints: [Create::make()], |
| 179 | + fields: [ |
| 180 | + Attribute::make('featureToggles') |
| 181 | + ->type(Arr::make()->items(Str::make()->enum(['valid1', 'valid2']))) |
| 182 | + ->writable(), |
| 183 | + ], |
| 184 | + ), |
| 185 | + ); |
| 186 | + |
| 187 | + $response = $this->api->handle( |
| 188 | + $this->buildRequest('POST', '/customers')->withParsedBody([ |
| 189 | + 'data' => [ |
| 190 | + 'type' => 'customers', |
| 191 | + 'attributes' => ['featureToggles' => ['valid1', 'valid2']], |
| 192 | + ], |
| 193 | + ]), |
| 194 | + ); |
| 195 | + |
| 196 | + $this->assertJsonApiDocumentSubset( |
| 197 | + ['data' => ['attributes' => ['featureToggles' => ['valid1', 'valid2']]]], |
| 198 | + $response->getBody(), |
| 199 | + true, |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + public function test_schema() |
| 204 | + { |
| 205 | + $this->assertEquals( |
| 206 | + [ |
| 207 | + 'type' => 'array', |
| 208 | + 'minItems' => 0, |
| 209 | + 'maxItems' => null, |
| 210 | + 'uniqueItems' => false, |
| 211 | + 'items' => null, |
| 212 | + ], |
| 213 | + Arr::make()->schema(), |
| 214 | + ); |
| 215 | + |
| 216 | + $this->assertEquals( |
| 217 | + [ |
| 218 | + 'type' => 'array', |
| 219 | + 'minItems' => 1, |
| 220 | + 'maxItems' => 10, |
| 221 | + 'uniqueItems' => true, |
| 222 | + 'items' => [ |
| 223 | + 'type' => 'string', |
| 224 | + 'enum' => ['valid1', 'valid2'], |
| 225 | + 'minLength' => 0, |
| 226 | + 'maxLength' => null, |
| 227 | + 'pattern' => null, |
| 228 | + 'format' => null, |
| 229 | + ], |
| 230 | + ], |
| 231 | + Arr::make() |
| 232 | + ->minItems(1) |
| 233 | + ->maxItems(10) |
| 234 | + ->uniqueItems() |
| 235 | + ->items(Str::make()->enum(['valid1', 'valid2'])) |
| 236 | + ->schema(), |
| 237 | + ); |
| 238 | + } |
| 239 | +} |
0 commit comments