Laravel/Lumenでレスポンスヘッダーのアサートをする
Illuminate\Foundation\Testing\Concerns\MakesHttpRequests
に実装されてるseeHeader
メソッドを使う。
framework/MakesHttpRequests.php at 5.2 · laravel/framework · GitHub
ヘッダーが存在する事を確認する
例えば/hoge
にアクセスした時にX-Hoge
ヘッダが存在するかを確認するときには以下のようにする。
<?php class HogeTest extends TestCase { public function testHoge { $this->visit("/hoge"); // "/hoge"へアクセス $this->seeHeader("X-Hoge"); } }
ヘッダーが存在して、値が同じな事を確認する
例えば/hoge
にアクセスした時にX-Hoge
ヘッダが存在して、値がfoo-bar
であることをアサートするときには以下のようにする。
<?php class HogeTest extends TestCase { public function testHoge { $this->visit("/hoge"); // "/hoge"へアクセス $this->seeHeader("X-Hoge", "foo-bar"); // アサート } }
Laravelのコードを読んでいたらseeHeader
メソッドを見つけたので書き直しました。
LaravelのartisanコマンドでgetArguments()やgetOptions()を使う時はコマンド名の指定に$nameを使う必要がある
LaravelのArtisanで自作Commandクラスを使う時に躓いたのでメモ
LaravelでArtisanコマンドを作る時に以下のようにコマンド名を指定する($signature
を使う)とgetArguments()
やgetOptions()
が読まれない。
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class HogeCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = "test:hoge"; /** * The console command description. * * @var string */ protected $description = "Test command"; /** * Execute the console command. * * @return mixed */ public function handle() { dd($this->argument()); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ["files", InputArgument::IS_ARRAY, "Files"], ]; } }
これでtest:hoge
コマンドを実行してもfiles
引数は読まれない。
理由
HogeCommand
内でコマンド名の指定に$signature
を使っているから。
$signature
での指定は以下のリンクにあるように、$signature
で名前だけでなく引数やオプションも指定する時に使う。
http://readouble.com/laravel/5/1/ja/artisan.html
修正する
$signature
では無く$name
でコマンド名を定義すればgetArguments()
やgetOptions()
が読まれる。
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class HogeCommand extends Command { /** * The name of the console command. * * @var string */ protected $name = "test:hoge"; /** * The console command description. * * @var string */ protected $description = "Test command"; /** * Execute the console command. * * @return mixed */ public function handle() { dd($this->argument()); } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return [ ["files", InputArgument::IS_ARRAY, "Files"], ]; } }
これでfiles
引数が動く