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

mikail10000000's avatar

After running php artisan config:cache phpunit issue

Hi, I'm not 100% sure but I guess after running php artisan config:cache in testing mode I get an error about 'PRAGMA foreign_keys=on;' (I use SQLite for testing env ) , before that everything was running fine, I tried individual test and has same error, after I remove the DB::statement('PRAGMA foreign_keys=on;') from my setUp() the test started to work but extremely slow, it used to take 7 seconds to run all my 100 tests but now it takes about 30 minutes , the app itself works fine, here is my TestCase class:



<?php

namespace Tests;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\DB;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
     protected function setUp()
    {
        parent::setUp();
        DB::statement('PRAGMA foreign_keys=on;');
        $this->disableExceptionHandling();
        \App\Permission::generatePermissions();
    }
  
    
    protected function ajax($type,$uri, array $data = [])
    {
        return $this->json($type, $uri, $data, array('HTTP_X-Requested-With' => 'XMLHttpRequest'));
    }


    // Hat tip, @adamwathan.
    protected function disableExceptionHandling()
    {
        $this->oldExceptionHandler = $this->app->make(ExceptionHandler::class);

        $this->app->instance(ExceptionHandler::class, new class extends Handler {
            public function __construct() {}
            public function report(\Exception $e) {}
            public function render($request, \Exception $e) {
                throw $e;
            }
        });
    }

    protected function withExceptionHandling()
    {
        $this->app->instance(ExceptionHandler::class, $this->oldExceptionHandler);

        return $this;
    }
}


this is the error: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PRAGMA foreign_keys=1' at line 1

my phpunit settings:


 <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>

        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>

Please help

0 likes
2 replies
Cronix's avatar

try

DB::statement(DB::raw('PRAGMA foreign_keys=1'));

Please or to participate in this conversation.