3030
3131class MutateUpdateMorphOperationsTest extends TestCase
3232{
33+ public function test_updating_a_resource_using_field_not_following_custom_rules (): void
34+ {
35+ $ modelToUpdate = ModelFactory::new ()->createOne ();
36+
37+ Gate::policy (Model::class, GreenPolicy::class);
38+
39+ $ response = $ this ->post (
40+ '/api/models/mutate ' ,
41+ [
42+ 'mutate ' => [
43+ [
44+ 'operation ' => 'update ' ,
45+ 'key ' => $ modelToUpdate ->getKey (),
46+ 'attributes ' => ['string ' => true ]
47+ ],
48+ ],
49+ ],
50+ ['Accept ' => 'application/json ' ]
51+ );
52+
53+ $ response ->assertStatus (422 );
54+ $ response ->assertJsonStructure (['message ' , 'errors ' => ['mutate.0 ' ]]);
55+ }
56+
3357 public function test_updating_a_resource_with_creating_morph_to_relation (): void
3458 {
3559 $ modelToUpdate = ModelFactory::new ()->createOne ();
@@ -1045,9 +1069,9 @@ public function test_updating_a_resource_with_creating_morphed_by_many_relation(
10451069 );
10461070 }
10471071
1048- public function test_creating_a_resource_with_creating_morphed_by_many_relation_with_unauthorized_pivot_fields (): void
1072+ public function test_updating_a_resource_with_creating_morphed_by_many_relation_with_unauthorized_pivot_fields (): void
10491073 {
1050- $ modelToCreate = ModelFactory::new ()->makeOne ();
1074+ $ modelToUpdate = ModelFactory::new ()->createOne ();
10511075
10521076 Gate::policy (Model::class, GreenPolicy::class);
10531077 Gate::policy (MorphedByManyRelation::class, GreenPolicy::class);
@@ -1057,10 +1081,11 @@ public function test_creating_a_resource_with_creating_morphed_by_many_relation_
10571081 [
10581082 'mutate ' => [
10591083 [
1060- 'operation ' => 'create ' ,
1084+ 'operation ' => 'update ' ,
1085+ 'key ' => $ modelToUpdate ->getKey (),
10611086 'attributes ' => [
1062- 'name ' => $ modelToCreate -> name ,
1063- 'number ' => $ modelToCreate -> number
1087+ 'name ' => ' new name' ,
1088+ 'number ' => 5001
10641089 ],
10651090 'relations ' => [
10661091 'morphedByManyRelation ' => [
@@ -1083,9 +1108,9 @@ public function test_creating_a_resource_with_creating_morphed_by_many_relation_
10831108 $ response ->assertJsonStructure (['message ' , 'errors ' => ['mutate.0.relations.morphedByManyRelation.0.pivot ' ]]);
10841109 }
10851110
1086- public function test_creating_a_resource_with_creating_morphed_by_many_relation_with_pivot_fields (): void
1111+ public function test_updating_a_resource_with_creating_morphed_by_many_relation_with_pivot_fields (): void
10871112 {
1088- $ modelToCreate = ModelFactory::new ()->makeOne ();
1113+ $ modelToUpdate = ModelFactory::new ()->createOne ();
10891114
10901115 Gate::policy (Model::class, GreenPolicy::class);
10911116 Gate::policy (MorphedByManyRelation::class, GreenPolicy::class);
@@ -1095,10 +1120,11 @@ public function test_creating_a_resource_with_creating_morphed_by_many_relation_
10951120 [
10961121 'mutate ' => [
10971122 [
1098- 'operation ' => 'create ' ,
1123+ 'operation ' => 'update ' ,
1124+ 'key ' => $ modelToUpdate ->getKey (),
10991125 'attributes ' => [
1100- 'name ' => $ modelToCreate -> name ,
1101- 'number ' => $ modelToCreate -> number
1126+ 'name ' => ' new name' ,
1127+ 'number ' => 5001
11021128 ],
11031129 'relations ' => [
11041130 'morphedByManyRelation ' => [
@@ -1126,18 +1152,19 @@ public function test_creating_a_resource_with_creating_morphed_by_many_relation_
11261152
11271153 $ this ->assertMutatedResponse (
11281154 $ response ,
1129- [$ modelToCreate ],
1155+ [],
1156+ [$ modelToUpdate ]
11301157 );
11311158
11321159 // Here we test that the relation is correctly linked
11331160 $ this ->assertEquals (
1134- Model::find ($ response ->json ('created .0 ' ))->morphedByManyRelation ()->count (), 2
1161+ Model::find ($ response ->json ('updated .0 ' ))->morphedByManyRelation ()->count (), 2
11351162 );
11361163 $ this ->assertEquals (
1137- Model::find ($ response ->json ('created .0 ' ))->morphedByManyRelation [0 ]->morphed_by_many_pivot ->number , 20
1164+ Model::find ($ response ->json ('updated .0 ' ))->morphedByManyRelation [0 ]->morphed_by_many_pivot ->number , 20
11381165 );
11391166 $ this ->assertEquals (
1140- Model::find ($ response ->json ('created .0 ' ))->morphedByManyRelation [1 ]->morphed_by_many_pivot ->number , 30
1167+ Model::find ($ response ->json ('updated .0 ' ))->morphedByManyRelation [1 ]->morphed_by_many_pivot ->number , 30
11411168 );
11421169 }
11431170
0 commit comments