Skip to content

Commit abb58f3

Browse files
authored
Merge pull request #69 from Lomkit/fix/relation-rules-triggered-on-attach-detach
Fix/relation rules triggered on attach detach
2 parents 11a07a5 + 8c37a10 commit abb58f3

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

src/Rules/CustomRulable.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
5858
$rules = $this->resource->createRules(
5959
app()->make(RestRequest::class)
6060
);
61-
} else {
61+
} elseif ($value['operation'] === 'update') {
6262
$rules = $this->resource->updateRules(
6363
app()->make(RestRequest::class)
6464
);
65+
} else {
66+
// No rules needed
67+
return;
6568
}
6669

6770
$rules = array_merge_recursive(

tests/Feature/Controllers/MutateUpdateOperationsTest.php

+108
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,60 @@ public function test_updating_a_resource_with_attaching_has_many_relation(): voi
594594
);
595595
}
596596

597+
public function test_updating_a_resource_with_attaching_has_many_relation_with_required_rules(): void
598+
{
599+
$modelToUpdate = ModelFactory::new()->createOne();
600+
$hasManyRelationToAttach = HasManyRelationFactory::new()
601+
->createOne();
602+
603+
Gate::policy(Model::class, GreenPolicy::class);
604+
Gate::policy(HasManyRelation::class, GreenPolicy::class);
605+
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
606+
607+
$response = $this->post(
608+
'/api/constrained/mutate',
609+
[
610+
'mutate' => [
611+
[
612+
'operation' => 'update',
613+
'key' => $modelToUpdate->getKey(),
614+
'attributes' => [
615+
'name' => 'new name',
616+
'number' => 5001,
617+
],
618+
'relations' => [
619+
'belongsToManyRelation' => [
620+
[
621+
'operation' => 'create',
622+
'attributes' => [],
623+
],
624+
],
625+
'hasManyRelation' => [
626+
[
627+
'operation' => 'attach',
628+
'key' => $hasManyRelationToAttach->getKey(),
629+
],
630+
],
631+
],
632+
],
633+
],
634+
],
635+
['Accept' => 'application/json']
636+
);
637+
638+
$this->assertMutatedResponse(
639+
$response,
640+
[],
641+
[$modelToUpdate],
642+
);
643+
644+
// Here we test that the relation is correctly linked
645+
$this->assertEquals(
646+
Model::find($response->json('updated.0'))->hasManyRelation()->count(),
647+
1
648+
);
649+
}
650+
597651
public function test_updating_a_resource_with_detaching_has_many_relation(): void
598652
{
599653
$modelToUpdate = ModelFactory::new()->createOne();
@@ -1191,6 +1245,60 @@ public function test_updating_a_resource_with_creating_belongs_to_many_relation_
11911245
);
11921246
}
11931247

1248+
public function test_updating_a_resource_with_updating_belongs_to_many_relation_pivot_fields(): void
1249+
{
1250+
$modelToUpdate = ModelFactory::new()->createOne();
1251+
1252+
$belongsToManyToUpdate = BelongsToManyRelationFactory::new()->createOne();
1253+
1254+
Gate::policy(Model::class, GreenPolicy::class);
1255+
Gate::policy(BelongsToManyRelation::class, GreenPolicy::class);
1256+
1257+
$response = $this->post(
1258+
'/api/models/mutate',
1259+
[
1260+
'mutate' => [
1261+
[
1262+
'operation' => 'update',
1263+
'key' => $modelToUpdate->getKey(),
1264+
'attributes' => [
1265+
'name' => 'new name',
1266+
'number' => 5001,
1267+
],
1268+
'relations' => [
1269+
'belongsToManyRelation' => [
1270+
[
1271+
'operation' => 'update',
1272+
'key' => $belongsToManyToUpdate->getKey(),
1273+
'pivot' => [
1274+
'number' => 20,
1275+
],
1276+
],
1277+
],
1278+
],
1279+
],
1280+
],
1281+
],
1282+
['Accept' => 'application/json']
1283+
);
1284+
1285+
$this->assertMutatedResponse(
1286+
$response,
1287+
[],
1288+
[$modelToUpdate]
1289+
);
1290+
1291+
// Here we test that the relation is correctly linked
1292+
$this->assertEquals(
1293+
Model::find($response->json('updated.0'))->belongsToManyRelation()->count(),
1294+
1
1295+
);
1296+
$this->assertEquals(
1297+
Model::find($response->json('updated.0'))->belongsToManyRelation[0]->belongs_to_many_pivot->number,
1298+
20
1299+
);
1300+
}
1301+
11941302
public function test_updating_a_resource_with_creating_multiple_belongs_to_many_relation(): void
11951303
{
11961304
$modelToUpdate = ModelFactory::new()->createOne();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Support\Rest\Resources;
4+
5+
use Lomkit\Rest\Concerns\Resource\DisableAutomaticGates;
6+
use Lomkit\Rest\Http\Requests\RestRequest;
7+
use Lomkit\Rest\Http\Resource;
8+
use Lomkit\Rest\Tests\Support\Models\HasManyRelation;
9+
10+
class ConstrainedHasManyResource extends Resource
11+
{
12+
use DisableAutomaticGates;
13+
public static $model = HasManyRelation::class;
14+
15+
public function rules(RestRequest $request)
16+
{
17+
return [
18+
'number' => 'required',
19+
];
20+
}
21+
22+
public function relations(RestRequest $request): array
23+
{
24+
return [];
25+
}
26+
27+
public function fields(RestRequest $request): array
28+
{
29+
return [
30+
'id',
31+
'number',
32+
];
33+
}
34+
}

tests/Support/Rest/Resources/ConstrainedResource.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Lomkit\Rest\Http\Resource;
77
use Lomkit\Rest\Relations\BelongsTo;
88
use Lomkit\Rest\Relations\BelongsToMany;
9+
use Lomkit\Rest\Relations\HasMany;
910
use Lomkit\Rest\Tests\Support\Models\Model;
1011

1112
class ConstrainedResource extends Resource
@@ -21,6 +22,7 @@ public function relations(RestRequest $request): array
2122
BelongsTo::make('belongsToRelation', BelongsToResource::class)
2223
->prohibitedOnCreation()
2324
->prohibitedOnUpdate(),
25+
HasMany::make('hasManyRelation', ConstrainedHasManyResource::class),
2426
];
2527
}
2628

0 commit comments

Comments
 (0)