Testing Laravel (5.1) console commands with phpunit -


what best way test laravel console commands?

here example of command i'm running. takes in value in constructor , in handle method.

class dosomething extends command {     protected $signature = 'app:do-something';     protected $description = 'does something';      public function __construct(a $a)     {         ...     }      public function handle(b $b)     {         ...         } } 

in test class, can mock both , b, can't figure out how pass $a in.

$this->artisan('app:do-something', [$b]); 

is possible? or going wrong? should pass in thought handle() method?

thanks.

you have change around how call command in testing, possible mock object passed through.

if class used artisan dependency-injected this:

public function __construct(actualobject $mocked_a) {     // } 

then write test case this:

$mocked_a = mockery::mock('actualobject'); $this->app->instance('actualobject', $mocked_a);  $kernel = $this->app->make(illuminate\contracts\console\kernel::class); $status = $kernel->handle(     $input = new symfony\component\console\input\arrayinput([         'command' => 'app:do-something',     ]),     $output = new symfony\component\console\output\bufferedoutput ); $console_output = $output->fetch(); 

the $this->app->instance('actualobject', $mocked_a); line able call upon , use mocked version of class, or object, instead of actual.

this work in laravel or lumen.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -