diff --git a/app/Initialize/InitializeFees.php b/app/Initialize/InitializeFees.php index 111912a6..021b6979 100644 --- a/app/Initialize/InitializeFees.php +++ b/app/Initialize/InitializeFees.php @@ -22,7 +22,6 @@ class InitializeFees Fee::create(['nami_id' => $fee->id, 'name' => $fee->name]) ->subscriptions()->create([ 'name' => $fee->name, - 'amount' => 1000, ]); }); } diff --git a/app/Letter/Page.php b/app/Letter/Page.php index 520c91d1..8f4b126e 100644 --- a/app/Letter/Page.php +++ b/app/Letter/Page.php @@ -49,7 +49,7 @@ class Page return $this->getPayments()->mapWithKeys(function (Payment $payment) { $key = "Beitrag {$payment->nr} für {$payment->member->firstname} {$payment->member->lastname} ({$payment->subscription->name})"; - return [$key => $this->number($payment->subscription->amount)]; + return [$key => $this->number($payment->subscription->getAmount())]; })->toArray(); } diff --git a/app/Letter/RememberDocument.php b/app/Letter/RememberDocument.php index 2bdfbcce..1a08c61a 100644 --- a/app/Letter/RememberDocument.php +++ b/app/Letter/RememberDocument.php @@ -4,7 +4,6 @@ namespace App\Letter; use App\Payment\Payment; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Collection; class RememberDocument extends Letter { @@ -18,57 +17,14 @@ class RememberDocument extends Letter return 'Zahlungserinnerung'; } - public function setFilename(string $filename): static - { - $this->filename = $filename; - - return $this; - } - - public function getFilename(): string - { - return $this->filename; - } - public function view(): string { return 'tex.remember'; } - /** - * @return array - */ - public function getPositions(Collection $page): array + public static function sendAllLabel(): string { - $memberIds = $page->pluck('id')->toArray(); - $payments = Payment::whereIn('member_id', $memberIds) - ->orderByRaw('nr, member_id')->whereNeedsRemember()->get(); - - return $payments->mapWithKeys(function (Payment $payment) { - $key = "Beitrag {$payment->nr} für {$payment->member->firstname} {$payment->member->lastname} ({$payment->subscription->name})"; - - return [$key => $this->number($payment->subscription->amount)]; - })->toArray(); - } - - public function getAddress(Collection $page): string - { - return $page->first()->address; - } - - public function getZip(Collection $page): string - { - return $page->first()->zip; - } - - public function getEmail(Collection $page): string - { - return $page->first()->email_parents ?: $page->first()->email; - } - - public function getLocation(Collection $page): string - { - return $page->first()->location; + return 'Erinnerungen versenden'; } public function afterSingle(Payment $payment): void @@ -103,9 +59,4 @@ class RememberDocument extends Letter 'Das zuletzt erinnerte Datum wird auf heute gesetzt.', ]; } - - public static function sendAllLabel(): string - { - return 'Erinnerungen versenden'; - } } diff --git a/app/Member/Actions/MemberShowAction.php b/app/Member/Actions/MemberShowAction.php index fa920ecf..4f24eb9a 100644 --- a/app/Member/Actions/MemberShowAction.php +++ b/app/Member/Actions/MemberShowAction.php @@ -20,7 +20,7 @@ class MemberShowAction return [ 'data' => new MemberResource($member ->load('memberships') - ->load('payments.subscription') + ->load('payments.subscription.children') ->load('nationality') ->load('region') ->load('subscription') diff --git a/app/Member/Member.php b/app/Member/Member.php index 7df1a599..6946d240 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -280,10 +280,11 @@ class Member extends Model public function scopeWithPendingPayment(Builder $q): Builder { return $q->addSelect([ - 'pending_payment' => Payment::selectRaw('SUM(subscriptions.amount)') + 'pending_payment' => Payment::selectRaw('SUM(subscription_children.amount)') ->whereColumn('payments.member_id', 'members.id') ->whereNeedsPayment() - ->join('subscriptions', 'subscriptions.id', 'payments.subscription_id'), + ->join('subscriptions', 'subscriptions.id', 'payments.subscription_id') + ->join('subscription_children', 'subscriptions.id', 'subscription_children.parent_id'), ]); } diff --git a/app/Payment/MemberPaymentBlock.php b/app/Payment/MemberPaymentBlock.php index 66d309eb..e605909a 100644 --- a/app/Payment/MemberPaymentBlock.php +++ b/app/Payment/MemberPaymentBlock.php @@ -12,7 +12,11 @@ class MemberPaymentBlock extends Block */ public function data(): array { - $amount = Payment::whereNeedsPayment()->selectRaw('sum(subscriptions.amount) AS nr')->join('subscriptions', 'subscriptions.id', 'payments.subscription_id')->first(); + $amount = Payment::whereNeedsPayment() + ->selectRaw('sum(subscription_children.amount) AS nr') + ->join('subscriptions', 'subscriptions.id', 'payments.subscription_id') + ->join('subscription_children', 'subscriptions.id', 'subscription_children.parent_id') + ->first(); $members = Member::whereHasPendingPayment()->count(); return [ diff --git a/app/Payment/Subscription.php b/app/Payment/Subscription.php index f0365afa..863c86fa 100644 --- a/app/Payment/Subscription.php +++ b/app/Payment/Subscription.php @@ -6,6 +6,7 @@ use App\Fee; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; class Subscription extends Model { @@ -13,8 +14,23 @@ class Subscription extends Model public $fillable = ['name', 'amount', 'fee_id']; + public function getAmount(): int + { + return $this->children->sum('amount'); + } + public function fee(): BelongsTo { return $this->belongsTo(Fee::class); } + + public function children(): HasMany + { + return $this->hasMany(SubscriptionChild::class, 'parent_id'); + } + + public static function booted(): void + { + static::deleting(fn ($model) => $model->children()->delete()); + } } diff --git a/app/Payment/SubscriptionChild.php b/app/Payment/SubscriptionChild.php new file mode 100644 index 00000000..4792ede9 --- /dev/null +++ b/app/Payment/SubscriptionChild.php @@ -0,0 +1,17 @@ + $this->amount, + 'name' => $this->name, + ]; + } +} diff --git a/app/Payment/SubscriptionController.php b/app/Payment/SubscriptionController.php index 7277ff5c..98168e24 100644 --- a/app/Payment/SubscriptionController.php +++ b/app/Payment/SubscriptionController.php @@ -29,20 +29,29 @@ class SubscriptionController extends Controller return \Inertia::render('subscription/SubscriptionForm', [ 'fees' => Fee::pluck('name', 'id'), 'mode' => 'create', - 'data' => (object) [], + 'data' => [ + 'children' => [], + ], ]); } public function store(Request $request): RedirectResponse { - Subscription::create($request->validate([ + $subscriptionParams = $request->validate([ 'name' => 'required|max:255', - 'amount' => 'required|numeric', 'fee_id' => 'required|exists:fees,id', ], [], [ 'fee_id' => 'Nami-Beitrag', - 'amount' => 'Interner Beitrag', - ])); + ]); + + $children = $request->validate([ + 'children' => 'present|array', + 'children.*.amount' => 'required|numeric', + 'children.*.name' => 'required|max:255', + ]); + + $subscription = Subscription::create($subscriptionParams); + $subscription->children()->createMany($children['children']); return redirect()->route('subscription.index'); } @@ -61,14 +70,27 @@ class SubscriptionController extends Controller public function update(Subscription $subscription, Request $request): RedirectResponse { - $subscription->update($request->validate([ + $subscriptionParams = $request->validate([ 'name' => 'required|max:255', - 'amount' => 'required|numeric', 'fee_id' => 'required|exists:fees,id', ], [], [ 'fee_id' => 'Nami-Beitrag', - 'amount' => 'Interner Beitrag', - ])); + ]); + $subscription->update($subscriptionParams); + $children = $request->validate([ + 'children' => 'present|array', + 'children.*.amount' => 'required|numeric', + 'children.*.name' => 'required|max:255', + ]); + $subscription->children()->delete(); + $subscription->children()->createMany($children['children']); + + return redirect()->route('subscription.index'); + } + + public function destroy(Subscription $subscription): RedirectResponse + { + $subscription->delete(); return redirect()->route('subscription.index'); } diff --git a/app/Payment/SubscriptionResource.php b/app/Payment/SubscriptionResource.php index 76c8937f..4132eff7 100644 --- a/app/Payment/SubscriptionResource.php +++ b/app/Payment/SubscriptionResource.php @@ -23,8 +23,9 @@ class SubscriptionResource extends JsonResource 'name' => $this->name, 'fee_id' => $this->fee_id, 'fee_name' => $this->fee->name, - 'amount_human' => number_format($this->amount / 100, 2, ',', '.').' €', - 'amount' => $this->amount, + 'amount_human' => number_format($this->getAmount() / 100, 2, ',', '.').' €', + 'amount' => $this->getAmount(), + 'children' => SubscriptionChildResource::collection($this->whenLoaded('children')), ]; } } diff --git a/database/factories/Payment/PaymentFactory.php b/database/factories/Payment/PaymentFactory.php index 8016ec72..e728d46d 100644 --- a/database/factories/Payment/PaymentFactory.php +++ b/database/factories/Payment/PaymentFactory.php @@ -7,6 +7,7 @@ use App\Payment\Payment; use App\Payment\Status; use App\Payment\Subscription; use Illuminate\Database\Eloquent\Factories\Factory; +use Tests\RequestFactories\Child; class PaymentFactory extends Factory { @@ -37,10 +38,13 @@ class PaymentFactory extends Factory return $this->state(['nr' => $nr]); } - public function subscription(string $name, int $amount): self + /** + * @param array $children + */ + public function subscription(string $name, array $children): self { return $this->for( - Subscription::factory()->state(['name' => $name, 'amount' => $amount])->for(Fee::factory()) + Subscription::factory()->state(['name' => $name])->for(Fee::factory())->children($children) ); } } diff --git a/database/factories/Payment/SubscriptionChildFactory.php b/database/factories/Payment/SubscriptionChildFactory.php new file mode 100644 index 00000000..345c1bc6 --- /dev/null +++ b/database/factories/Payment/SubscriptionChildFactory.php @@ -0,0 +1,27 @@ + + */ +class SubscriptionChildFactory extends Factory +{ + protected $model = SubscriptionChild::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'amount' => $this->faker->numberBetween(10, 3000), + 'name' => $this->faker->words(5, true), + ]; + } +} diff --git a/database/factories/Payment/SubscriptionFactory.php b/database/factories/Payment/SubscriptionFactory.php index 05185da4..595f9fa5 100644 --- a/database/factories/Payment/SubscriptionFactory.php +++ b/database/factories/Payment/SubscriptionFactory.php @@ -4,7 +4,9 @@ namespace Database\Factories\Payment; use App\Fee; use App\Payment\Subscription; +use App\Payment\SubscriptionChild; use Illuminate\Database\Eloquent\Factories\Factory; +use Tests\RequestFactories\Child; class SubscriptionFactory extends Factory { @@ -14,7 +16,6 @@ class SubscriptionFactory extends Factory { return [ 'name' => $this->faker->word, - 'amount' => $this->faker->numberBetween(1000, 50000), 'fee_id' => Fee::factory()->createOne()->id, ]; } @@ -24,8 +25,17 @@ class SubscriptionFactory extends Factory return $this->state(['name' => $name]); } - public function amount(int $amount): self + /** + * @param array $children + */ + public function children(array $children): self { - return $this->state(['amount' => $amount]); + $instance = $this; + + foreach ($children as $child) { + $instance = $instance->has(SubscriptionChild::factory()->state($child->toArray()), 'children'); + } + + return $instance; } } diff --git a/database/migrations/2022_12_13_203644_create_subscription_children_table.php b/database/migrations/2022_12_13_203644_create_subscription_children_table.php new file mode 100644 index 00000000..606b4fc2 --- /dev/null +++ b/database/migrations/2022_12_13_203644_create_subscription_children_table.php @@ -0,0 +1,46 @@ +get(); + + Schema::table('subscriptions', function (Blueprint $table) { + $table->dropColumn('amount'); + }); + + Schema::create('subscription_children', function (Blueprint $table) { + $table->uuid('id'); + $table->foreignId('parent_id')->constrained('subscriptions'); + $table->string('name'); + $table->unsignedInteger('amount'); + }); + + foreach ($subscriptions as $subscription) { + SubscriptionChild::create([ + 'parent_id' => $subscription->id, + 'name' => 'name', + 'amount' => $subscription->amount, + ]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +}; diff --git a/resources/js/views/subscription/SubscriptionForm.vue b/resources/js/views/subscription/SubscriptionForm.vue index 9cfa4f15..b4fe9532 100644 --- a/resources/js/views/subscription/SubscriptionForm.vue +++ b/resources/js/views/subscription/SubscriptionForm.vue @@ -1,16 +1,50 @@ diff --git a/tests/Feature/Letter/DocumentFactoryTest.php b/tests/Feature/Letter/DocumentFactoryTest.php index 0e9d4808..6ee1c488 100644 --- a/tests/Feature/Letter/DocumentFactoryTest.php +++ b/tests/Feature/Letter/DocumentFactoryTest.php @@ -11,6 +11,7 @@ use App\Letter\RememberDocument; use App\Member\Member; use App\Payment\Payment; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\TestCase; use Zoomyboy\Tex\Tex; @@ -40,7 +41,10 @@ class DocumentFactoryTest extends TestCase 'zip' => '::zip::', 'location' => '::location::', ]) - ->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', 1500)) + ->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', [ + new Child('a', 1000), + new Child('a', 500), + ])) ->create(); $letter = app(DocumentFactory::class)->singleLetter(BillDocument::class, $this->query($member)); @@ -59,7 +63,7 @@ class DocumentFactoryTest extends TestCase $member = Member::factory() ->defaults() ->state(['lastname' => '::lastname::']) - ->has(Payment::factory()->notPaid()->nr('1995')->subscription('::subName::', 1500)) + ->has(Payment::factory()->notPaid()->nr('1995')) ->create(); $letter = app(DocumentFactory::class)->singleLetter(BillDocument::class, $this->query($member)); diff --git a/tests/Feature/Letter/LetterSendActionTest.php b/tests/Feature/Letter/LetterSendActionTest.php index f110f822..5ba74aa7 100644 --- a/tests/Feature/Letter/LetterSendActionTest.php +++ b/tests/Feature/Letter/LetterSendActionTest.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Storage; use Mail; +use Tests\RequestFactories\Child; use Tests\TestCase; use Zoomyboy\Tex\Tex; @@ -29,7 +30,9 @@ class LetterSendActionTest extends TestCase $this->login()->loginNami(); $this->member = Member::factory() ->defaults() - ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', 5400)) + ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', [ + new Child('a', 5400), + ])) ->emailBillKind() ->create(['firstname' => 'Lah', 'lastname' => 'Mom', 'email' => 'peter@example.com']); } diff --git a/tests/Feature/Member/IndexTest.php b/tests/Feature/Member/IndexTest.php index 789e7150..c6586c4b 100644 --- a/tests/Feature/Member/IndexTest.php +++ b/tests/Feature/Member/IndexTest.php @@ -9,6 +9,7 @@ use App\Payment\Payment; use App\Subactivity; use Carbon\Carbon; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\TestCase; class IndexTest extends TestCase @@ -108,7 +109,10 @@ class IndexTest extends TestCase { $this->withoutExceptionHandling()->login()->loginNami(); $member = Member::factory() - ->has(Payment::factory()->notPaid()->nr('2019')->subscription('Free', 1050)) + ->has(Payment::factory()->notPaid()->nr('2019')->subscription('Free', [ + new Child('a', 1000), + new Child('b', 50), + ])) ->defaults()->create(); $response = $this->get('/member'); diff --git a/tests/Feature/Member/ShowTest.php b/tests/Feature/Member/ShowTest.php index b702f6d6..15d6d40c 100644 --- a/tests/Feature/Member/ShowTest.php +++ b/tests/Feature/Member/ShowTest.php @@ -15,6 +15,7 @@ use App\Payment\Subscription; use App\Region; use Carbon\Carbon; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\TestCase; class ShowTest extends TestCase @@ -34,7 +35,10 @@ class ShowTest extends TestCase $member = Member::factory() ->defaults() ->has(Membership::factory()->promise(now())->in('€ LeiterIn', 5, 'Jungpfadfinder', 88)->state(['created_at' => '2022-11-19 05:00:00'])) - ->has(Payment::factory()->notPaid()->nr('2019')->subscription('Free', 1050)) + ->has(Payment::factory()->notPaid()->nr('2019')->subscription('Free', [ + new Child('uu', 1000), + new Child('a', 50), + ])) ->for(Gender::factory()->name('Männlich')) ->for(Region::factory()->name('NRW')) ->postBillKind() diff --git a/tests/Feature/Payment/MemberPaymentsBlockTest.php b/tests/Feature/Payment/MemberPaymentsBlockTest.php index b3bfa4c3..c006d0bf 100644 --- a/tests/Feature/Payment/MemberPaymentsBlockTest.php +++ b/tests/Feature/Payment/MemberPaymentsBlockTest.php @@ -6,6 +6,7 @@ use App\Member\Member; use App\Payment\MemberPaymentBlock; use App\Payment\Payment; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\TestCase; class MemberPaymentsBlockTest extends TestCase @@ -18,7 +19,10 @@ class MemberPaymentsBlockTest extends TestCase Member::factory() ->defaults() - ->has(Payment::factory()->notPaid()->subscription('example', 3400)) + ->has(Payment::factory()->notPaid()->subscription('example', [ + new Child('gg', 3400), + new Child('gg', 100), + ])) ->create(); Member::factory() ->defaults() @@ -27,7 +31,7 @@ class MemberPaymentsBlockTest extends TestCase $data = app(MemberPaymentBlock::class)->render()['data']; $this->assertEquals([ - 'amount' => '34,00 €', + 'amount' => '35,00 €', 'members' => 1, 'total_members' => 2, ], $data); diff --git a/tests/Feature/Sendpayment/SendpaymentTest.php b/tests/Feature/Sendpayment/SendpaymentTest.php index 030b2b62..4453c160 100644 --- a/tests/Feature/Sendpayment/SendpaymentTest.php +++ b/tests/Feature/Sendpayment/SendpaymentTest.php @@ -8,6 +8,7 @@ use App\Member\Member; use App\Payment\Payment; use App\Payment\Status; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\RequestFactories\LetterSettingsFake; use Tests\TestCase; use Zoomyboy\Tex\Tex; @@ -37,8 +38,8 @@ class SendpaymentTest extends TestCase $this->login()->loginNami(); $member = Member::factory() ->defaults() - ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', 5400)) - ->has(Payment::factory()->paid()->nr('1998')->subscription('bezahltdesc', 5800)) + ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', [new Child('a', 5400)])) + ->has(Payment::factory()->paid()->nr('1998')->subscription('bezahltdesc', [new Child('b', 5800)])) ->postBillKind() ->create(); @@ -59,7 +60,7 @@ class SendpaymentTest extends TestCase $this->login()->loginNami(); $member = Member::factory() ->defaults() - ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', 5400)) + ->has(Payment::factory()->notPaid()->nr('1997')->subscription('tollerbeitrag', [new Child('u', 5400)])) ->emailBillKind() ->create(); diff --git a/tests/Feature/Subscription/DestroyTest.php b/tests/Feature/Subscription/DestroyTest.php new file mode 100644 index 00000000..80a38289 --- /dev/null +++ b/tests/Feature/Subscription/DestroyTest.php @@ -0,0 +1,29 @@ +withoutExceptionHandling()->login()->loginNami(); + $subscription = Subscription::factory()->name('hi')->for(Fee::factory())->children([ + new Child('a', 1400), + new Child('b', 1500), + ])->create(); + + $response = $this->from('/subscription')->delete("/subscription/{$subscription->id}"); + + $response->assertRedirect('/subscription'); + $this->assertDatabaseCount('subscription_children', 0); + $this->assertDatabaseCount('subscriptions', 0); + } +} diff --git a/tests/Feature/Subscription/EditTest.php b/tests/Feature/Subscription/EditTest.php new file mode 100644 index 00000000..09d1c9ee --- /dev/null +++ b/tests/Feature/Subscription/EditTest.php @@ -0,0 +1,34 @@ +withoutExceptionHandling()->login()->loginNami(); + $subscription = Subscription::factory()->name('hi')->for(Fee::factory())->children([ + new Child('a', 1400), + new Child('b', 1500), + ])->create(); + + $response = $this->get("/subscription/{$subscription->id}/edit"); + + $this->assertInertiaHas([ + 'children' => [ + ['name' => 'a', 'amount' => 1400], + ['name' => 'b', 'amount' => 1500], + ], + 'name' => 'hi', + 'id' => $subscription->id, + ], $response, 'data'); + } +} diff --git a/tests/Feature/Subscription/StoreTest.php b/tests/Feature/Subscription/StoreTest.php index 3e6f973b..139b2197 100644 --- a/tests/Feature/Subscription/StoreTest.php +++ b/tests/Feature/Subscription/StoreTest.php @@ -3,7 +3,9 @@ namespace Tests\Feature\Subscription; use App\Fee; +use App\Payment\Subscription; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\RequestFactories\SubscriptionRequestFactory; use Tests\TestCase; @@ -18,14 +20,21 @@ class StoreTest extends TestCase $response = $this->from('/subscription')->post( '/subscription', - SubscriptionRequestFactory::new()->amount(2500)->fee($fee)->name('lorem')->create() + SubscriptionRequestFactory::new()->fee($fee)->name('lorem')->children([ + new Child('ch', 2500), + ])->create() ); $response->assertRedirect('/subscription'); + $subscription = Subscription::firstWhere('name', 'lorem'); $this->assertDatabaseHas('subscriptions', [ - 'amount' => 2500, 'fee_id' => $fee->id, - 'name' => 'Lorem', + 'name' => 'lorem', + ]); + $this->assertDatabaseHas('subscription_children', [ + 'name' => 'ch', + 'amount' => 2500, + 'parent_id' => $subscription->id, ]); } @@ -40,7 +49,6 @@ class StoreTest extends TestCase ); $this->assertErrors([ - 'amount' => 'Interner Beitrag ist erforderlich.', 'fee_id' => 'Nami-Beitrag ist nicht vorhanden.', 'name' => 'Name ist erforderlich.', ], $response); diff --git a/tests/Feature/Subscription/UpdateTest.php b/tests/Feature/Subscription/UpdateTest.php index c593602a..0d8293e6 100644 --- a/tests/Feature/Subscription/UpdateTest.php +++ b/tests/Feature/Subscription/UpdateTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\Subscription; use App\Fee; use App\Payment\Subscription; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\RequestFactories\Child; use Tests\RequestFactories\SubscriptionRequestFactory; use Tests\TestCase; @@ -15,7 +16,7 @@ class UpdateTest extends TestCase public function testItUpdatesASubscription(): void { $this->withoutExceptionHandling()->login()->loginNami(); - $subscription = Subscription::factory()->amount(670)->name('hi')->for(Fee::factory())->create(); + $subscription = Subscription::factory()->name('hi')->for(Fee::factory())->create(); $fee = Fee::factory()->create(); $response = $this->from("/subscription/{$subscription->id}")->patch( @@ -26,17 +27,39 @@ class UpdateTest extends TestCase $response->assertRedirect('/subscription'); $this->assertDatabaseHas('subscriptions', [ 'id' => $subscription->id, - 'amount' => 2500, 'fee_id' => $fee->id, 'name' => 'Lorem', ]); } + public function testItUpdatesChildren(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + $subscription = Subscription::factory()->name('hi')->for(Fee::factory())->children([ + new Child('a', 1400), + new Child('b', 1500), + ])->create(); + + $response = $this->from("/subscription/{$subscription->id}")->patch( + "/subscription/{$subscription->id}", + SubscriptionRequestFactory::new()->children([ + new Child('c', 1900), + ])->create() + ); + + $response->assertRedirect('/subscription'); + $this->assertDatabaseHas('subscription_children', [ + 'parent_id' => $subscription->id, + 'name' => 'c', + 'amount' => 1900, + ]); + $this->assertDatabaseCount('subscription_children', 1); + } + public function testItValidatesRequest(): void { $this->login()->loginNami(); - $subscription = Subscription::factory()->amount(670)->name('hi')->for(Fee::factory())->create(); - $fee = Fee::factory()->create(); + $subscription = Subscription::factory()->name('hi')->for(Fee::factory())->create(); $response = $this->from("/subscription/{$subscription->id}")->patch( "/subscription/{$subscription->id}", @@ -44,7 +67,6 @@ class UpdateTest extends TestCase ); $this->assertErrors([ - 'amount' => 'Interner Beitrag ist erforderlich.', 'fee_id' => 'Nami-Beitrag ist nicht vorhanden.', 'name' => 'Name ist erforderlich.', ], $response); diff --git a/tests/RequestFactories/Child.php b/tests/RequestFactories/Child.php new file mode 100644 index 00000000..36d393f3 --- /dev/null +++ b/tests/RequestFactories/Child.php @@ -0,0 +1,21 @@ + $this->amount, + 'name' => $this->name, + ]; + } +} diff --git a/tests/RequestFactories/SubscriptionRequestFactory.php b/tests/RequestFactories/SubscriptionRequestFactory.php index 1be28522..50821c68 100644 --- a/tests/RequestFactories/SubscriptionRequestFactory.php +++ b/tests/RequestFactories/SubscriptionRequestFactory.php @@ -7,12 +7,15 @@ use Worksome\RequestFactories\RequestFactory; class SubscriptionRequestFactory extends RequestFactory { + /** + * @return array> + */ public function definition(): array { return [ - 'amount' => $this->faker->numberBetween(100, 2000), 'fee_id' => Fee::factory()->create()->id, 'name' => $this->faker->words(5, true), + 'children' => [], ]; } @@ -34,9 +37,16 @@ class SubscriptionRequestFactory extends RequestFactory public function invalid(): self { return $this->state([ - 'amount' => '', 'fee_id' => 9999, 'name' => '', ]); } + + /** + * @param array $children + */ + public function children(array $children): self + { + return $this->state(['children' => array_map(fn ($child) => $child->toArray(), $children)]); + } }