The issue you're encountering is likely due to the way SQLite handles certain SQL operations differently compared to other databases like MySQL or PostgreSQL. Specifically, the combination of selectRaw and with might be causing SQLite to misinterpret or mishandle the query, leading to the "Cannot declare class" error.
Here are a few steps you can take to troubleshoot and potentially resolve this issue:
-
Check for Namespace Conflicts: Ensure that there are no namespace conflicts or duplicate class declarations in your codebase. This error can sometimes be caused by autoloading issues or duplicate class names.
-
Simplify the Query for Testing: Since the issue only occurs during testing with SQLite, you can try simplifying the query to isolate the problem. For example, you can remove the
selectRawpart and see if the issue persists. -
Use Eloquent Relationships: Instead of using
selectRaw, you can try to achieve the same result using Eloquent relationships and query builder methods. This might help SQLite handle the query better. -
Custom Testing Database Configuration: Consider using a different database for testing that is more similar to your production environment. This can help avoid SQLite-specific issues.
Here's a modified version of your method that avoids using selectRaw and instead uses Eloquent relationships and query builder methods:
public function territoryRevenues()
{
return $this->revenues()
->join('series', 'revenues.source_id', '=', 'series.id')
->select([
DB::raw("'App\\\\Territory' as source_type"),
'series.territory_id as id',
'series.territory_id as source_id',
DB::raw('SUM(revenue_total) as revenue_total')
])
->groupBy('series.territory_id')
->with(['source' => function ($query) {
$query->withTrashed();
}]);
}
If the above solution still doesn't work, you can try breaking down the query into smaller parts and testing each part individually to identify the exact cause of the issue.
Additionally, you can create a custom testing database configuration in your phpunit.xml file to use a different database for testing:
<php>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="test_database"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>
</php>
This way, you can avoid SQLite-specific issues and ensure that your tests run in an environment similar to production.
I hope this helps! Let me know if you have any further questions or need additional assistance.