1

I need to do a check before launching the php artisan test command.

I need this because I currently have 2 databases for DEV, one local and one shared with my collaborators. I want to prevent the tests from running on the shared database.

I already saw that I could create a custom command with Laravel but I would like to know if there was a better solution ...

Thanks in advance for your feedback

PewixG3
  • 13
  • 3

1 Answers1

1

If you have .env.testing file & define specific database name & credentials there, then test will use this file. For more restriction you can add below code block on your TestCase abstract class & extends that class on all of your Test Case classes.

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function setUp() :void
    {
        parent::setUp();
        
        if ( !file_exists(__DIR__.'/../.env.testing') ) {
            throw new \Exception('!!! Create .env.testing file !!!');
        }
    }
}     

And in your test case classes

class AuthTest extends TestCase
{
}
Manash Kumar
  • 995
  • 6
  • 13
  • Thank you for this quick answer! I just saw that I have an .env.testing file with the right information but it doesn't work I'm using PEST (I forgot to mention it I'm sorry). I saw that you have to run with the --env=testing option for it to work but that's not my goal – PewixG3 Feb 23 '23 at 07:16
  • Finally I used the TestCase class and I was able to put a check that blocks the launch! Thanks a lot! – PewixG3 Feb 23 '23 at 07:51