What's the best way to test a function modularly

I have this computed text column, as you can see it's pretty complicated, so I want to add some tests, problem is that I've never wrote a test before. if possible I want to test this function without relying on the UI if you get what I mean, i.e. given this input what does the function output


TextColumn::make('ref')
    ->getStateUsing(
        function (Model $record) {
            $startOfYear = now()->startOfYear()->toDateTimeString();
            $now = now()->toDateTimeString();
            $invoices = Invoice::whereBetween('created_at', [$startOfYear, $now])
                ->get()
                ->sortBy('created_at');
            if (! $invoices->first()) {
                $calculatedId = 1;
            } else {
                $calculatedId = $record->id - $invoices->first()->id + 1;
            }
            $currentYear = (string) now()->year;
            $state = $currentYear.'-'.sprintf('%03d', $calculatedId);

            return $state;
        })
Was this page helpful?