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
+ }
0 commit comments