<?php

namespace Tests\Traits;

use Illuminate\Support\Facades\DB;

trait DatabaseTransactions
{
    /**
     * Database connections to use for transactions.
     *
     * @var array
     */
    protected array $connections = ['mysql','mysql7'];

    /**
     * Begin transactions on all specified database connections.
     */
    protected function setUp(): void
    {
        parent::setUp();

        foreach ($this->connections as $connection) {
            DB::connection($connection)->beginTransaction();
        }
    }

    /**
     * Rollback transactions on all specified database connections.
     */
    protected function tearDown(): void
    {
        foreach ($this->connections as $connection) {
            DB::connection($connection)->rollBack();
        }

        parent::tearDown();
    }

    /**
     * Set the database connections for transactions.
     *
     * @param string|array $connections
     * @return void
     */
    protected function setTransactionConnections(array $connections): void
    {
        $this->connections = $connections;
    }
}