|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Unit; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Fields\Field; |
| 6 | +use Binaryk\LaravelRestify\Fields\HasOne; |
| 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\IntegrationTest; |
| 13 | + |
| 14 | +class HasOneFieldTest extends IntegrationTest |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + Restify::repositories([ |
| 20 | + UserWithPostRepository::class, |
| 21 | + ]); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_has_one_will_be_returned_in_relations() |
| 25 | + { |
| 26 | + $user = factory(User::class)->create(); |
| 27 | + factory(Post::class)->create([ |
| 28 | + 'user_id' => $user->id, |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->getJson('/restify-api/'.UserWithPostRepository::uriKey()) |
| 32 | + ->assertJsonStructure([ |
| 33 | + 'data' => [ |
| 34 | + [ |
| 35 | + 'attributes' => [ |
| 36 | + 'name', |
| 37 | + 'post', |
| 38 | + ], |
| 39 | + ], |
| 40 | + ], |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_has_one_field_is_saved_when_storing() |
| 45 | + { |
| 46 | + factory(Post::class)->create([ |
| 47 | + 'user_id' => factory(User::class), |
| 48 | + ]); |
| 49 | + |
| 50 | + $this->postJson('/restify-api/'.UserWithPostRepository::uriKey(), [ |
| 51 | + 'name' => 'Eduard Lupacescu', |
| 52 | + |
| 53 | + 'password' => 'secret!', |
| 54 | + 'post' => [ |
| 55 | + 'title' => 'New Post with user', |
| 56 | + 'description' => 'New Post description', |
| 57 | + ], |
| 58 | + ]) |
| 59 | + ->dump() |
| 60 | + ->assertCreated(); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class UserWithPostRepository extends Repository |
| 65 | +{ |
| 66 | + public static $model = User::class; |
| 67 | + |
| 68 | + public function fields(RestifyRequest $request) |
| 69 | + { |
| 70 | + return [ |
| 71 | + Field::new('name'), |
| 72 | + Field::new('email'), |
| 73 | + Field::new('password'), |
| 74 | + |
| 75 | + HasOne::make('post', 'post', PostRepository::class), |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public static function uriKey() |
| 80 | + { |
| 81 | + return 'user-with-post-repository'; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class PostRepository extends Repository |
| 86 | +{ |
| 87 | + public static $model = Post::class; |
| 88 | + |
| 89 | + public function fields(RestifyRequest $request) |
| 90 | + { |
| 91 | + return [ |
| 92 | + Field::new('title')->storingRules('required')->messages([ |
| 93 | + 'required' => 'This field is required', |
| 94 | + ]), |
| 95 | + |
| 96 | + Field::new('description')->storingRules('required'), |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments