|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Fields; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Fields\BelongsTo; |
| 6 | +use Binaryk\LaravelRestify\Fields\MorphOne; |
| 7 | +use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 8 | +use Binaryk\LaravelRestify\Repositories\Repository; |
| 9 | +use Binaryk\LaravelRestify\Restify; |
| 10 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; |
| 11 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\User; |
| 12 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; |
| 13 | +use Binaryk\LaravelRestify\Tests\IntegrationTest; |
| 14 | + |
| 15 | +class MorphOneFieldTest extends IntegrationTest |
| 16 | +{ |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + $this->authenticate(); |
| 21 | + |
| 22 | + Restify::repositories([ |
| 23 | + PostWithMophOneRepository::class, |
| 24 | + ]); |
| 25 | + } |
| 26 | + |
| 27 | + protected function tearDown(): void |
| 28 | + { |
| 29 | + parent::tearDown(); |
| 30 | + Repository::clearResolvedInstances(); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_morph_one_present_on_show_when_specified_related() |
| 34 | + { |
| 35 | + $post = factory(Post::class)->create([ |
| 36 | + 'user_id' => factory(User::class), |
| 37 | + ]); |
| 38 | + |
| 39 | + $relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id?related=user") |
| 40 | + ->assertJsonStructure([ |
| 41 | + 'data' => [ |
| 42 | + 'relationships' => [ |
| 43 | + 'user' => [ |
| 44 | + 'id', |
| 45 | + 'type', |
| 46 | + 'attributes', |
| 47 | + ], |
| 48 | + ], |
| 49 | + ], |
| 50 | + ]) |
| 51 | + ->json('data.relationships'); |
| 52 | + |
| 53 | + $this->assertNotNull($relationships); |
| 54 | + |
| 55 | + $relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id") |
| 56 | + ->json('data.relationships'); |
| 57 | + |
| 58 | + $this->assertNull($relationships); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class PostWithMophOneRepository extends Repository |
| 63 | +{ |
| 64 | + public static $model = Post::class; |
| 65 | + |
| 66 | + public static function getRelated() |
| 67 | + { |
| 68 | + return [ |
| 69 | + 'user' => BelongsTo::make('user', 'user', UserRepository::class), |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + public function fields(RestifyRequest $request) |
| 74 | + { |
| 75 | + return [ |
| 76 | + field('title'), |
| 77 | + |
| 78 | + MorphOne::make('user', 'user', UserRepository::class), |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments