FarhadMohammadi's avatar

How to test update artisan commands?

I have an artisan command like below

	public function handle(): void
  {
        info('[AdvertisementCommand][handle][Start]');

        Advertisement::expired()->update(['status' => Advertisement::STATUS_EXPIRED]);

         info('[AdvertisementCommand][handle][Done]');
  }

expired scope just select rows with ended_at < now() How can I write a test for this? Is it need to test or not?

0 likes
5 replies
Sinnbeck's avatar
  1. Add some test data with another status
  2. Run the command in a test
  3. Check if the data was changed

If there is any chance that it could ever be changed and break, then test it (I assume this is the case) :)

1 like
FarhadMohammadi's avatar

@Sinnbeck so true, but the point is How I check the data was changed? for example: assertEquals(true number of rows, affecredRows)? How I get affected rows?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@FarhadMohammadi Lets say you add 2 records to the database. 1 that is "expired" and 1 that isnt. Then you run the command. Then you check the adverticements. You can keep the id's in the test when you create them if you want

$this->assertEquals($expiredAdverticement->refresh()->status,  Advertisement::STATUS_EXPIRED);
$this->assertEquals($dverticement->refresh()->status,  Advertisement::STATUS_ACTIVE);
1 like

Please or to participate in this conversation.