|
8 | 8 | CatFactory::new()->createOne(); |
9 | 9 | }); |
10 | 10 |
|
11 | | -it('can use a custom user-defined state', function () { |
| 11 | +it('can use a custom state', function () { |
12 | 12 | /** @var TestCase $this */ |
13 | 13 | $cat = Cat::firstOrFail(); |
14 | 14 |
|
|
23 | 23 | ]); |
24 | 24 | }); |
25 | 25 |
|
| 26 | +it('can use a custom state with a magic method', function () { |
| 27 | + $cat = Cat::firstOrFail(); |
| 28 | + |
| 29 | + $resource = CatResource::custom()->make($cat)->toJson(); |
| 30 | + |
| 31 | + expect($resource)->toBeJson(); |
| 32 | + |
| 33 | + expect($resource)->json()->toEqual([ |
| 34 | + 'id' => $cat->id, |
| 35 | + 'name' => $cat->name, |
| 36 | + 'custom_field' => 'custom_value', |
| 37 | + ]); |
| 38 | +}); |
| 39 | + |
| 40 | +it('can use a snake_cased state as camelCase when using the magic method', function () { |
| 41 | + $cat = Cat::firstOrFail(); |
| 42 | + |
| 43 | + $resource = CatResource::snakeCustom()->make($cat)->toJson(); |
| 44 | + |
| 45 | + expect($resource)->toBeJson(); |
| 46 | + |
| 47 | + expect($resource)->json()->toEqual([ |
| 48 | + 'id' => $cat->id, |
| 49 | + 'name' => $cat->name, |
| 50 | + 'snake_custom_field' => 'snake_custom_value', |
| 51 | + ]); |
| 52 | +}); |
| 53 | + |
| 54 | +it('can use a kebab-cased state as camelCase when using the magic method', function () { |
| 55 | + $cat = Cat::firstOrFail(); |
| 56 | + |
| 57 | + $resource = CatResource::kebabCustom()->make($cat)->toJson(); |
| 58 | + |
| 59 | + expect($resource)->toBeJson(); |
| 60 | + |
| 61 | + expect($resource)->json()->toEqual([ |
| 62 | + 'id' => $cat->id, |
| 63 | + 'name' => $cat->name, |
| 64 | + 'kebab_custom_field' => 'kebab_custom_value', |
| 65 | + ]); |
| 66 | +}); |
| 67 | + |
26 | 68 | it('cannot use an unregistered state', function () { |
27 | 69 | /** @var TestCase $this */ |
28 | 70 | $cat = Cat::firstOrFail(); |
|
0 commit comments