From dc971499f67b06a9b241472931a1c6d89ac3f9d0 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 13 Jun 2021 11:33:50 +0200 Subject: [PATCH] Add memberTest --- app/Initialize/InitializeMembers.php | 12 ++-- app/Member/Member.php | 19 ++++++- app/Member/UpdateJob.php | 62 +++++++++++++++++++++ database/factories/Member/MemberFactory.php | 36 ++++++++++++ tests/Feature/Member/UpdateTest.php | 38 +++++++++++++ 5 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 app/Member/UpdateJob.php create mode 100644 database/factories/Member/MemberFactory.php create mode 100644 tests/Feature/Member/UpdateTest.php diff --git a/app/Initialize/InitializeMembers.php b/app/Initialize/InitializeMembers.php index 2798cf38..ebdd82d2 100644 --- a/app/Initialize/InitializeMembers.php +++ b/app/Initialize/InitializeMembers.php @@ -9,6 +9,7 @@ use App\Member\Member; use App\Region; use App\Nationality; use App\Fee; +use App\Group; class InitializeMembers { @@ -45,11 +46,12 @@ class InitializeMembers { 'email' => $member->email, 'email_parents' => $member->email_parents, 'nami_id' => $member->id, - 'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id))->id, - 'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id))->id, - 'region_id' => Region::where('nami_id', $member->region_id)->firstOrFail()->id, - 'country_id' => Country::where('nami_id', '=', $member->country_id)->firstOrFail()->id, - 'fee_id' => optional(Fee::firstWhere('nami_id', '=', $member->fee_id))->id, + 'group_id' => Group::firstOrCreate(['nami_id' => $member->group_id], ['nami_id' => $member->group_id, 'name' => 'AAA'])->id, + 'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id ?: -1))->id, + 'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id, + 'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id, + 'country_id' => Country::where('nami_id', $member->country_id)->firstOrFail()->id, + 'fee_id' => optional(Fee::firstWhere('nami_id', $member->fee_id ?: -1))->id, 'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id, ]); }); diff --git a/app/Member/Member.php b/app/Member/Member.php index 4c3ee3db..1c2d7ea4 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -6,12 +6,17 @@ use App\Events\MemberCreated; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; use App\Bill\BillKind; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use App\Nationality; +use App\Fee; +use App\Group; class Member extends Model { use Notifiable; + use HasFactory; - public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'send_newspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'fee_id', 'region_id', 'gender_id', 'confession_id', 'letter_address', 'bill_kind_id']; + public $fillable = ['firstname', 'lastname', 'nickname', 'other_country', 'birthday', 'joined_at', 'send_newspaper', 'address', 'further_address', 'zip', 'location', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'nami_id', 'group_id', 'letter_address', 'country_id', 'way_id', 'nationality_id', 'fee_id', 'region_id', 'gender_id', 'confession_id', 'letter_address', 'bill_kind_id']; public $dates = ['joined_at', 'birthday']; @@ -72,7 +77,7 @@ class Member extends Model public function nationality() { - return $this->belongsTo(App\Nationality::class); + return $this->belongsTo(Nationality::class); } public function memberships() @@ -82,18 +87,26 @@ class Member extends Model public function fee() { - return $this->belongsTo(App\Fee::class); + return $this->belongsTo(Fee::class); } public function billKind() { return $this->belongsTo(BillKind::class); } + public function group() { + return $this->belongsTo(Group::class); + } + public static function booted() { static::updating(function($model) { if ($model->nami_id === null) { $model->bill_kind_id = null; } }); + + static::updated(function($model) { + UpdateJob::dispatch($model); + }); } } diff --git a/app/Member/UpdateJob.php b/app/Member/UpdateJob.php new file mode 100644 index 00000000..2a6d6f61 --- /dev/null +++ b/app/Member/UpdateJob.php @@ -0,0 +1,62 @@ +memberId = $member->id; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $this->member = Member::find($this->memberId); + + Nami::putMember([ + 'firstname' => $this->member->firstname, + 'lastname' => $this->member->lastname, + 'nickname' => $this->member->nickname, + 'joined_at' => $this->member->joined_at, + 'birthday' => $this->member->birthday, + 'send_newspaper' => $this->member->send_newspaper, + 'address' => $this->member->address, + 'zip' => $this->member->zip, + 'location' => $this->member->location, + 'nickname' => $this->member->nickname, + 'other_country' => $this->member->other_country, + 'further_address' => $this->member->further_address, + 'main_phone' => $this->member->main_phone, + 'mobile_phone' => $this->member->mobile_phone, + 'work_phone' => $this->member->work_phone, + 'fax' => $this->member->fax, + 'email' => $this->member->email, + 'email_parents' => $this->member->email_parents, + 'gender_id' => optional($this->member)->nami_id, + 'confession_id' => optional($this->member->confession)->nami_id, + 'region_id' => optional($this->member->region)->nami_id, + 'country_id' => $this->member->country->nami_id, + 'fee_id' => optional($this->member->fee)->nami_id, + 'nationality_id' => $this->member->nationality->nami_id, + 'id' => $this->member->nami_id, + 'group_id' => $this->member->group->nami_id, + ]); + } +} diff --git a/database/factories/Member/MemberFactory.php b/database/factories/Member/MemberFactory.php new file mode 100644 index 00000000..f14552d0 --- /dev/null +++ b/database/factories/Member/MemberFactory.php @@ -0,0 +1,36 @@ + $this->faker->firstName, + 'lastname' => $this->faker->lastName, + 'birthday' => $this->faker->dateTimeBetween('-30 years'), + 'joined_at' => $this->faker->dateTimeBetween('-30 years'), + 'send_newspaper' => $this->faker->boolean, + 'address' => $this->faker->streetAddress, + 'zip' => $this->faker->postCode, + 'location' => $this->faker->city, + ]; + } +} diff --git a/tests/Feature/Member/UpdateTest.php b/tests/Feature/Member/UpdateTest.php new file mode 100644 index 00000000..e2e35262 --- /dev/null +++ b/tests/Feature/Member/UpdateTest.php @@ -0,0 +1,38 @@ +fakeNamiMembers([ + [ 'gruppierungId' => 12399, 'vorname' => 'Max', 'id' => 999 ] + ]); + + $member = Member::factory() + ->for(Country::factory()) + ->for(Group::factory()->state(['nami_id' => 12399])) + ->for(Nationality::factory()) + ->for(Fee::factory()) + ->create(['firstname' => 'Max', 'nami_id' => 999]); + + $member->update(['firstname' => 'Jane']); + + $this->assertMemberExists(12399, [ + 'vorname' => 'Jane', + 'id' => 999 + ]); + } +}