From d73bcd376d977364aab03ebb956d996de7a06675 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 25 Apr 2023 00:32:44 +0200 Subject: [PATCH] Fixed: Delete courses with members --- app/Member/Member.php | 1 + tests/Feature/Member/DeleteTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/app/Member/Member.php b/app/Member/Member.php index 212d23e9..d971cccd 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -285,6 +285,7 @@ class Member extends Model static::deleting(function (self $model): void { $model->payments->each->delete(); $model->memberships->each->delete(); + $model->courses->each->delete(); }); static::saving(fn ($model) => $model->updateSearch()); diff --git a/tests/Feature/Member/DeleteTest.php b/tests/Feature/Member/DeleteTest.php index 3798d112..df8e41ce 100644 --- a/tests/Feature/Member/DeleteTest.php +++ b/tests/Feature/Member/DeleteTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Member; +use App\Course\Models\Course; +use App\Course\Models\CourseMember; use App\Member\DeleteJob; use App\Member\Member; use Carbon\Carbon; @@ -56,4 +58,14 @@ class DeleteTest extends TestCase app(MemberFake::class)->assertDeleted(123, Carbon::parse('yesterday')); } + + public function testItDeletesMembersWithCourses(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + $member = Member::factory()->defaults()->has(CourseMember::factory()->for(Course::factory()), 'courses')->create(); + + $member->delete(); + + $this->assertDatabaseCount('members', 0); + } }