Home>

I'm making tests with Laravel and PHPUnit.
use RefreshDatabase;DB is reset before each test because I am using, but AI (auto increment) is not reset. Therefore, I try to truncate, but I get an error.

tearDown ()I wrote it every time at the end of the function of the test without using, and it worked without error. But I want to write in tearDown ().

/ **
     * Test to succeed
     *
     * @return void
     * /
    public function testSuccessfull (): void
    {
      <Omitted>
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 0;');
        DB :: table ('users')->truncate ();
        DB :: table ('chats')->truncate ();
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 1;');
    }
    /**
     * Test to fail
     *
     * @return void
     * /
    public function testFailed (): void
    {
<Omitted>
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 0;');
        DB :: table ('users')->truncate ();
        DB :: table ('chats')->truncate ();
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 1;');
    }
Call to undefined method Illuminate \ Support \ Facades \ DB :: statement ()
Corresponding source code
<? php
namespace Tests \ Feature;
use Illuminate \ Foundation \ Testing \ RefreshDatabase;
use Illuminate \ Foundation \ Testing \ WithFaker;
use Illuminate \ Testing \ TestResponse;
use Tests \ TestCase;
use App \ Models \ User;
use App \ Models \ Skill;
use Illuminate \ Support \ Facades \ DB;
class GetTest extends TestCase
{
    use RefreshDatabase;
   /**
     * Add test data to test DB
     *
     * @return void
     * /
    public function setup (): void
    {
        parent :: setUp ();
        Skill :: factory ()
            ->count (20)
            ->for (User :: factory ()->state ([
                'id' =>1,
            ]))
            ->create ();
    }
   /**
     * truncate
     *
     * @return void
     * /
    public function teardown (): void
    {
        parent :: tearDown ();
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 0;');
        DB :: table ('users')->truncate ();
        DB :: table ('skills')->truncate ();
        DB :: statement ('SET FOREIGN_KEY_CHECKS = 1;');
    }
   /**
     * Test to succeed
     *
     * @return void
     * /
    public function testSuccessfull (): void
    {
    }
    /**
     * Test to fail
     *
     * @return void
     * /
    public function testFailed (): void
    {
    }
}
Supplementary information (FW/tool version, etc.)

laravel8
phpunit9
mysql 8

  • Answer # 1

    I think it's because the application is closed.
    When tearing down, after doing the necessary processing, not first
    Will calling parent :: tearDown ();resolve?

      public function teardown (): void
        {
            DB :: statement ('SET FOREIGN_KEY_CHECKS = 0;');
            DB :: table ('users')->truncate ();
            DB :: table ('skills')->truncate ();
            DB :: statement ('SET FOREIGN_KEY_CHECKS = 1;');
            parent :: tearDown ();// ← Move here
        }

    Or, if SQLite in-memory is used instead of MySQL, the serial number will be restored for each test method, so you do not have to take the above measures. In that case, please uncomment the following two places in phpunit.xml.

        <!-<server name = "DB_CONNECTION" value = "sqlite" />->
          <!-<server name = "DB_DATABASE" value = ": memory:" />->

    As another measure, I think it is good to write a test so that it does not depend on the serial number.