How To Test Artisan Commands in Laravel 5










30















I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.



Anyone an idea how to write it?










share|improve this question



















  • 1





    laravel.com/docs/master/artisan#calling-commands-via-code ?

    – andrewtweber
    Nov 9 '15 at 21:01











  • stackoverflow.com/questions/34814954/…

    – bernie
    Jan 15 '16 at 15:55











  • This was a better approach for me: stackoverflow.com/a/41122816/470749

    – Ryan
    Jun 23 '17 at 16:13















30















I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.



Anyone an idea how to write it?










share|improve this question



















  • 1





    laravel.com/docs/master/artisan#calling-commands-via-code ?

    – andrewtweber
    Nov 9 '15 at 21:01











  • stackoverflow.com/questions/34814954/…

    – bernie
    Jan 15 '16 at 15:55











  • This was a better approach for me: stackoverflow.com/a/41122816/470749

    – Ryan
    Jun 23 '17 at 16:13













30












30








30


1






I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.



Anyone an idea how to write it?










share|improve this question
















I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.



Anyone an idea how to write it?







laravel unit-testing laravel-5.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 '16 at 6:17









mnv

5,57253152




5,57253152










asked Nov 9 '15 at 14:51









Thomas.YuThomas.Yu

4431822




4431822







  • 1





    laravel.com/docs/master/artisan#calling-commands-via-code ?

    – andrewtweber
    Nov 9 '15 at 21:01











  • stackoverflow.com/questions/34814954/…

    – bernie
    Jan 15 '16 at 15:55











  • This was a better approach for me: stackoverflow.com/a/41122816/470749

    – Ryan
    Jun 23 '17 at 16:13












  • 1





    laravel.com/docs/master/artisan#calling-commands-via-code ?

    – andrewtweber
    Nov 9 '15 at 21:01











  • stackoverflow.com/questions/34814954/…

    – bernie
    Jan 15 '16 at 15:55











  • This was a better approach for me: stackoverflow.com/a/41122816/470749

    – Ryan
    Jun 23 '17 at 16:13







1




1





laravel.com/docs/master/artisan#calling-commands-via-code ?

– andrewtweber
Nov 9 '15 at 21:01





laravel.com/docs/master/artisan#calling-commands-via-code ?

– andrewtweber
Nov 9 '15 at 21:01













stackoverflow.com/questions/34814954/…

– bernie
Jan 15 '16 at 15:55





stackoverflow.com/questions/34814954/…

– bernie
Jan 15 '16 at 15:55













This was a better approach for me: stackoverflow.com/a/41122816/470749

– Ryan
Jun 23 '17 at 16:13





This was a better approach for me: stackoverflow.com/a/41122816/470749

– Ryan
Jun 23 '17 at 16:13












2 Answers
2






active

oldest

votes


















22














It is much easier now:



<?php

class YourCommandTest extends TestCase


public function testExample()

$this->artisan('command', ['param' => 'value']);








share|improve this answer


















  • 1





    Where is your assertion ? What do you assert for ?

    – Mkey
    Mar 8 '18 at 11:57











  • That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

    – André Castelo
    Jun 7 '18 at 20:27











  • @Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

    – Carlton
    Jul 13 '18 at 12:49


















39














Example of test





<?php

class YourCommandTest extends TestCase

public function testExample()

Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);

// If you need result of console output
$resultAsText = Artisan::output();

$this->assertTrue(true);








share|improve this answer




















  • 1





    Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

    – alariva
    Jun 25 '16 at 19:12







  • 1





    I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

    – mnv
    Jun 25 '16 at 20:25






  • 1





    @alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

    – mnv
    Jun 26 '16 at 12:51






  • 1





    I have not option processUncoveredFilesFromWhitelist="true"

    – mnv
    Jun 26 '16 at 19:58






  • 1





    I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

    – alariva
    Jun 27 '16 at 1:01










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33611788%2fhow-to-test-artisan-commands-in-laravel-5%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









22














It is much easier now:



<?php

class YourCommandTest extends TestCase


public function testExample()

$this->artisan('command', ['param' => 'value']);








share|improve this answer


















  • 1





    Where is your assertion ? What do you assert for ?

    – Mkey
    Mar 8 '18 at 11:57











  • That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

    – André Castelo
    Jun 7 '18 at 20:27











  • @Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

    – Carlton
    Jul 13 '18 at 12:49















22














It is much easier now:



<?php

class YourCommandTest extends TestCase


public function testExample()

$this->artisan('command', ['param' => 'value']);








share|improve this answer


















  • 1





    Where is your assertion ? What do you assert for ?

    – Mkey
    Mar 8 '18 at 11:57











  • That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

    – André Castelo
    Jun 7 '18 at 20:27











  • @Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

    – Carlton
    Jul 13 '18 at 12:49













22












22








22







It is much easier now:



<?php

class YourCommandTest extends TestCase


public function testExample()

$this->artisan('command', ['param' => 'value']);








share|improve this answer













It is much easier now:



<?php

class YourCommandTest extends TestCase


