The issue seems to be related to the fact that the now() function in the second test is using Carbon, which in turn is trying to resolve a database connection. One possible solution is to mock the now() function to return a fixed date and time, instead of relying on Carbon to calculate it dynamically. This can be achieved using the Carbon::setTestNow() method.
Here's an updated version of the second test that uses this approach:
use Carbon\Carbon;
test('token is valid if valid_until is later than the current date and time', function () {
$token = new UserToken();
$token->value = Str::random(32);
$token->type = TokenType::VERIFICATION;
$token->valid_until = Carbon::now()->addDays(1);
Carbon::setTestNow($token->valid_until->subSeconds(1)); // set "now" to one second before valid_until
expect($token->isValid())->toEqual(false);
Carbon::setTestNow($token->valid_until); // set "now" to valid_until
expect($token->isValid())->toEqual(true);
Carbon::setTestNow(); // reset "now" to the real current date and time
});
This test sets the "now" time to one second before the valid_until time, and checks that the token is not valid at that point. Then it sets the "now" time to the valid_until time, and checks that the token is valid at that point. Finally, it resets the "now" time to the real current date and time.
Note that this approach requires the Carbon class to be imported at the top of the test file:
use Carbon\Carbon;