ConsoleApplicationTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Illuminate\Tests\Integration\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use Illuminate\Contracts\Console\Kernel;
  6. use Orchestra\Testbench\TestCase;
  7. class ConsoleApplicationTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. parent::setUp();
  12. $this->app[Kernel::class]->registerCommand(new FooCommandStub);
  13. }
  14. public function testArtisanCallUsingCommandName()
  15. {
  16. $this->artisan('foo:bar', [
  17. 'id' => 1,
  18. ])->assertExitCode(0);
  19. }
  20. public function testArtisanCallUsingCommandClass()
  21. {
  22. $this->artisan(FooCommandStub::class, [
  23. 'id' => 1,
  24. ])->assertExitCode(0);
  25. }
  26. public function testArtisanCallNow()
  27. {
  28. $exitCode = $this->artisan('foo:bar', [
  29. 'id' => 1,
  30. ])->run();
  31. $this->assertSame(0, $exitCode);
  32. }
  33. public function testArtisanWithMockCallAfterCallNow()
  34. {
  35. $exitCode = $this->artisan('foo:bar', [
  36. 'id' => 1,
  37. ])->run();
  38. $mock = $this->artisan('foo:bar', [
  39. 'id' => 1,
  40. ]);
  41. $this->assertSame(0, $exitCode);
  42. $mock->assertExitCode(0);
  43. }
  44. public function testArtisanInstantiateScheduleWhenNeed()
  45. {
  46. $this->assertFalse($this->app->resolved(Schedule::class));
  47. $this->app[Kernel::class]->registerCommand(new ScheduleCommandStub);
  48. $this->assertFalse($this->app->resolved(Schedule::class));
  49. $this->artisan('foo:schedule');
  50. $this->assertTrue($this->app->resolved(Schedule::class));
  51. }
  52. }
  53. class FooCommandStub extends Command
  54. {
  55. protected $signature = 'foo:bar {id}';
  56. public function handle()
  57. {
  58. //
  59. }
  60. }
  61. class ScheduleCommandStub extends Command
  62. {
  63. protected $signature = 'foo:schedule';
  64. public function handle(Schedule $schedule)
  65. {
  66. //
  67. }
  68. }