public function testExample()

$this->artisan('command', ['param' => 'value']);









share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 8 '17 at 11:50









Roma RushRoma Rush

4,0531914




4,0531914







  • 1





    Where is your assertion ? What do you assert for ?

    – Mkey
    Mar 8 '18 at 11:57











  • That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

    – André Castelo
    Jun 7 '18 at 20:27











  • @Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

    – Carlton
    Jul 13 '18 at 12:49












  • 1





    Where is your assertion ? What do you assert for ?

    – Mkey
    Mar 8 '18 at 11:57











  • That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

    – André Castelo
    Jun 7 '18 at 20:27











  • @Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

    – Carlton
    Jul 13 '18 at 12:49







1




1





Where is your assertion ? What do you assert for ?

– Mkey
Mar 8 '18 at 11:57





Where is your assertion ? What do you assert for ?

– Mkey
Mar 8 '18 at 11:57













That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

– André Castelo
Jun 7 '18 at 20:27





That will depend entirely on what you want to happen. There would be a setup before the $this->artisan() call and the assertions after it.

– André Castelo
Jun 7 '18 at 20:27













@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

– Carlton
Jul 13 '18 at 12:49





@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.

– Carlton
Jul 13 '18 at 12:49













39














Example of test





<?php

class YourCommandTest extends TestCase

public function testExample()

Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);

// If you need result of console output
$resultAsText = Artisan::output();

$this->assertTrue(true);








share|improve this answer




















  • 1





    Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

    – alariva
    Jun 25 '16 at 19:12







  • 1





    I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

    – mnv
    Jun 25 '16 at 20:25






  • 1





    @alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

    – mnv
    Jun 26 '16 at 12:51






  • 1





    I have not option processUncoveredFilesFromWhitelist="true"

    – mnv
    Jun 26 '16 at 19:58






  • 1





    I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

    – alariva
    Jun 27 '16 at 1:01















39














Example of test





<?php

class YourCommandTest extends TestCase

public function testExample()

Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);

// If you need result of console output
$resultAsText = Artisan::output();

$this->assertTrue(true);








share|improve this answer




















  • 1





    Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

    – alariva
    Jun 25 '16 at 19:12







  • 1





    I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

    – mnv
    Jun 25 '16 at 20:25






  • 1





    @alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

    – mnv
    Jun 26 '16 at 12:51






  • 1





    I have not option processUncoveredFilesFromWhitelist="true"

    – mnv
    Jun 26 '16 at 19:58






  • 1





    I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

    – alariva
    Jun 27 '16 at 1:01













39












39








39







Example of test





<?php

class YourCommandTest extends TestCase

public function testExample()

Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);

// If you need result of console output
$resultAsText = Artisan::output();

$this->assertTrue(true);








share|improve this answer















Example of test





<?php

class YourCommandTest extends TestCase

public function testExample()

Artisan::call('your_command', [
'command_parameter_1' => 'value1',
'command_parameter_2' => 'value2',
]);

// If you need result of console output
$resultAsText = Artisan::output();

$this->assertTrue(true);









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:10









William Desportes

10238




10238










answered May 26 '16 at 19:39









mnvmnv

5,57253152




5,57253152







  • 1





    Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

    – alariva
    Jun 25 '16 at 19:12







  • 1





    I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

    – mnv
    Jun 25 '16 at 20:25






  • 1





    @alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

    – mnv
    Jun 26 '16 at 12:51






  • 1





    I have not option processUncoveredFilesFromWhitelist="true"

    – mnv
    Jun 26 '16 at 19:58






  • 1





    I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

    – alariva
    Jun 27 '16 at 1:01












  • 1





    Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

    – alariva
    Jun 25 '16 at 19:12







  • 1





    I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

    – mnv
    Jun 25 '16 at 20:25






  • 1





    @alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

    – mnv
    Jun 26 '16 at 12:51






  • 1





    I have not option processUncoveredFilesFromWhitelist="true"

    – mnv
    Jun 26 '16 at 19:58






  • 1





    I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

    – alariva
    Jun 27 '16 at 1:01







1




1





Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

– alariva
Jun 25 '16 at 19:12






Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.

– alariva
Jun 25 '16 at 19:12





1




1





I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

– mnv
Jun 25 '16 at 20:25





I run tests with option --coverage-html to generate coverage report: phpunit --coverage-html coverage_path. And I see coverage of methods what called inside command.

– mnv
Jun 25 '16 at 20:25




1




1





@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

– mnv
Jun 26 '16 at 12:51





@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in phpunit.xml.

– mnv
Jun 26 '16 at 12:51




1




1





I have not option processUncoveredFilesFromWhitelist="true"

– mnv
Jun 26 '16 at 19:58





I have not option processUncoveredFilesFromWhitelist="true"

– mnv
Jun 26 '16 at 19:58




1




1





I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

– alariva
Jun 27 '16 at 1:01





I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.

– alariva
Jun 27 '16 at 1:01

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33611788%2fhow-to-test-artisan-commands-in-laravel-5%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto