wonder95's avatar

Model created using ->create() not persisted to db in PHPUnit test

I'm in the process of updating a large Laravel/MongoDb/Vue app from Laravel 9 to 10, and also converting from Jenssegers\Mongodb to MongoDb\Laravel and after quite a bit of work, I have only one PHPUnit test that is failing. The issue that I can't figure out is why in the test, a model is not being persisted to the database when using ->create() to create it. The test does this:

$masFMO = factory(FederalMetaOpportunityModel::class)->create(['_id' => FederalContractOpportunityModel::MAS_ID]);

Per the docs, the model should be persisted to the db, but right after that, if I try to refresh the model from the db

$masFMO->refresh();

I get an error No query results for model [App\Models\FederalMetaOpportunityModel]. The initial fail that brought this to my attention comes later in a getter where it tries to access this model from a related model.

This exact same test runs fine on Laravel 9 and Jenssegers\Mongodb. I have been hunting around, but haven't been able to find any similar reports.

Has anyone else run into this issue?

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue with the persistence of a model in your PHPUnit test after upgrading to Laravel 10 and switching from jenssegers/mongodb to mongodb/laravel. Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Database Connection in Testing Environment: Ensure that your .env.testing file or the testing configuration in phpunit.xml is set up correctly with the right database connection details.

  2. Database Transactions: Verify if you are using database transactions in your tests. If you are, make sure that you're not rolling back the transaction before attempting to refresh the model.

  3. Factory Compatibility: Laravel 10 might have some changes in how factories work. Ensure that your factory is compatible with Laravel 10. You might need to update your factory definition.

  4. Eloquent Refresh Method: The refresh() method re-retrieves the model from the database. If the model isn't found, it throws a ModelNotFoundException. Make sure that the model is actually being saved in the database.

  5. Debugging: Add debugging statements before the refresh() call to check if the model exists in the database. You can use DB::getQueryLog() to log the queries and see if the insert operation is actually being performed.

  6. MongoDB Laravel Package: Since you've switched to a different MongoDB package, ensure that all configurations and usage are in line with the new package's requirements. Check the documentation for any changes in syntax or behavior.

  7. Check Model's boot Method: If your model has a boot method, ensure that it's not interfering with the creation of the model. Sometimes global scopes or other global actions can prevent a model from being saved correctly.

  8. PHPUnit Version: Ensure that the PHPUnit version you're using is compatible with Laravel 10. Incompatibilities might cause unexpected behavior.

  9. Laravel's create Method: Double-check the attributes you're passing to the create method. Ensure that they are fillable in the model or unset the $guarded property temporarily for testing.

Here's a sample code snippet to help you debug the issue:

// Before the refresh, check if the model exists in the database.
$exists = FederalMetaOpportunityModel::find($masFMO->_id);
if ($exists) {
    echo "Model exists in the database.\n";
} else {
    echo "Model does not exist in the database.\n";
}

// Attempt to refresh the model.
try {
    $masFMO->refresh();
    echo "Model refreshed successfully.\n";
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
    echo "Failed to refresh the model: " . $e->getMessage() . "\n";
}

// Enable query log for debugging.
\DB::enableQueryLog();

// Your test code here...

// Dump the query log to see what's happening.
dd(\DB::getQueryLog());

If none of these steps resolve the issue, it might be helpful to check the GitHub issues or forums for the mongodb/laravel package for any known issues or to report this as a new issue.

nexxai's avatar

Curious if you see the same thing by doing

$masFMO = FederalMetaOpportunityModel::factory()->create(['_id' => FederalContractOpportunityModel::MAS_ID]);

instead of using the global factory() helper

Please or to participate in this conversation.