Laravel 5.2 で以下のようなコンソールアプリを作りました。動作としては問題ないように見えます。
bash
1$ ./artisan goodexample:start 2 3 [RuntimeException] 4 Either id or tag needed. 5 6$ ./artisan goodexample:start --id=1 7GoodExampleCommand called with id=1 8 9$ ./artisan goodexample:start --id=1 -v 10GoodExampleCommand called with id=1(verbose) 11 12$ ./artisan goodexample:start --undefined=1 13 14 [Symfony\Component\Console\Exception\RuntimeException] 15 The "--undefined" option does not exist.
ソースは以下の通りです。当初はコマンド定義として $signature を使っていましたが、今は $name と getOptions() に変えています。
php
1<?php 2 3namespace App\Console\Commands; 4 5use Illuminate\Console\Command; 6use Symfony\Component\Console\Input\InputOption; 7use RuntimeException; 8 9class GoodExampleCommand extends Command 10{ 11//protected $signature = 'example:start {--id=} {--tag=}'; 12 protected $name = 'goodexample:start'; 13 protected $description = 'Run GoodExample'; 14 public function __construct() 15 { 16 parent::__construct(); 17 } 18 public function handle() 19 { 20 $id = $this->option('id'); 21 $tag = $this->option('tag'); 22 if (!$id && !$tag) { 23 throw new RuntimeException('Either id or tag needed.'); 24 } 25 $this->info(sprintf("GoodExampleCommand called with %s=%s%s", 26 $id ? 'id':'tag', $id ? $id : $tag, 27 $this->getOutput()->isVerbose() ? '(verbose)' : '')); 28 } 29 protected function getOptions() 30 { 31 return [ 32 [ 'id', 'i', InputOption::VALUE_REQUIRED, 'desc of id', null ], 33 [ 'tag', 't', InputOption::VALUE_REQUIRED, 'desc of tag', null ], 34 ]; 35 } 36}
ところがテストでは、デフォルトで使える -v が未定義オプション扱いになってしまいます。
bash
1$ phpunit tests/GoodExampleCommandTest.php 2PHPUnit 5.6.0 by Sebastian Bergmann and contributors. 3 4...E 4 / 4 (100%) 5 6Time: 130 ms, Memory: 10.00MB 7 8There was 1 error: 9 101) GoodExampleCommandTest::testGoodExampleCommandWithIdVerboseWillSuccess 11Symfony\Component\Console\Exception\InvalidOptionException: The "-v" option does not exist. 12 13/var/www/laravel/vendor/symfony/console/Input/ArrayInput.php:154 14/var/www/laravel/vendor/symfony/console/Input/ArrayInput.php:136 15/var/www/laravel/vendor/symfony/console/Input/Input.php:62 16/var/www/laravel/vendor/symfony/console/Command/Command.php:221 17/var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php:155 18/var/www/laravel/tests/GoodExampleCommandTest.php:53 19/var/www/laravel/tests/GoodExampleCommandTest.php:45 20 21ERRORS! 22Tests: 4, Assertions: 4, Errors: 1.
テスト側のソースは以下の通りです:
php:GoodExampleCommandTest.php
1<?php 2 3use Illuminate\Foundation\Testing\WithoutMiddleware; 4use App\Console\Commands\GoodExampleCommand; 5use Symfony\Component\Console\Output\BufferedOutput; 6use Symfony\Component\Console\Input\ArrayInput; 7 8class GoodExampleCommandTest extends TestCase 9{ 10 public function setUp() 11 { 12 parent::setUp(); 13 $this->command = new \App\Console\Commands\GoodExampleCommand; 14 $this->command->setLaravel($this->app); 15 } 16 17 /** example:start - no options 18 * @expectedException RuntimeException 19 * @expectedExceptionMessage Either id or tag needed. 20 */ 21 public function testGoodExampleCommandWithoutMandatoryOptionsWillFail() 22 { 23 $output = $this->execute(); 24 } 25 26 /** example:start - id specified */ 27 public function testGoodExampleCommandWithIdWillSuccess() 28 { 29 $output = $this->execute(['--id' => 'id1']); 30 $this->assertEquals('GoodExampleCommand called with id=id1', 31 trim($output->fetch())); 32 } 33 34 /** example:star - tag specified */ 35 public function testGoodExampleCommandWithTagWillSuccess() 36 { 37 $output = $this->execute(['--tag' => 'tag1']); 38 $this->assertEquals('GoodExampleCommand called with tag=tag1', 39 trim($output->fetch())); 40 } 41 42 /** example:start - id specified (verbose) */ 43 public function testGoodExampleCommandWithIdVerboseWillSuccess() 44 { 45 $output = $this->execute(['--id' => 'id1', '-v' => null ]); // ここが通らない 46 $this->assertEquals('GoodExampleCommand called with id=id1(verbose)', 47 trim($output->fetch())); 48 } 49 50 protected function execute(array $params = []) 51 { 52 $output = new BufferedOutput(); 53 $this->command->run( new ArrayInput($params), $output); 54 return $output; 55 } 56 57}
CLI に関するテストの書き方は書籍(LaravelリファレンスVer.5.1 LTS対応)を参考にしていますが、Symfony のコンポーネントを使って実行する部分については、正直あまり理解できておりません。何かアドバイスがありましたらお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/14 01:30