From 9d53dfc7726151673286ee6162fc179adacc7b70 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 12 Aug 2022 22:07:59 +0200 Subject: [PATCH] Fix: Store memebr --- app/Bill/BillKind.php | 3 + app/Gender.php | 2 + app/Member/MemberRequest.php | 15 ++- app/Region.php | 2 + database/factories/Bill/BillKindFactory.php | 26 +++++ database/factories/CountryFactory.php | 3 + database/factories/GenderFactory.php | 24 ++++ database/factories/NationalityFactory.php | 3 + database/factories/RegionFactory.php | 25 ++++ tests/Feature/Member/StoreTest.php | 119 ++++++++++++++++++++ tests/Lib/MergesAttributes.php | 19 ++++ tests/TestCase.php | 3 + 12 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 database/factories/Bill/BillKindFactory.php create mode 100644 database/factories/GenderFactory.php create mode 100644 database/factories/RegionFactory.php create mode 100644 tests/Feature/Member/StoreTest.php create mode 100644 tests/Lib/MergesAttributes.php diff --git a/app/Bill/BillKind.php b/app/Bill/BillKind.php index fea99c2d..daa60022 100644 --- a/app/Bill/BillKind.php +++ b/app/Bill/BillKind.php @@ -2,10 +2,13 @@ namespace App\Bill; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class BillKind extends Model { + use HasFactory; + public $fillable = ['name']; public $timestamps = false; } diff --git a/app/Gender.php b/app/Gender.php index 8eab60cd..44fc4090 100644 --- a/app/Gender.php +++ b/app/Gender.php @@ -3,11 +3,13 @@ namespace App; use App\Nami\HasNamiField; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Gender extends Model { use HasNamiField; + use HasFactory; public $fillable = ['name', 'nami_id']; } diff --git a/app/Member/MemberRequest.php b/app/Member/MemberRequest.php index 9df8de6b..bf75f66b 100644 --- a/app/Member/MemberRequest.php +++ b/app/Member/MemberRequest.php @@ -65,13 +65,24 @@ class MemberRequest extends FormRequest 'without_efz_at' => 'nullable|date_format:Y-m-d', 'multiply_pv' => 'boolean', 'multiply_more_pv' => 'boolean', + 'send_newspaper' => 'boolean', + 'main_phone' => '', + 'mobile_phone' => '', + 'letter_address' => '', + 'gender_id' => 'nullable|exists:genders,id', + 'region_id' => 'nullable|exists:regions,id', + 'nationality_id' => 'nullable|exists:nationalities,id', + 'children_phone' => '', + 'fax' => '', ]; } public function persistCreate(NamiSettings $settings): void { - $this->merge(['group_id' => Group::where('nami_id', $settings->default_group_id)->firstOrFail()->id]); - $member = Member::create($this->input()); + $member = Member::create([ + ...$this->validated(), + 'group_id' => Group::where('nami_id', $settings->default_group_id)->firstOrFail()->id, + ]); if ($this->input('has_nami')) { CreateJob::dispatch($member); } diff --git a/app/Region.php b/app/Region.php index 5a9ba256..0a46a05f 100644 --- a/app/Region.php +++ b/app/Region.php @@ -3,10 +3,12 @@ namespace App; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Region extends Model { + use HasFactory; public $timestamps = false; public $fillable = ['name', 'nami_id', 'is_null']; diff --git a/database/factories/Bill/BillKindFactory.php b/database/factories/Bill/BillKindFactory.php new file mode 100644 index 00000000..1adfbe1b --- /dev/null +++ b/database/factories/Bill/BillKindFactory.php @@ -0,0 +1,26 @@ + + */ +class BillKindFactory extends Factory +{ + public $model = BillKind::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'name' => $this->faker->words(3, true), + ]; + } +} diff --git a/database/factories/CountryFactory.php b/database/factories/CountryFactory.php index 69ce0675..e7eb1ffe 100644 --- a/database/factories/CountryFactory.php +++ b/database/factories/CountryFactory.php @@ -5,6 +5,9 @@ namespace Database\Factories; use App\Country; use Illuminate\Database\Eloquent\Factories\Factory; +/** + * @extends Factory + */ class CountryFactory extends Factory { protected $model = Country::class; diff --git a/database/factories/GenderFactory.php b/database/factories/GenderFactory.php new file mode 100644 index 00000000..29823b5e --- /dev/null +++ b/database/factories/GenderFactory.php @@ -0,0 +1,24 @@ + + */ +class GenderFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'name' => $this->faker->words(3, true), + 'nami_id' => $this->faker->numberBetween(100, 200), + ]; + } +} diff --git a/database/factories/NationalityFactory.php b/database/factories/NationalityFactory.php index 0342fae4..a38f1b94 100644 --- a/database/factories/NationalityFactory.php +++ b/database/factories/NationalityFactory.php @@ -5,6 +5,9 @@ namespace Database\Factories; use App\Nationality; use Illuminate\Database\Eloquent\Factories\Factory; +/** + * @extends Factory + */ class NationalityFactory extends Factory { protected $model = Nationality::class; diff --git a/database/factories/RegionFactory.php b/database/factories/RegionFactory.php new file mode 100644 index 00000000..a50feea8 --- /dev/null +++ b/database/factories/RegionFactory.php @@ -0,0 +1,25 @@ + + */ +class RegionFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'name' => $this->faker->words(3, true), + 'nami_id' => $this->faker->numberBetween(100, 200), + 'is_null' => false, + ]; + } +} diff --git a/tests/Feature/Member/StoreTest.php b/tests/Feature/Member/StoreTest.php new file mode 100644 index 00000000..ee425ede --- /dev/null +++ b/tests/Feature/Member/StoreTest.php @@ -0,0 +1,119 @@ +create(); + NamiSettings::fake([ + 'default_group_id' => 55, + 'password' => 'tt', + ]); + $this->withoutExceptionHandling()->login()->loginNami(); + $country = Country::factory()->create(); + $gender = Gender::factory()->create(); + $region = Region::factory()->create(); + $nationality = Nationality::factory()->create(); + $activity = Activity::factory()->create(); + $subactivity = Subactivity::factory()->create(); + $subscription = Subscription::factory()->create(); + $billKind = BillKind::factory()->create(); + + $response = $this + ->from('/member/create') + ->post('/member', $this->attributes([ + 'country_id' => $country->id, + 'gender_id' => $gender->id, + 'region_id' => $region->id, + 'nationality_id' => $nationality->id, + 'first_activity_id' => $activity->id, + 'first_subactivity_id' => $subactivity->id, + 'subscription_id' => $subscription->id, + 'bill_kind_id' => $billKind->id, + ])); + + $response->assertStatus(302)->assertSessionHasNoErrors(); + $response->assertRedirect('/member'); + $member = Member::firstWhere('firstname', 'Joe'); + Queue::assertPushed(CreateJob::class, fn ($job) => $job->memberId === $member->id); + $this->assertDatabaseHas('members', [ + 'address' => 'Bavert 50', + 'bill_kind_id' => $billKind->id, + 'birthday' => '2013-02-19', + 'children_phone' => '+49 123 44444', + 'country_id' => $country->id, + 'email_parents' => 'osloot@aol.com', + 'firstname' => 'Joe', + 'gender_id' => $gender->id, + 'joined_at' => '2022-08-12', + 'lastname' => 'Muster', + 'letter_address' => null, + 'location' => 'Solingen', + 'main_phone' => '+49 212 2334322', + 'mobile_phone' => '+49 157 53180451', + 'nationality_id' => $nationality->id, + 'region_id' => $region->id, + 'send_newspaper' => '1', + 'subscription_id' => $subscription->id, + 'zip' => '42719', + 'fax' => '+49 666', + ]); + } + + public function defaults(): array + { + return [ + 'address' => 'Bavert 50', + 'birthday' => '2013-02-19', + 'children_phone' => '+49 123 44444', + 'efz' => '', + 'email' => '', + 'email_parents' => 'osloot@aol.com', + 'fax' => '+49 666', + 'firstname' => 'Joe', + 'further_address' => '', + 'has_nami' => true, + 'has_svk' => false, + 'has_vk' => false, + 'joined_at' => '2022-08-12', + 'lastname' => 'Muster', + 'letter_address' => '', + 'location' => 'Solingen', + 'main_phone' => '+49 212 2334322', + 'mobile_phone' => '+49 157 53180451', + 'more_ps_at' => '', + 'multiply_more_pv' => false, + 'multiply_pv' => false, + 'other_country' => '', + 'ps_at' => '', + 'send_newspaper' => true, + 'without_education_at' => '', + 'without_efz_at' => '', + 'work_phone' => '', + 'zip' => '42719', + ]; + } +} diff --git a/tests/Lib/MergesAttributes.php b/tests/Lib/MergesAttributes.php new file mode 100644 index 00000000..d36b9540 --- /dev/null +++ b/tests/Lib/MergesAttributes.php @@ -0,0 +1,19 @@ +defaults()); + + foreach ($overwrites as $key => $value) { + $defaults->put($key, $value); + } + + return $defaults->toArray(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index f22601c1..13efdedc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Tests; +use App\Group; use App\Member\Member; use App\Setting\NamiSettings; use App\User; @@ -30,7 +31,9 @@ abstract class TestCase extends BaseTestCase NamiSettings::fake([ 'mglnr' => $mglnr, 'password' => $password, + 'default_group_id' => 55, ]); + Group::factory()->create(['nami_id' => 55]); return $this; }