The issue you're encountering is likely due to the state of the database between test runs. When running tests locally, the database might not be reset properly between tests, causing tables to already exist. On GitHub Actions, the environment is fresh each time, so the tables do not exist initially.
Here are a few steps to ensure that your tests run consistently both locally and on GitHub Actions:
-
Ensure Database Reset Between Tests: Use the
RefreshDatabasetrait to ensure that the database is reset between tests. This trait will handle rolling back the database after each test. -
Check Migration Loading: Ensure that migrations are loaded correctly and only once. You might be loading migrations multiple times, causing the "tables already exist" error.
-
Use
RefreshDatabaseCorrectly: Ensure that theRefreshDatabasetrait is used correctly to reset the database state.
Here's a revised version of your TestCase class:
<?php
namespace Soap\WorkflowStorage\Tests;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
use Soap\WorkflowStorage\WorkflowStorageServiceProvider;
use function Orchestra\Testbench\workbench_path;
class TestCase extends Orchestra
{
use RefreshDatabase;
use WithWorkbench;
protected function setUp(): void
{
parent::setUp();
Factory::guessFactoryNamesUsing(function (string $modelName) {
if (Str::startsWith($modelName, 'Workbench\\App\\Models\\')) {
// Factories within the tests directory
return 'Workbench\\Database\\Factories\\'.class_basename($modelName).'Factory';
}
// Factories within the package directory
return 'Soap\\WorkflowStorage\\Database\\Factories\\'.class_basename($modelName).'Factory';
});
// Load package migrations
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
protected function getPackageProviders($app)
{
return [
WorkflowStorageServiceProvider::class,
];
}
public function getEnvironmentSetUp($app)
{
// Set up the environment for testing
$app['config']->set('database.default', 'testing');
}
/**
* Define database migrations.
*
* @return void
*/
protected function defineDatabaseMigrations()
{
// Load workbench migrations
$this->loadMigrationsFrom(workbench_path('database/migrations'));
}
}
Additional Tips:
-
Check Database Configuration: Ensure that your
phpunit.xmlorphpunit.xml.distfile is correctly configured to use thetestingdatabase. -
Clear Cache: Sometimes, cached configurations or migrations can cause issues. Clear the cache before running tests:
php artisan config:clear php artisan cache:clear php artisan migrate:reset -
GitHub Actions Configuration: Ensure that your GitHub Actions workflow is correctly setting up the database. Here is an example of a typical setup:
name: Run Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest services: mysql: image: mysql:5.7 env: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: test ports: - 3306:3306 options: >- --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 steps: - uses: actions/checkout@v2 - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: 7.4 - name: Install Dependencies run: composer install --prefer-dist --no-progress --no-suggest - name: Create .env file run: cp .env.example .env - name: Run Migrations run: php artisan migrate - name: Run Tests run: vendor/bin/phpunit
By following these steps, you should be able to ensure that your tests run consistently both locally and on GitHub Actions.