|
| 1 | +<?php |
| 2 | +namespace Quartz\App\Tests\Command; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use Quartz\App\Command\SchedulerCommand; |
| 6 | +use Quartz\Bridge\LoggerSubscriber; |
| 7 | +use Quartz\Bridge\SignalSubscriber; |
| 8 | +use Quartz\Scheduler\StdScheduler; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Tester\CommandTester; |
| 11 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 12 | + |
| 13 | +class SchedulerCommandTest extends TestCase |
| 14 | +{ |
| 15 | + public function testShouldExtendSymfonyCommand() |
| 16 | + { |
| 17 | + $this->assertInstanceOf(Command::class, new SchedulerCommand($this->createSchedulerMock())); |
| 18 | + } |
| 19 | + |
| 20 | + public function testShouldStartScheduler() |
| 21 | + { |
| 22 | + $dispatcher = $this->createEventDispatcherMock(); |
| 23 | + |
| 24 | + $scheduler = $this->createSchedulerMock(); |
| 25 | + $scheduler |
| 26 | + ->expects($this->any()) |
| 27 | + ->method('getEventDispatcher') |
| 28 | + ->willReturn($dispatcher) |
| 29 | + ; |
| 30 | + $scheduler |
| 31 | + ->expects($this->once()) |
| 32 | + ->method('start') |
| 33 | + ; |
| 34 | + |
| 35 | + $command = new SchedulerCommand($scheduler); |
| 36 | + |
| 37 | + $tester = new CommandTester($command); |
| 38 | + $tester->execute([]); |
| 39 | + |
| 40 | + $this->assertEmpty($tester->getDisplay()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testShouldAddLoggerSubscriber() |
| 44 | + { |
| 45 | + $dispatcher = $this->createEventDispatcherMock(); |
| 46 | + $dispatcher |
| 47 | + ->expects($this->at(0)) |
| 48 | + ->method('addSubscriber') |
| 49 | + ->with($this->isInstanceOf(LoggerSubscriber::class)) |
| 50 | + ; |
| 51 | + |
| 52 | + $scheduler = $this->createSchedulerMock(); |
| 53 | + $scheduler |
| 54 | + ->expects($this->any()) |
| 55 | + ->method('getEventDispatcher') |
| 56 | + ->willReturn($dispatcher) |
| 57 | + ; |
| 58 | + |
| 59 | + $command = new SchedulerCommand($scheduler); |
| 60 | + |
| 61 | + $tester = new CommandTester($command); |
| 62 | + $tester->execute([]); |
| 63 | + |
| 64 | + $this->assertEmpty($tester->getDisplay()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testShouldAddSignalSubscriber() |
| 68 | + { |
| 69 | + $dispatcher = $this->createEventDispatcherMock(); |
| 70 | + $dispatcher |
| 71 | + ->expects($this->at(1)) |
| 72 | + ->method('addSubscriber') |
| 73 | + ->with($this->isInstanceOf(SignalSubscriber::class)) |
| 74 | + ; |
| 75 | + |
| 76 | + $scheduler = $this->createSchedulerMock(); |
| 77 | + $scheduler |
| 78 | + ->expects($this->any()) |
| 79 | + ->method('getEventDispatcher') |
| 80 | + ->willReturn($dispatcher) |
| 81 | + ; |
| 82 | + |
| 83 | + $command = new SchedulerCommand($scheduler); |
| 84 | + |
| 85 | + $tester = new CommandTester($command); |
| 86 | + $tester->execute([]); |
| 87 | + |
| 88 | + $this->assertEmpty($tester->getDisplay()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return \PHPUnit_Framework_MockObject_MockObject|StdScheduler |
| 93 | + */ |
| 94 | + private function createSchedulerMock() |
| 95 | + { |
| 96 | + return $this->createMock(StdScheduler::class); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
| 101 | + */ |
| 102 | + private function createEventDispatcherMock() |
| 103 | + { |
| 104 | + return $this->createMock(EventDispatcherInterface::class); |
| 105 | + } |
| 106 | +} |
0 commit comments