Skip to content

Commit e6bd977

Browse files
committed
feature: HasOne endpoint tests
1 parent 52ebda7 commit e6bd977

19 files changed

+887
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/vendor
23
.phpunit.result.cache
34
phpunit.xml

tests/Feature/Relations/BelongsTo/BelongsToStandardRestoreOperationsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ public function restoring_a_single_relation_resource_when_authorized()
4545
/** @test */
4646
public function restoring_a_single_not_trashed_relation_resource()
4747
{
48-
$trashedCategory = factory(Category::class)->create();
49-
$post = factory(Post::class)->create(['category_id' => $trashedCategory->id]);
48+
$category = factory(Category::class)->create();
49+
$post = factory(Post::class)->create(['category_id' => $category->id]);
5050

5151
Gate::policy(Category::class, GreenPolicy::class);
5252

53-
$response = $this->post("/api/posts/{$post->id}/category/{$trashedCategory->id}/restore");
53+
$response = $this->post("/api/posts/{$post->id}/category/{$category->id}/restore");
5454

55-
$this->assertResourceRestored($response, $trashedCategory);
55+
$this->assertResourceRestored($response, $category);
5656
}
5757

5858
/** @test */

tests/Feature/Relations/BelongsTo/BelongsToStandardShowOperationsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Orion\Tests\Feature\Relations\BelongsTo;
44

