|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Controllers; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; |
| 6 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostPolicy; |
| 7 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\User; |
| 8 | +use Binaryk\LaravelRestify\Tests\IntegrationTest; |
| 9 | +use Illuminate\Support\Facades\Gate; |
| 10 | + |
| 11 | +class RelatedIndexControllerTest extends IntegrationTest |
| 12 | +{ |
| 13 | + public function test_can_list_posts_belongs_to_a_user() |
| 14 | + { |
| 15 | + $this->mockUsers(); |
| 16 | + $this->mockPosts(1, 10); |
| 17 | + |
| 18 | + $this->mockPosts( |
| 19 | + factory(User::class)->create()->id |
| 20 | + ); |
| 21 | + |
| 22 | + $response = $this->getJson('restify-api/posts?viaRepository=users&viaRepositoryId=1&viaRelationship=posts') |
| 23 | + ->assertStatus(200); |
| 24 | + |
| 25 | + $this->assertCount(10, $response->json('data')); |
| 26 | + } |
| 27 | + |
| 28 | + public function test_can_show_post_belongs_to_a_user() |
| 29 | + { |
| 30 | + factory(User::class)->create(); |
| 31 | + factory(User::class)->create(); |
| 32 | + |
| 33 | + factory(Post::class)->create([ |
| 34 | + 'user_id' => 2, |
| 35 | + 'title' => 'First Post', |
| 36 | + ]); |
| 37 | + |
| 38 | + factory(Post::class)->create([ |
| 39 | + 'user_id' => 1, |
| 40 | + 'title' => 'Second Post', |
| 41 | + ]); |
| 42 | + |
| 43 | + $this->getJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts') |
| 44 | + ->assertStatus(404); |
| 45 | + |
| 46 | + $count = $this->getJson('restify-api/posts/2?viaRepository=users&viaRepositoryId=1&viaRelationship=posts') |
| 47 | + ->assertStatus(200); |
| 48 | + |
| 49 | + $this->assertCount(1, $count->json()); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_can_store_post_belongs_to_a_user() |
| 53 | + { |
| 54 | + factory(User::class)->create(); |
| 55 | + |
| 56 | + factory(User::class)->create(); |
| 57 | + |
| 58 | + $this->postJson('restify-api/posts?viaRepository=users&viaRepositoryId=1&viaRelationship=posts', [ |
| 59 | + 'title' => 'Created for the user 1', |
| 60 | + ]) |
| 61 | + ->assertStatus(201); |
| 62 | + |
| 63 | + $belongsFirst = $this->getJson('restify-api/posts?viaRepository=users&viaRepositoryId=1&viaRelationship=posts') |
| 64 | + ->assertStatus(200); |
| 65 | + |
| 66 | + $belongsSecond = $this->getJson('restify-api/posts?viaRepository=users&viaRepositoryId=2&viaRelationship=posts') |
| 67 | + ->assertStatus(200); |
| 68 | + |
| 69 | + $this->assertCount(1, $belongsFirst->json('data')); |
| 70 | + $this->assertCount(0, $belongsSecond->json('data')); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_can_update_post_belongs_to_a_user() |
| 74 | + { |
| 75 | + factory(User::class)->create(); |
| 76 | + factory(User::class)->create(); |
| 77 | + |
| 78 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); |
| 79 | + |
| 80 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); |
| 81 | + |
| 82 | + $response = $this->putJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts', [ |
| 83 | + 'title' => 'Post updated title', |
| 84 | + ])->assertStatus(200); |
| 85 | + |
| 86 | + $this->putJson('restify-api/posts/2?viaRepository=users&viaRepositoryId=1&viaRelationship=posts', [ |
| 87 | + 'title' => 'Post updated title', |
| 88 | + ])->assertStatus(404); |
| 89 | + |
| 90 | + $this->assertEquals('Post updated title', $response->json('data.attributes.title')); |
| 91 | + } |
| 92 | + |
| 93 | + public function test_can_destroy_post_belongs_to_a_user() |
| 94 | + { |
| 95 | + factory(User::class)->create(); |
| 96 | + factory(User::class)->create(); |
| 97 | + |
| 98 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); |
| 99 | + |
| 100 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); |
| 101 | + |
| 102 | + $this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(204); |
| 103 | + |
| 104 | + $this->deleteJson('restify-api/posts/2?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(404); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_policy_check_before_destroy_post_belongs_to_a_user() |
| 108 | + { |
| 109 | + $_SERVER['restify.post.deletable'] = false; |
| 110 | + |
| 111 | + Gate::policy(Post::class, PostPolicy::class); |
| 112 | + |
| 113 | + factory(User::class)->create(); |
| 114 | + |
| 115 | + factory(User::class)->create(); |
| 116 | + |
| 117 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); |
| 118 | + |
| 119 | + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); |
| 120 | + |
| 121 | + $this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(403); |
| 122 | + |
| 123 | + $this->deleteJson('restify-api/posts/2?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(404); |
| 124 | + } |
| 125 | +} |
0 commit comments