forceFill(array_merge([ 'lan_id' => 1, 'name' => 'Erik Olsson', 'guardian_name' => 'VÄrdnadshavare', 'deposit' => 0, 'amount_left' => 0, 'give_leftover' => 0, 'is_in_group' => 0, ], $attributes))->save(); return $customer; } public function test_deleting_a_deposit_takes_the_money_back_off_the_balance(): void { $customer = $this->customer(['deposit' => 500, 'amount_left' => 500]); $deposit = Deposit::create(['customer_id' => $customer->id, 'amount' => 200]); (new DepositController())->destroy($deposit->id); $customer->refresh(); $this->assertSame(300, $customer->deposit); $this->assertSame(300, $customer->amount_left); $this->assertNull(Deposit::find($deposit->id)); } public function test_deleting_a_group_members_deposit_reverses_it_on_the_group_account(): void { $group = $this->customer([ 'lan_id' => 10, 'name' => 'Klanen', 'deposit' => 500, 'amount_left' => 500, 'is_in_group' => 0, 'customer_group_id' => 7, ]); $member = $this->customer([ 'lan_id' => 11, 'name' => 'Anna', 'deposit' => 0, 'amount_left' => 0, 'is_in_group' => 1, 'customer_group_id' => 7, ]); // Deposits from a member are recorded on the member but credited to the group $deposit = Deposit::create(['customer_id' => $member->id, 'amount' => 200]); (new DepositController())->destroy($deposit->id); $this->assertSame(300, $group->refresh()->deposit); $this->assertSame(300, $group->amount_left); $this->assertSame(0, $member->refresh()->deposit); } public function test_deleting_a_purchase_gives_the_money_back(): void { $customer = $this->customer(['deposit' => 500, 'amount_left' => 300]); $purchase = Purchase::create(['customer_id' => $customer->id, 'amount' => 200]); (new PurchaseController())->destroy($purchase->id); $customer->refresh(); $this->assertSame(500, $customer->amount_left); // A purchase never touched the deposit, so it stays where it was $this->assertSame(500, $customer->deposit); $this->assertNull(Purchase::find($purchase->id)); } public function test_a_deposit_survives_a_round_trip_through_store_and_destroy(): void { // lan_id deliberately differs from id, so it is clear which one store() used $customer = $this->customer(['lan_id' => 4242, 'deposit' => 500, 'amount_left' => 500]); $request = \Illuminate\Http\Request::create('/api/register_deposit', 'POST', [ 'customer_id' => 4242, 'deposit' => 250, 'give_leftover' => 0, ]); (new DepositController())->store($request); $this->assertSame(750, $customer->refresh()->deposit); (new DepositController())->destroy(Deposit::first()->id); $customer->refresh(); $this->assertSame(500, $customer->deposit); $this->assertSame(500, $customer->amount_left); } public function test_amount_left_can_be_mass_assigned_now_that_the_key_is_spelled_right(): void { $customer = Customer::create([ 'lan_id' => 5, 'name' => 'Test Testsson', 'guardian_name' => 'VÄrdnadshavare', 'deposit' => 100, 'amount_left' => 100, ]); $this->assertSame(100, $customer->refresh()->amount_left); } }