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

PetroGromovo's avatar

How in testing switch DatabaseTransactions off/on on condition?

Hello, In laravel 5.8 application I use test with DatabaseTransactions option, so inserted data in testing db are cleared after testing, like:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use DB;
use Illuminate\Foundation\Testing\WithFaker;

use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\User;
use App\UserGroup;
use App\Http\Traits\funcsTrait;

class ProfilePageTest extends TestCase  // vendor/bin/phpunit   tests/Feature/ProfilepageTest.php
{
    use funcsTrait;

    use DatabaseTransactions; // TO COMMENT THESE LINE IF WE NEED TO CHECK CREATED DATA IN BD

    public function testProfilePage()
    {
...

and that works ok, but I need in some case to switch off DatabaseTransactions option without modifing php testing files. But to change some option when relative key test_without_database_transactions in db.setting_table is set to true. If there is a way to make it, like condition :

if ( check value in in db.setting_table.test_without_database_transactions ) {
use Illuminate\Foundation\Testing\DatabaseTransactions;
} 

use App\User;
use App\UserGroup;
use App\Http\Traits\funcsTrait;

class ProfilePageTest extends TestCase  // vendor/bin/phpunit   tests/Feature/ProfilepageTest.php
{
    use funcsTrait;

if ( check value in in db.setting_table.test_without_database_transactions ) {
    use DatabaseTransactions; // TO COMMENT THESE LINE IF WE NEED TO CHECK CREATED DATA IN BD
}

Can it be done in someway?

Thanks!

0 likes
5 replies
PetroGromovo's avatar

I found that DatabaseTransactions.php functionality is implemented in vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseTransactions.php file. Seems there is a way of coping this file into my project under /app/ and modifing it with comdition code, but I am not sure in which subdirecory that file must be copied?

Tray2's avatar

You can also use RefreshDatabase which refreshes the database every time.

PetroGromovo's avatar

I know about RefreshDatabase - that is not my case and I have some demo initial data and make make my test with these data. Resfreshing them all takes some time. I think to use and modify vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseTransactions.php file in my /app/, but I am not sure how correctly to make it.

PetroGromovo's avatar

Sorry, but could you remind how to copy vendor/laravel/framework/src/Illuminate/Foundation/Testing/DatabaseTransactions.php file in my /app/ subdirectory, but I am not sure how correctly to make it ? I do not know how this feature is entitles and failed to find the decision in the net...

Tray2's avatar

I'm using an in memory sqllite dayabase for my tests and so far I have 159 tests and most of those are using RefreshDatabase and they take about 2.5s to run. So unless you use hundreds+ rows for your tests you don't need to worry about performance.

PHPUnit 8.3.4 by Sebastian Bergmann and contributors.

...............................................................  63 / 159 ( 39%)
............................................................... 126 / 159 ( 79%)
.................................                               159 / 159 (100%)

Time: 2.52 seconds, Memory: 34.00 MB

OK (159 tests, 451 assertions)

Please or to participate in this conversation.