|
| 1 | +<?php |
| 2 | + |
| 3 | +use Farbcode\StatefulResources\Enums\Variant; |
| 4 | +use Farbcode\StatefulResources\Tests\TestCase; |
| 5 | +use Workbench\App\Http\Resources\CatResource; |
| 6 | +use Workbench\App\Models\Cat; |
| 7 | +use Workbench\Database\Factories\CatFactory; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + CatFactory::new()->createOne(); |
| 11 | +}); |
| 12 | + |
| 13 | +it('can return a stateful resource with the default state', function () { |
| 14 | + /** @var TestCase $this */ |
| 15 | + $cat = Cat::firstOrFail(); |
| 16 | + |
| 17 | + $resource = CatResource::make($cat)->toJson(); |
| 18 | + |
| 19 | + expect($resource)->toBeJson(); |
| 20 | + |
| 21 | + expect($resource)->json()->toEqual([ |
| 22 | + 'id' => $cat->id, |
| 23 | + 'name' => $cat->name, |
| 24 | + 'breed' => $cat->breed, |
| 25 | + 'fluffyness' => $cat->fluffyness, |
| 26 | + 'color' => $cat->color, |
| 27 | + ]); |
| 28 | + |
| 29 | +}); |
| 30 | + |
| 31 | +it('can return a stateful resource with the correct "full" state', function () { |
| 32 | + /** @var TestCase $this */ |
| 33 | + $cat = Cat::firstOrFail(); |
| 34 | + |
| 35 | + $resource = CatResource::state(Variant::Full)->make($cat)->toJson(); |
| 36 | + |
| 37 | + expect($resource)->toBeJson(); |
| 38 | + |
| 39 | + expect($resource)->json()->toEqual([ |
| 40 | + 'id' => $cat->id, |
| 41 | + 'name' => $cat->name, |
| 42 | + 'breed' => $cat->breed, |
| 43 | + 'fluffyness' => $cat->fluffyness, |
| 44 | + 'color' => $cat->color, |
| 45 | + ]); |
| 46 | +}); |
| 47 | + |
| 48 | +it('can use a stateful resource with the "minimal" state', function () { |
| 49 | + /** @var TestCase $this */ |
| 50 | + $cat = Cat::firstOrFail(); |
| 51 | + |
| 52 | + $resource = CatResource::state(Variant::Minimal)->make($cat)->toJson(); |
| 53 | + |
| 54 | + expect($resource)->toBeJson(); |
| 55 | + |
| 56 | + expect($resource)->json()->toEqual([ |
| 57 | + 'id' => $cat->id, |
| 58 | + 'name' => $cat->name, |
| 59 | + ]); |
| 60 | +}); |
| 61 | + |
| 62 | +it('can use a stateful resource with the "table" state', function () { |
| 63 | + /** @var TestCase $this */ |
| 64 | + $cat = Cat::firstOrFail(); |
| 65 | + |
| 66 | + $resource = CatResource::state(Variant::Table)->make($cat)->toJson(); |
| 67 | + |
| 68 | + expect($resource)->toBeJson(); |
| 69 | + |
| 70 | + expect($resource)->json()->toEqual([ |
| 71 | + 'id' => $cat->id, |
| 72 | + 'name' => $cat->name, |
| 73 | + 'breed' => $cat->breed, |
| 74 | + ]); |
| 75 | +}); |
0 commit comments