Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mushood's avatar
Level 41

Database connection and transactions

Hello

I am running phpunit test on my project and I have two issues:

  1. I can't seem to get DatabaseTransactions to work. Data is persisted and not rolled back. Test passes.
  2. I can't change the default database connection to another specific database.

Here are the relevant files:

  1. My test file:
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class CategoryTest extends TestCase
{
  use DatabaseTransactions;

  protected $connectionsToTransact = ['mysql','mysql_testing'];

   public function test_i_get_no_error_when_field_is_valid(){
      $response = $this->post('/category', array(
        '_token' => csrf_token(),
        'title' => "titles",
      ));
      $response->assertStatus(302);
      $response->assertSessionMissing('errors');
   }

the data title is persisted in the database. If i run php artisan migrate:refresh, the database is refreshed correctly.

  1. Controller
    public function store(Request $request)
    {
      $rules = [
        'title' => 'required|unique:category_translations|min:4|max:255|oneWord',
      ];

      $this->validate($request, $rules);

      $category = new Category;
      $category -> title = $request -> title;
      $category -> save();

      return redirect()->back();
    }

FOR MY SECOND ISSUE:

  1. my database.php file
'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'mysql_testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_TESTING', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
.
.
.

If i run php:artisan migrate --database mysql_testing , the command runs correctly.

  1. my phpunit.xml file
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="mysql_testing"/>
    </php>

Full project if needed : https://github.com/Mushood/BlogSphere

0 likes
2 replies
mushood's avatar
Level 41

My initial dev env was on windows 10. If i switch to DatabaseMigrations, no issues there. Works as expected.

I tried running the project in Ubuntu 14.04 and the DatabaseTransactions works fine. So the problem would not be in the code but in my setup. Does anyone have any ideas at what I should look at between the 2?

However, to make my tests run only on the mysql_testing db connection is still not working in Ubuntu. Any help here would also be appreciated.

Please or to participate in this conversation.