0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】 独自のカスタムコマンドを作成したときのテスト

Posted at

環境

Laravel v9.5.1 (PHP v8.1.3)

独自のカスタムコマンドを作成したときのテスト

下記コマンドを作成。

php artisan command:sample
<?php

namespace App\Console\Commands;

class SampleSchedule extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'command:sample';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'sample';

  /**
   * Execute the console command.
   *
   * @return int
   */
  public function handle()
  {
   // コマンドを実行したときの処理
  }
}

$this->artisan('command:sample')でコマンドを即時実行できる。

tests/Feature/CommandTest.php
// テストデータ用意
...

// コマンド実行
$this->artisan('command:sample')->assertSuccessful();

// コマンド実行後の結果
...

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?