55
use Illuminate\Support\Facades\Gate;
6-
76
use Mockery;
87
use Orion\Contracts\ComponentsResolver;
98
use Orion\Tests\Feature\TestCase;
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace Orion\Tests\Feature\Relations\HasOne;
4+
5+
use Illuminate\Support\Facades\Gate;
6+
use Mockery;
7+
use Orion\Contracts\ComponentsResolver;
8+
use Orion\Tests\Feature\TestCase;
9+
use Orion\Tests\Fixtures\App\Http\Resources\SampleResource;
10+
use Orion\Tests\Fixtures\App\Models\Post;
11+
use Orion\Tests\Fixtures\App\Models\PostImage;
12+
use Orion\Tests\Fixtures\App\Models\PostMeta;
13+
use Orion\Tests\Fixtures\App\Policies\GreenPolicy;
14+
use Orion\Tests\Fixtures\App\Policies\RedPolicy;
15+
16+
class HasOneStandardDeleteOperationsTest extends TestCase
17+
{
18+
/** @test */
19+
public function trashing_a_single_soft_deletable_relation_resource_without_authorization()
20+
{
21+
$post = factory(Post::class)->create();
22+
factory(PostImage::class)->create(['post_id' => $post->id]);
23+
24+
Gate::policy(PostImage::class, RedPolicy::class);
25+
26+
$response = $this->delete("/api/posts/{$post->id}/image");
27+
28+
$this->assertUnauthorizedResponse($response);
29+
}
30+
31+
/** @test */
32+
public function deleting_a_single_relation_resource_without_authorization()
33+
{
34+
$post = factory(Post::class)->create();
35+
factory(PostMeta::class)->create(['post_id' => $post->id]);
36+
37+
Gate::policy(PostMeta::class, RedPolicy::class);
38+
39+
$response = $this->delete("/api/posts/{$post->id}/meta");
40+
41+
$this->assertUnauthorizedResponse($response);
42+
}
43+
44+
/** @test */
45+
public function force_deleting_a_single_relation_resource_without_authorization()
46+
{
47+
$post = factory(Post::class)->create();
48+
factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
49+
50+
Gate::policy(PostImage::class, RedPolicy::class);
51+
52+
$response = $this->delete("/api/posts/{$post->id}/image?force=true");
53+
54+
$this->assertUnauthorizedResponse($response);
55+
}
56+
57+
/** @test */
58+
public function trashing_a_single_soft_deletable_relation_resource_when_authorized()
59+
{
60+
$post = factory(Post::class)->create();
61+
$postImage = factory(PostImage::class)->create(['post_id' => $post->id]);
62+
63+
Gate::policy(PostImage::class, GreenPolicy::class);
64+
65+
$response = $this->delete("/api/posts/{$post->id}/image");
66+
67+
$this->assertResourceTrashed($response, $postImage);
68+
}
69+
70+
/** @test */
71+
public function deleting_a_single_relation_resource_when_authorized()
72+
{
73+
$post = factory(Post::class)->create();
74+
$postMeta = factory(PostMeta::class)->create(['post_id' => $post->id])->fresh();
75+
76+
Gate::policy(PostMeta::class, GreenPolicy::class);
77+
78+
$response = $this->delete("/api/posts/{$post->id}/meta");
79+
80+
$this->assertResourceDeleted($response, $postMeta);
81+
}
82+
83+
/** @test */
84+
public function force_deleting_a_single_trashed_relation_resource_when_authorized()
85+
{
86+
$post = factory(Post::class)->create();
87+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id])->fresh();
88+
89+
Gate::policy(PostImage::class, GreenPolicy::class);
90+
91+
$response = $this->delete("/api/posts/{$post->id}/image?force=true");
92+
93+
$this->assertResourceDeleted($response, $trashedPostImage);
94+
}
95+
96+
/** @test */
97+
public function deleting_a_single_trashed_relation_resource_without_trashed_query_parameter()
98+
{
99+
$post = factory(Post::class)->create();
100+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
101+
102+
Gate::policy(PostImage::class, GreenPolicy::class);
103+
104+
$response = $this->delete("/api/posts/{$post->id}/image");
105+
106+
$response->assertNotFound();
107+
$response->assertJsonStructure(['message']);
108+
$this->assertDatabaseHas('post_images', $trashedPostImage->getAttributes());
109+
}
110+
111+
/** @test */
112+
public function deleting_a_single_trashed_relation_resource_with_trashed_query_parameter()
113+
{
114+
$post = factory(Post::class)->create();
115+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
116+
117+
Gate::policy(PostImage::class, GreenPolicy::class);
118+
119+
$response = $this->delete("/api/posts/{$post->id}/image?with_trashed=true");
120+
121+
$response->assertNotFound();
122+
$response->assertJsonStructure(['message']);
123+
$this->assertDatabaseHas('post_images', $trashedPostImage->getAttributes());
124+
}
125+
126+
/** @test */
127+
public function deleting_a_single_trashed_relation_resource_with_force_query_parameter()
128+
{
129+
$post = factory(Post::class)->create();
130+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id])->fresh();
131+
132+
Gate::policy(PostImage::class, GreenPolicy::class);
133+
134+
$response = $this->delete("/api/posts/{$post->id}/image?force=true");
135+
136+
$this->assertResourceDeleted($response, $trashedPostImage);
137+
}
138+
139+
/** @test */
140+
public function transforming_a_single_deleted_relation_resource()
141+
{
142+
$post = factory(Post::class)->create();
143+
$postMeta = factory(PostMeta::class)->create(['post_id' => $post->id])->fresh();
144+
145+
app()->bind(ComponentsResolver::class, function () {
146+
$componentsResolverMock = Mockery::mock(\Orion\Drivers\Standard\ComponentsResolver::class)->makePartial();
147+
$componentsResolverMock->shouldReceive('resolveResourceClass')->once()->andReturn(SampleResource::class);
148+
149+
return $componentsResolverMock;
150+
});
151+
152+
Gate::policy(PostMeta::class, GreenPolicy::class);
153+
154+
$response = $this->delete("/api/posts/{$post->id}/meta");
155+
156+
$this->assertResourceDeleted($response, $postMeta, ['test-field-from-resource' => 'test-value']);
157+
}
158+
159+
/** @test */
160+
public function deleting_a_single_relation_resource_and_getting_included_relation()
161+
{
162+
$post = factory(Post::class)->create()->fresh();
163+
$postMeta = factory(PostMeta::class)->create(['post_id' => $post->id])->fresh();
164+
165+
Gate::policy(PostMeta::class, GreenPolicy::class);
166+
167+
$response = $this->delete("/api/posts/{$post->id}/meta?include=post");
168+
169+
$this->assertResourceDeleted($response, $postMeta, ['post' => $post->toArray()]);
170+
}
171+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Orion\Tests\Feature\Relations\HasOne;
4+
5+
use Illuminate\Support\Facades\Gate;
6+
use Mockery;
7+
use Orion\Contracts\ComponentsResolver;
8+
use Orion\Tests\Feature\TestCase;
9+
use Orion\Tests\Fixtures\App\Http\Resources\SampleResource;
10+
use Orion\Tests\Fixtures\App\Models\Post;
11+
use Orion\Tests\Fixtures\App\Models\PostImage;
12+
use Orion\Tests\Fixtures\App\Models\PostMeta;
13+
use Orion\Tests\Fixtures\App\Policies\GreenPolicy;
14+
use Orion\Tests\Fixtures\App\Policies\RedPolicy;
15+
16+
class HasOneStandardRestoreOperationsTest extends TestCase
17+
{
18+
/** @test */
19+
public function restoring_a_single_relation_resource_without_authorization()
20+
{
21+
$post = factory(Post::class)->create();
22+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
23+
24+
Gate::policy(PostImage::class, RedPolicy::class);
25+
26+
//TODO: make possible to omit child relation key?
27+
$response = $this->post("/api/posts/{$post->id}/image/{$trashedPostImage->id}/restore");
28+
29+
$this->assertUnauthorizedResponse($response);
30+
}
31+
32+
/** @test */
33+
public function restoring_a_single_relation_resource_when_authorized()
34+
{
35+
$post = factory(Post::class)->create();
36+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
37+
38+
Gate::policy(PostImage::class, GreenPolicy::class);
39+
40+
$response = $this->post("/api/posts/{$post->id}/image/{$trashedPostImage->id}/restore");
41+
42+
$this->assertResourceRestored($response, $trashedPostImage);
43+
}
44+
45+
/** @test */
46+
public function restoring_a_single_not_trashed_relation_resource()
47+
{
48+
$post = factory(Post::class)->create();
49+
$postImage = factory(PostImage::class)->create(['post_id' => $post->id]);
50+
51+
Gate::policy(PostImage::class, GreenPolicy::class);
52+
53+
$response = $this->post("/api/posts/{$post->id}/image/{$postImage->id}/restore");
54+
55+
$this->assertResourceRestored($response, $postImage);
56+
}
57+
58+
/** @test */
59+
public function restoring_a_single_relation_resource_that_is_not_marked_as_soft_deletable()
60+
{
61+
$post = factory(Post::class)->create();
62+
$postMeta = factory(PostMeta::class)->create(['post_id' => $post->id])->fresh();
63+
64+
Gate::policy(PostMeta::class, GreenPolicy::class);
65+
66+
$response = $this->post("/api/posts/{$post->id}/meta/{$postMeta->id}/restore");
67+
68+
$response->assertNotFound();
69+
}
70+
71+
/** @test */
72+
public function transforming_a_single_restored_relation_resource()
73+
{
74+
$post = factory(Post::class)->create();
75+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
76+
77+
Gate::policy(PostImage::class, GreenPolicy::class);
78+
79+
app()->bind(ComponentsResolver::class, function () {
80+
$componentsResolverMock = Mockery::mock(\Orion\Drivers\Standard\ComponentsResolver::class)->makePartial();
81+
$componentsResolverMock->shouldReceive('resolveResourceClass')->once()->andReturn(SampleResource::class);
82+
83+
return $componentsResolverMock;
84+
});
85+
86+
$response = $this->post("/api/posts/{$post->id}/image/{$trashedPostImage->id}/restore");
87+
88+
$this->assertResourceRestored($response, $trashedPostImage, ['test-field-from-resource' => 'test-value']);
89+
}
90+
91+
/** @test */
92+
public function restoring_a_single_relation_resource_and_getting_included_relation()
93+
{
94+
$post = factory(Post::class)->create();
95+
$trashedPostImage = factory(PostImage::class)->state('trashed')->create(['post_id' => $post->id]);
96+
97+
Gate::policy(PostImage::class, GreenPolicy::class);
98+
99+
$response = $this->post("/api/posts/{$post->id}/image/{$trashedPostImage->id}/restore?include=post");
100+
101+
$this->assertResourceRestored($response, $trashedPostImage, ['post' => $trashedPostImage->post->toArray()]);
102+
}
103+
}

0 commit comments

Comments
 (0)