expenses = new InMemoryExpenseRepository; $this->jobs = new FakeExpenseJobs; $this->pricing = new FakeExpensePricing; $transactions = new FakeTransactionManager; $factory = new ExpenseDetailsFactory($this->pricing); $this->record = new RecordExpense($this->expenses, $this->jobs, $factory, $transactions); $this->update = new UpdateExpense($this->expenses, $this->jobs, $factory, $transactions); $this->remove = new RemoveExpense($this->expenses, $this->jobs, $transactions); $this->jobs->add(1); $this->jobs->add(2, locked: true); }); function purchase(array $fields = []): ExpenseData { return new ExpenseData(...array_merge([ 'userId' => 5, 'category' => ExpenseCategory::Materials, 'paidBy' => ExpensePayer::Company, 'spentOn' => '2026-11-20', 'description' => 'Tulostuspaperi', 'cost' => '100', ], $fields)); } function mileage(array $fields = []): ExpenseData { return purchase(array_merge([ 'category' => ExpenseCategory::Mileage, 'description' => '', 'cost' => '', 'kilometers' => '42,5', 'destination' => 'Tampere', 'purpose' => 'Kuvaukset', ], $fields)); } it('records a purchase billed at cost plus markup', function () { $id = $this->record->execute(1, purchase(['markup' => '15', 'paidBy' => ExpensePayer::Employee]), recordedBy: 9); $expense = $this->expenses->findById($id); expect($expense?->userId())->toBe(5) ->and($expense?->recordedBy())->toBe(9) ->and($expense?->details()->cost->cents)->toBe(10000) ->and($expense?->details()->billing?->amount->cents)->toBe(11500) ->and($expense?->details()->paidBy)->toBe(ExpensePayer::Employee); }); it('bills a given amount instead of cost plus markup', function () { $id = $this->record->execute(1, purchase(['markup' => '15', 'billedAmount' => '130']), 5); expect($this->expenses->findById($id)?->details()->billing?->amount->cents)->toBe(13000); }); it('keeps no billed amount for an expense that is not billed', function () { $id = $this->record->execute(1, purchase(['isBillable' => false, 'markup' => '15', 'billedAmount' => '130']), 5); expect($this->expenses->findById($id)?->details()->billing)->toBeNull(); }); it('prices kilometres with the mileage rate', function () { $id = $this->record->execute(1, mileage(), 5); $details = $this->expenses->findById($id)?->details(); expect($details?->cost->cents)->toBe(2508) ->and($details?->kilometers?->tenths)->toBe(425) ->and($details?->billing?->amount->cents)->toBe(2508); }); it('enforces expense rules', function (Closure $record, string $exception) { expect(fn () => $record->call($this))->toThrow($exception); })->with([ 'description' => [fn () => $this->record->execute(1, purchase(['description' => ' ']), 5), ExpenseDescriptionRequired::class], 'cost' => [fn () => $this->record->execute(1, purchase(['cost' => '']), 5), ExpenseCostRequired::class], 'cost format' => [fn () => $this->record->execute(1, purchase(['cost' => '10,999']), 5), InvalidMoneyAmount::class], 'mileage details' => [fn () => $this->record->execute(1, mileage(['purpose' => '']), 5), MileageDetailsRequired::class], 'kilometres format' => [fn () => $this->record->execute(1, mileage(['kilometers' => '4,25']), 5), InvalidKilometers::class], 'markup format' => [fn () => $this->record->execute(1, purchase(['markup' => 'paljon']), 5), InvalidMarkup::class], 'locked job' => [fn () => $this->record->execute(2, purchase(), 5), ExpenseLocked::class], 'unknown job' => [fn () => $this->record->execute(99, purchase(), 5), ExpenseJobNotFound::class], ]); it('needs a mileage rate for kilometres', function () { $this->pricing->mileageRateCents = null; $this->record->execute(1, mileage(), 5); })->throws(MileageRateMissing::class); it('changes and removes expenses only while the job is open', function () { $id = $this->record->execute(1, purchase(), 5); $this->update->execute($id, purchase(['userId' => 6, 'cost' => '80'])); expect($this->expenses->findById($id)?->userId())->toBe(6) ->and($this->expenses->findById($id)?->details()->cost->cents)->toBe(8000); $this->jobs->add(1, locked: true); expect(fn () => $this->update->execute($id, purchase()))->toThrow(ExpenseLocked::class) ->and(fn () => $this->remove->execute($id))->toThrow(ExpenseLocked::class); $this->jobs->add(1); $this->remove->execute($id); expect($this->expenses->items)->toBe([]); }); it('parses kilometres, markups and percentages', function () { expect(Kilometers::parse('42,5 km')?->tenths)->toBe(425) ->and(Kilometers::parse('')?->tenths)->toBeNull() ->and((new Kilometers(400))->format())->toBe('40') ->and(MarkupPercentage::parse('12,5 %')?->basisPoints)->toBe(1250) ->and(Money::fromCents(1999)->addPercent(1500)->cents)->toBe(2299); });