0

Making in laravel 9.48.0 with mysql database http tests after I added RefreshDatabase into test file

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;

use Tests\TestCase;
use App\Models\{Article, User};
use Illuminate\Support\Str;

class ArticlesCrudTest extends TestCase
{
    use InteractsWithExceptionHandling;
    use RefreshDatabase;

I got “There is no active transaction” error on 1st test from 15 tests in this file

I do not use sqllite, but other mysql database, so phpunit.xml have sqlite and memory options disabled:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
         printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinterForPhpUnit9"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <!-- <env name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <env name="DB_DATABASE" value=":memory:"/> -->
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

My control have code :

\Log::info( ' -1 store::');
DB::beginTransaction();
try {
    $article = Article::create([
        'title'        => $data['title'],
        'text'         => $data['text'],
        'text_shortly' => $data['text_shortly'],
        'creator_id'   => $data['creator_id'],
        'published'    => $data['published'],
    ]);
    \Log::info( ' -2 store::');
    DB::Commit();

    $article->load('creator');

    return response()->json(
        ['article' => (new ArticleResource($article))],
        HTTP_RESPONSE_OK_RESOURCE_CREATED
    ); // 201
} catch (\Exception $e) {
    DB::rollback();
}

In log file there are 2 lines from this controller. Before I added RefreshDatabase these tests worked ok, just like in real work.

What is wrong ?

Thanks!

mstdmstd
  • 2,195
  • 17
  • 63
  • 140

0 Answers0