Add memberTest

This commit is contained in:
philipp lang 2021-06-13 11:33:50 +02:00
parent 5181d9db7f
commit dc971499f6
5 changed files with 159 additions and 8 deletions

View File

@ -9,6 +9,7 @@ use App\Member\Member;
use App\Region; use App\Region;
use App\Nationality; use App\Nationality;
use App\Fee; use App\Fee;
use App\Group;
class InitializeMembers { class InitializeMembers {
@ -45,11 +46,12 @@ class InitializeMembers {
'email' => $member->email, 'email' => $member->email,
'email_parents' => $member->email_parents, 'email_parents' => $member->email_parents,
'nami_id' => $member->id, 'nami_id' => $member->id,
'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id))->id, 'group_id' => Group::firstOrCreate(['nami_id' => $member->group_id], ['nami_id' => $member->group_id, 'name' => 'AAA'])->id,
'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id))->id, 'gender_id' => optional(Gender::firstWhere('nami_id', $member->gender_id ?: -1))->id,
'region_id' => Region::where('nami_id', $member->region_id)->firstOrFail()->id, 'confession_id' => optional(Confession::firstWhere('nami_id', $member->confession_id ?: -1))->id,
'country_id' => Country::where('nami_id', '=', $member->country_id)->firstOrFail()->id, 'region_id' => optional(Region::firstWhere('nami_id', $member->region_id ?: -1))->id,
'fee_id' => optional(Fee::firstWhere('nami_id', '=', $member->fee_id))->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, 'nationality_id' => Nationality::where('nami_id', $member->nationality_id)->firstOrFail()->id,
]); ]);
}); });

View File

@ -6,12 +6,17 @@ use App\Events\MemberCreated;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use App\Bill\BillKind; use App\Bill\BillKind;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Nationality;
use App\Fee;
use App\Group;
class Member extends Model class Member extends Model
{ {
use Notifiable; 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']; public $dates = ['joined_at', 'birthday'];
@ -72,7 +77,7 @@ class Member extends Model
public function nationality() public function nationality()
{ {
return $this->belongsTo(App\Nationality::class); return $this->belongsTo(Nationality::class);
} }
public function memberships() public function memberships()
@ -82,18 +87,26 @@ class Member extends Model
public function fee() public function fee()
{ {
return $this->belongsTo(App\Fee::class); return $this->belongsTo(Fee::class);
} }
public function billKind() { public function billKind() {
return $this->belongsTo(BillKind::class); return $this->belongsTo(BillKind::class);
} }
public function group() {
return $this->belongsTo(Group::class);
}
public static function booted() { public static function booted() {
static::updating(function($model) { static::updating(function($model) {
if ($model->nami_id === null) { if ($model->nami_id === null) {
$model->bill_kind_id = null; $model->bill_kind_id = null;
} }
}); });
static::updated(function($model) {
UpdateJob::dispatch($model);
});
} }
} }

62
app/Member/UpdateJob.php Normal file
View File

@ -0,0 +1,62 @@
<?php
namespace App\Member;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Zoomyboy\LaravelNami\Nami;
class UpdateJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $memberId;
public $member;
public function __construct(Member $member)
{
$this->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,
]);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Database\Factories\Member;
use App\Member\Member;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Country;
class MemberFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Member::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'firstname' => $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,
];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Tests\Feature\Member;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Member\Member;
use App\Country;
use App\Nationality;
use App\Fee;
use App\Group;
class UpdateTest extends TestCase
{
use RefreshDatabase;
public function test_it_can_update_a_member()
{
$this->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
]);
}
}