|
2 | 2 |
|
3 | 3 | use Code16\Sharp\EntityList\Commands\EntityCommand; |
4 | 4 | use Code16\Sharp\EntityList\Commands\Wizards\EntityWizardCommand; |
| 5 | +use Code16\Sharp\Form\Fields\SharpFormEditorField; |
5 | 6 | use Code16\Sharp\Form\Fields\SharpFormTextField; |
6 | 7 | use Code16\Sharp\Show\Fields\SharpShowEntityListField; |
7 | 8 | use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity; |
| 9 | +use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm; |
8 | 10 | use Code16\Sharp\Tests\Fixtures\Sharp\PersonList; |
9 | 11 | use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow; |
10 | 12 | use Code16\Sharp\Utils\Fields\FieldsContainer; |
@@ -202,8 +204,129 @@ public function getListData(): array |
202 | 204 | ->assertListContains(['name' => 'Marie Curie']); |
203 | 205 | }); |
204 | 206 |
|
205 | | -test('get & assert form', function () { |
| 207 | +test('nested form', function () { |
| 208 | + $this->sharpForm(PersonEntity::class)->create(); |
| 209 | + expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-form/person'); |
| 210 | + $this->sharpForm(PersonEntity::class, 1)->update([]); |
| 211 | + expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-form/person/1'); |
| 212 | +}); |
| 213 | + |
| 214 | +test('create & store form', function () { |
| 215 | + fakeFormFor('person', new class() extends PersonForm |
| 216 | + { |
| 217 | + public function buildFormFields(FieldsContainer $formFields): void |
| 218 | + { |
| 219 | + $formFields |
| 220 | + ->addField(SharpFormEditorField::make('name')) |
| 221 | + ->addField(SharpFormEditorField::make('job')); |
| 222 | + } |
| 223 | + |
| 224 | + public function find($id): array |
| 225 | + { |
| 226 | + return ['name' => 'John Wayne', 'job' => 'actor']; |
| 227 | + } |
| 228 | + |
| 229 | + public function update($id, array $data) |
| 230 | + { |
| 231 | + expect($data)->toEqual(['name' => 'John Doe', 'job' => null]); |
| 232 | + |
| 233 | + return 1; |
| 234 | + } |
| 235 | + }); |
| 236 | + |
| 237 | + $this->sharpForm(PersonEntity::class) |
| 238 | + ->create() |
| 239 | + ->assertOk() |
| 240 | + ->store(['name' => 'John Doe']) |
| 241 | + ->assertValid() |
| 242 | + ->assertRedirect(); |
| 243 | +}); |
| 244 | + |
| 245 | +test('store form', function () { |
| 246 | + fakeFormFor('person', new class() extends PersonForm |
| 247 | + { |
| 248 | + public function buildFormFields(FieldsContainer $formFields): void |
| 249 | + { |
| 250 | + $formFields |
| 251 | + ->addField(SharpFormEditorField::make('name')) |
| 252 | + ->addField(SharpFormEditorField::make('job')); |
| 253 | + } |
| 254 | + |
| 255 | + public function find($id): array |
| 256 | + { |
| 257 | + return ['name' => 'John Wayne', 'job' => 'actor']; |
| 258 | + } |
| 259 | + |
| 260 | + public function update($id, array $data) |
| 261 | + { |
| 262 | + expect($data)->toEqual(['name' => 'John Doe', 'job' => null]); |
| 263 | + |
| 264 | + return 1; |
| 265 | + } |
| 266 | + }); |
| 267 | + |
| 268 | + $this->sharpForm(PersonEntity::class) |
| 269 | + ->store(['name' => 'John Doe']) |
| 270 | + ->assertValid() |
| 271 | + ->assertRedirect(); |
| 272 | +}); |
| 273 | + |
| 274 | +test('edit & update form', function () { |
| 275 | + fakeFormFor('person', new class() extends PersonForm |
| 276 | + { |
| 277 | + public function buildFormFields(FieldsContainer $formFields): void |
| 278 | + { |
| 279 | + $formFields |
| 280 | + ->addField(SharpFormEditorField::make('name')) |
| 281 | + ->addField(SharpFormEditorField::make('job')); |
| 282 | + } |
| 283 | + |
| 284 | + public function find($id): array |
| 285 | + { |
| 286 | + return ['name' => 'John Wayne', 'job' => 'actor']; |
| 287 | + } |
| 288 | + |
| 289 | + public function update($id, array $data) |
| 290 | + { |
| 291 | + expect($data)->toEqual(['name' => 'John Doe', 'job' => 'actor']); |
| 292 | + |
| 293 | + return 1; |
| 294 | + } |
| 295 | + }); |
| 296 | + |
206 | 297 | $this->sharpForm(PersonEntity::class, 1) |
207 | | - ->get() |
208 | | - ->assertOk(); |
| 298 | + ->edit() |
| 299 | + ->assertOk() |
| 300 | + ->update(['name' => 'John Doe']) |
| 301 | + ->assertValid() |
| 302 | + ->assertRedirect(); |
| 303 | +}); |
| 304 | + |
| 305 | +test('update form', function () { |
| 306 | + fakeFormFor('person', new class() extends PersonForm |
| 307 | + { |
| 308 | + public function buildFormFields(FieldsContainer $formFields): void |
| 309 | + { |
| 310 | + $formFields |
| 311 | + ->addField(SharpFormEditorField::make('name')) |
| 312 | + ->addField(SharpFormEditorField::make('job')); |
| 313 | + } |
| 314 | + |
| 315 | + public function find($id): array |
| 316 | + { |
| 317 | + return ['name' => 'John Wayne', 'job' => 'actor']; |
| 318 | + } |
| 319 | + |
| 320 | + public function update($id, array $data) |
| 321 | + { |
| 322 | + expect($data)->toEqual(['name' => 'John Doe', 'job' => null]); |
| 323 | + |
| 324 | + return 1; |
| 325 | + } |
| 326 | + }); |
| 327 | + |
| 328 | + $this->sharpForm(PersonEntity::class, 1) |
| 329 | + ->update(['name' => 'John Doe']) |
| 330 | + ->assertValid() |
| 331 | + ->assertRedirect(); |
209 | 332 | }); |
0 commit comments