Testing a side effect of `$this->artisan('schedule:run')` command.
A bit of background
There are occasions where our application is required to be in a demo mode. Enabling demo mode (either by env APP_DEMO or config demo.enabled (which checks for APP_DEMO otherwise false)) will also allow an expiration date to be entered. Data is stored in the Demo model.
An artisan command demo:expires has been built and tested directly in a unit test to ensure the results are what are expected (which they are). This artisan command has been added to the Kernel.php to allow schedule:run to invoke the demo:expires command on a given cron interval. Calling php artisan schedule:run via terminal does indeed invoke the command demo:expires and the results are as expected. Creating a unit test to test the schedule:run functionality to ensure the results are correct is where the issue occurs.
The issue
Calling $this->artisan('schedule:run') within a test doesn't seem/appear to be honouring any of the test env data running in a 'testing' environment but instead uses the local env data subsequently effecting the local database, calls to the database and the incorrect env data. This has been confirmed with dd(App::environment()); when calling the unit test, which results in 'local' as opposed to 'testing'. I understand this is a CLI call and could be running a different php process etc but any advise, help would be greatly appreciated.
Here is the test code being invoked:
/** @test */
public function it_refreshes_the_database_when_expired_from_schedule_worker()
{
putenv("APP_DEMO=true");
$user = $this->createLearner();
Demo::factory()->create();
$expiryDate = Demo::expires();
$this->travelTo($expiryDate);
$this->assertTrue(Demo::enabled());
$this->assertDatabaseHas('users', ['email' => $user->email]);
$this->artisan('schedule:run');
$this->assertFalse(Demo::enabled());
$this->assertDatabaseMissing('users', ['email' => $user->email]);
$this->assertDatabaseHas('users', ['email' => config('onboarding.support.user.email')]);
putenv("APP_DEMO=false");
}
Please or to participate in this conversation.