ConsoleEventSchedulerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Illuminate\Tests\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\Scheduling\CacheEventMutex;
  5. use Illuminate\Console\Scheduling\CacheSchedulingMutex;
  6. use Illuminate\Console\Scheduling\EventMutex;
  7. use Illuminate\Console\Scheduling\Schedule;
  8. use Illuminate\Console\Scheduling\SchedulingMutex;
  9. use Illuminate\Container\Container;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. class ConsoleEventSchedulerTest extends TestCase
  13. {
  14. /**
  15. * @var \Illuminate\Console\Scheduling\Schedule
  16. */
  17. private $schedule;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $container = Container::getInstance();
  22. $container->instance(EventMutex::class, m::mock(CacheEventMutex::class));
  23. $container->instance(SchedulingMutex::class, m::mock(CacheSchedulingMutex::class));
  24. $container->instance(Schedule::class, $this->schedule = new Schedule(m::mock(EventMutex::class)));
  25. }
  26. protected function tearDown(): void
  27. {
  28. m::close();
  29. }
  30. public function testMutexCanReceiveCustomStore()
  31. {
  32. Container::getInstance()->make(EventMutex::class)->shouldReceive('useStore')->once()->with('test');
  33. Container::getInstance()->make(SchedulingMutex::class)->shouldReceive('useStore')->once()->with('test');
  34. $this->schedule->useCache('test');
  35. }
  36. public function testExecCreatesNewCommand()
  37. {
  38. $escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';
  39. $escapeReal = '\\' === DIRECTORY_SEPARATOR ? '\\"' : '"';
  40. $schedule = $this->schedule;
  41. $schedule->exec('path/to/command');
  42. $schedule->exec('path/to/command -f --foo="bar"');
  43. $schedule->exec('path/to/command', ['-f']);
  44. $schedule->exec('path/to/command', ['--foo' => 'bar']);
  45. $schedule->exec('path/to/command', ['-f', '--foo' => 'bar']);
  46. $schedule->exec('path/to/command', ['--title' => 'A "real" test']);
  47. $schedule->exec('path/to/command', [['one', 'two']]);
  48. $schedule->exec('path/to/command', ['-1 minute']);
  49. $schedule->exec('path/to/command', ['foo' => ['bar', 'baz']]);
  50. $schedule->exec('path/to/command', ['--foo' => ['bar', 'baz']]);
  51. $schedule->exec('path/to/command', ['-F' => ['bar', 'baz']]);
  52. $events = $schedule->events();
  53. $this->assertSame('path/to/command', $events[0]->command);
  54. $this->assertSame('path/to/command -f --foo="bar"', $events[1]->command);
  55. $this->assertSame('path/to/command -f', $events[2]->command);
  56. $this->assertSame("path/to/command --foo={$escape}bar{$escape}", $events[3]->command);
  57. $this->assertSame("path/to/command -f --foo={$escape}bar{$escape}", $events[4]->command);
  58. $this->assertSame("path/to/command --title={$escape}A {$escapeReal}real{$escapeReal} test{$escape}", $events[5]->command);
  59. $this->assertSame("path/to/command {$escape}one{$escape} {$escape}two{$escape}", $events[6]->command);
  60. $this->assertSame("path/to/command {$escape}-1 minute{$escape}", $events[7]->command);
  61. $this->assertSame("path/to/command {$escape}bar{$escape} {$escape}baz{$escape}", $events[8]->command);
  62. $this->assertSame("path/to/command --foo={$escape}bar{$escape} --foo={$escape}baz{$escape}", $events[9]->command);
  63. $this->assertSame("path/to/command -F {$escape}bar{$escape} -F {$escape}baz{$escape}", $events[10]->command);
  64. }
  65. public function testExecCreatesNewCommandWithTimezone()
  66. {
  67. $schedule = new Schedule('UTC');
  68. $schedule->exec('path/to/command');
  69. $events = $schedule->events();
  70. $this->assertSame('UTC', $events[0]->timezone);
  71. $schedule = new Schedule('Asia/Tokyo');
  72. $schedule->exec('path/to/command');
  73. $events = $schedule->events();
  74. $this->assertSame('Asia/Tokyo', $events[0]->timezone);
  75. }
  76. public function testCommandCreatesNewArtisanCommand()
  77. {
  78. $escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';
  79. $schedule = $this->schedule;
  80. $schedule->command('queue:listen');
  81. $schedule->command('queue:listen --tries=3');
  82. $schedule->command('queue:listen', ['--tries' => 3]);
  83. $events = $schedule->events();
  84. $binary = $escape.PHP_BINARY.$escape;
  85. $artisan = $escape.'artisan'.$escape;
  86. $this->assertEquals($binary.' '.$artisan.' queue:listen', $events[0]->command);
  87. $this->assertEquals($binary.' '.$artisan.' queue:listen --tries=3', $events[1]->command);
  88. $this->assertEquals($binary.' '.$artisan.' queue:listen --tries=3', $events[2]->command);
  89. }
  90. public function testCreateNewArtisanCommandUsingCommandClass()
  91. {
  92. $escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';
  93. $schedule = $this->schedule;
  94. $schedule->command(ConsoleCommandStub::class, ['--force']);
  95. $events = $schedule->events();
  96. $binary = $escape.PHP_BINARY.$escape;
  97. $artisan = $escape.'artisan'.$escape;
  98. $this->assertEquals($binary.' '.$artisan.' foo:bar --force', $events[0]->command);
  99. }
  100. public function testItUsesCommandDescriptionAsEventDescription()
  101. {
  102. $schedule = $this->schedule;
  103. $event = $schedule->command(ConsoleCommandStub::class);
  104. $this->assertEquals('This is a description about the command', $event->description);
  105. }
  106. public function testItShouldBePossibleToOverwriteTheDescription()
  107. {
  108. $schedule = $this->schedule;
  109. $event = $schedule->command(ConsoleCommandStub::class)
  110. ->description('This is an alternative description');
  111. $this->assertEquals('This is an alternative description', $event->description);
  112. }
  113. public function testCallCreatesNewJobWithTimezone()
  114. {
  115. $schedule = new Schedule('UTC');
  116. $schedule->call('path/to/command');
  117. $events = $schedule->events();
  118. $this->assertSame('UTC', $events[0]->timezone);
  119. $schedule = new Schedule('Asia/Tokyo');
  120. $schedule->call('path/to/command');
  121. $events = $schedule->events();
  122. $this->assertSame('Asia/Tokyo', $events[0]->timezone);
  123. }
  124. }
  125. class FooClassStub
  126. {
  127. protected $schedule;
  128. public function __construct(Schedule $schedule)
  129. {
  130. $this->schedule = $schedule;
  131. }
  132. }
  133. class ConsoleCommandStub extends Command
  134. {
  135. protected $signature = 'foo:bar';
  136. protected $description = 'This is a description about the command';
  137. protected $foo;
  138. public function __construct(FooClassStub $foo)
  139. {
  140. parent::__construct();
  141. $this->foo = $foo;
  142. }
  143. }