skip to Main Content

I’m looking a solution to clear cache in Laravel, before starting all the tests (please note I mean ALL tests, not every single test):

    $this->artisan('cache:clear');
    $this->artisan('route:clear');
    $this->artisan('config:clear');

P.S. Laravel 9 and PHPUnit 9.5

3

Answers


  1. Add

    protected function setUp(): void
    {
        parent::setUp();
        $this->artisan('cache:clear');
        $this->artisan('route:clear');
        $this->artisan('config:clear');
    }
    

    in tests/TestCase.php

    Login or Signup to reply.
  2. You can do this by creating your own TestCase (or edit the original TestCase,php), and add these tasks to the createApplication method:

    use IlluminateFoundationTestingTestCase as BaseTestCase;
    use IlluminateSupportFacadesArtisan;
    
    abstract class TestCase extends BaseTestCase
    {
        use CreatesApplication;
    
        public function createApplication()
        {
            $app = require __DIR__.'/../bootstrap/app.php';
    
            $app->make(IlluminateContractsConsoleKernel::class)->bootstrap();
            
            Artisan::call('cache:clear');
            Artisan::call('route:clear');
            Artisan::call('config:clear');
    
            return $app;
        }
    }
    

    Each test should extend this TestCase instead of IlluminateFoundationTestingTestCase

    You can also consider not to cache routes and configuration in your development environment. In your phpunit.xml you can also set the cache driver that is used during testing to a different one than that is used during development, e.g.:

    <phpunit 
        ...>
        ...
        <php>
            ...
            <env name="CACHE_DRIVER" value="array"/>
            ...
        </php>
    </phpunit>
    

    That would drop the necessity to clear the caches before running your tests.

    In this answer you can read more about configuration caching during testing. There you can also find a link to a GitHub issue that talks about clearing caches before running tests.

    Login or Signup to reply.
  3. As I known, no such way to do this, because this is not the range of unit tests.

    Unit tests designed to setting same environment for each tests, environment of all tests is not its responsibility.

    But you can still work around by make a custom command to call config:clear and test.

    1. Make a custom command.

    php artisan make:command test

    2. Edit your test command.

    Default path will be app/Console/Commands/test.php.

    ...
    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->call('cache:clear');
        $this->call('route:clear');
        $this->call('config:clear');
        $this->call('test');
    }
    

    3. Call unit tests by your own test command.

    If you didn’t change the default $signature, app:test will be the default command you just created.

    Run php artisan app:test to call unit tests.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search