murilo's avatar
Level 10

can I set a global variable when I run test to know if my seed is running test or not ?

hello , I am testing with pest . and I have many seeder files ( many seeder child files ). I dont know if is a good Idea to create a global variable to know if I am testing or not . in those files sometimes , I need to run a different seed if is testing . but it has many child files , I wold have to pass the value to all of them to know that I am testing or not .

maybe I could add it in a global variable that I am testing or not . to do something like this -

if(IS_TESTING) {
	  User::factory()->count(20)->create()->each(function($user)   {
            $tasks = [
                    SaveUserRoleRole::class,
                    SavePresentation::class     ];

            foreach ($tasks as $task){
                (new $task)->handle($user);
            }
	}

if(IS_NOT_TESTING_TESTING) {
User::factory()->count(20)->create()->each(function($user)   {
            $tasks = [
                    SaveUserRoleRole::class,
				    CreateFolder::class,
                    SavePresentation::class,
                    CreateImage::class                ];

            foreach ($tasks as $task){
                (new $task)->handle($user);
            }
	}


}

can I set a global variable when I start to seed to know that I am testing , and all those child classes will know that I am seeding test . cold I do that ?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, you can set a global variable to determine if you are running tests or not. In Laravel, you can use environment variables to achieve this. You can set an environment variable in your phpunit.xml file, which is used when running tests.

Here’s how you can do it:

  1. Set the Environment Variable in phpunit.xml:

    Open your phpunit.xml file and add an environment variable to indicate that you are running tests.

    <php>
        <env name="IS_TESTING" value="true"/>
    </php>
    
  2. Access the Environment Variable in Your Seeder:

    In your seeder files, you can check the value of this environment variable using Laravel's env helper function.

    if (env('IS_TESTING')) {
        User::factory()->count(20)->create()->each(function($user) {
            $tasks = [
                SaveUserRoleRole::class,
                SavePresentation::class
            ];
    
            foreach ($tasks as $task) {
                (new $task)->handle($user);
            }
        });
    } else {
        User::factory()->count(20)->create()->each(function($user) {
            $tasks = [
                SaveUserRoleRole::class,
                CreateFolder::class,
                SavePresentation::class,
                CreateImage::class
            ];
    
            foreach ($tasks as $task) {
                (new $task)->handle($user);
            }
        });
    }
    
  3. Ensure Environment Variable is Available Globally:

    To make sure the environment variable is available globally, you can set it in your AppServiceProvider or any other service provider that is loaded early in the request lifecycle.

    // In AppServiceProvider or any other service provider
    public function boot()
    {
        if (env('IS_TESTING')) {
            define('IS_TESTING', true);
        } else {
            define('IS_TESTING', false);
        }
    }
    

    With this setup, you can use the IS_TESTING constant anywhere in your application to check if you are running tests.

  4. Using the Constant in Your Seeder:

    Now, you can use the IS_TESTING constant in your seeder files.

    if (IS_TESTING) {
        User::factory()->count(20)->create()->each(function($user) {
            $tasks = [
                SaveUserRoleRole::class,
                SavePresentation::class
            ];
    
            foreach ($tasks as $task) {
                (new $task)->handle($user);
            }
        });
    } else {
        User::factory()->count(20)->create()->each(function($user) {
            $tasks = [
                SaveUserRoleRole::class,
                CreateFolder::class,
                SavePresentation::class,
                CreateImage::class
            ];
    
            foreach ($tasks as $task) {
                (new $task)->handle($user);
            }
        });
    }
    

By following these steps, you can set a global variable to determine if you are running tests and use it in your seeder files to conditionally execute different logic.

Please or to participate in this conversation.