Skip to content

Commit f2a8f55

Browse files
authored
Support empty mutate keys (#204)
1 parent 605cd4a commit f2a8f55

File tree

5 files changed

+435
-5
lines changed

5 files changed

+435
-5
lines changed

src/Rules/Mutate/Mutate.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public function buildValidationRules(string $attribute, mixed $value): array
4141
new ArrayWithKey($this->resource->getFields($request)),
4242
],
4343
$attributeConsideringRelationType.'.key' => [
44-
'required_if:'.$attributeConsideringRelationType.'.operation,update',
45-
'required_if:'.$attributeConsideringRelationType.'.operation,attach',
46-
'required_if:'.$attributeConsideringRelationType.'.operation,detach',
47-
'required_if:'.$attributeConsideringRelationType.'.operation,toggle',
48-
'required_if:'.$attributeConsideringRelationType.'.operation,sync',
44+
'present_if:'.$attributeConsideringRelationType.'.operation,update',
45+
'present_if:'.$attributeConsideringRelationType.'.operation,attach',
46+
'present_if:'.$attributeConsideringRelationType.'.operation,detach',
47+
'present_if:'.$attributeConsideringRelationType.'.operation,toggle',
48+
'present_if:'.$attributeConsideringRelationType.'.operation,sync',
4949
'prohibited_if:'.$attributeConsideringRelationType.'.operation,create',
5050
'exists:'.$this->resource::newModel()->getTable().','.$this->resource::newModel()->getKeyName(),
5151
],

tests/Feature/Controllers/MutateCreateMorphOperationsTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,49 @@ public function test_creating_a_resource_with_attaching_multiple_morph_many_rela
351351
);
352352
}
353353

354+
public function test_creating_a_resource_with_attaching_empty_morph_many_relation(): void
355+
{
356+
$modelToCreate = ModelFactory::new()->makeOne();
357+
358+
Gate::policy(Model::class, GreenPolicy::class);
359+
Gate::policy(MorphManyRelation::class, GreenPolicy::class);
360+
361+
$response = $this->post(
362+
'/api/models/mutate',
363+
[
364+
'mutate' => [
365+
[
366+
'operation' => 'create',
367+
'attributes' => [
368+
'name' => $modelToCreate->name,
369+
'number' => $modelToCreate->number,
370+
],
371+
'relations' => [
372+
'morphManyRelation' => [
373+
[
374+
'operation' => 'attach',
375+
'key' => [],
376+
],
377+
],
378+
],
379+
],
380+
],
381+
],
382+
['Accept' => 'application/json']
383+
);
384+
385+
$this->assertMutatedResponse(
386+
$response,
387+
[$modelToCreate],
388+
);
389+
390+
// Here we test that the relation is correctly linked
391+
$this->assertEquals(
392+
Model::find($response->json('created.0'))->morphManyRelation()->count(),
393+
0
394+
);
395+
}
396+
354397
public function test_creating_a_resource_with_updating_morph_many_relation(): void
355398
{
356399
$modelToCreate = ModelFactory::new()->makeOne();

tests/Feature/Controllers/MutateCreateOperationsTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,49 @@ public function test_creating_a_resource_with_attaching_multiple_has_many_relati
675675
);
676676
}
677677

678+
public function test_creating_a_resource_with_attaching_empty_has_many_relation(): void
679+
{
680+
$modelToCreate = ModelFactory::new()->makeOne();
681+
682+
Gate::policy(Model::class, GreenPolicy::class);
683+
Gate::policy(HasManyRelation::class, GreenPolicy::class);
684+
685+
$response = $this->post(
686+
'/api/models/mutate',
687+
[
688+
'mutate' => [
689+
[
690+
'operation' => 'create',
691+
'attributes' => [
692+
'name' => $modelToCreate->name,
693+
'number' => $modelToCreate->number,
694+
],
695+
'relations' => [
696+
'hasManyRelation' => [
697+
[
698+
'operation' => 'attach',
699+
'key' => [],
700+
],
701+
],
702+
],
703+
],
704+
],
705+
],
706+
['Accept' => 'application/json']
707+
);
708+
709+
$this->assertMutatedResponse(
710+
$response,
711+
[$modelToCreate],
712+
);
713+
714+
// Here we test that the relation is correctly linked
715+
$this->assertEquals(
716+
Model::find($response->json('created.0'))->hasManyRelation()->count(),
717+
0
718+
);
719+
}
720+
678721
public function test_creating_a_resource_with_updating_has_many_relation(): void
679722
{
680723
$modelToCreate = ModelFactory::new()->makeOne();

tests/Feature/Controllers/MutateUpdateMorphOperationsTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,51 @@ public function test_updating_a_resource_with_attaching_multiple_morph_many_rela
430430
);
431431
}
432432

433+
public function test_updating_a_resource_with_attaching_empty_morph_many_relation(): void
434+
{
435+
$modelToUpdate = ModelFactory::new()->createOne();
436+
437+
Gate::policy(Model::class, GreenPolicy::class);
438+
Gate::policy(MorphManyRelation::class, GreenPolicy::class);
439+
440+
$response = $this->post(
441+
'/api/models/mutate',
442+
[
443+
'mutate' => [
444+
[
445+
'operation' => 'update',
446+
'key' => $modelToUpdate->getKey(),
447+
'attributes' => [
448+
'name' => 'new name',
449+
'number' => 5001,
450+
],
451+
'relations' => [
452+
'morphManyRelation' => [
453+
[
454+
'operation' => 'attach',
455+
'key' => [],
456+
],
457+
],
458+
],
459+
],
460+
],
461+
],
462+
['Accept' => 'application/json']
463+
);
464+
465+
$this->assertMutatedResponse(
466+
$response,
467+
[],
468+
[$modelToUpdate],
469+
);
470+
471+
// Here we test that the relation is correctly linked
472+
$this->assertEquals(
473+
Model::find($response->json('updated.0'))->morphManyRelation()->count(),
474+
0
475+
);
476+
}
477+
433478
public function test_updating_a_resource_with_detaching_morph_many_relation(): void
434479
{
435480
$modelToUpdate = ModelFactory::new()->createOne();

0 commit comments

Comments
 (0)