How to wrap whole class in transaction in PHPUnit?

Viewed 326

My idea is to specify in each test class parameters, depends how we want to wrap test in transactions. I use Laravel 5.1.

use DB;

class TestCase
{
    protected static $wrapMethodInTransaction = true;
    protected static $wrapClassInTransaction = false;

    /**
     * @beforeClass
     */
    public static function isClassWrappedInTransactionBefore()
    {
        if (!static::$wrapMethodInTransaction && static::$wrapClassInTransaction) {
            DB::beginTransaction();
        }
    }

    /**
     * @afterClass
     */
    public static function isClassWrappedInTransactionAfter()
    {
       if (!static::$wrapMethodInTransaction && static::$wrapClassInTransaction) {
            DB::rollback();
       }
    }
}

The case is that @beforeClass & @afterClass fires when application is not booted yet/already dead, thus class DB is not accesible. I just want to run those methods once per class, but when application is booted/not killed yet. Are there any solutions for this?

DatabaseTransactions trait does not do the work, because it starts transaction after setUp() and rollbacks it after tearDown(). Laravel's 'magic'. I want the setUp() to be in transaction.

0 Answers
Related