zenstruck /
schedule-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Zenstruck\ScheduleBundle\Tests\Schedule\Extension; |
||
| 4 | |||
| 5 | use PHPUnit\Framework\TestCase; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Symfony\Component\Lock\LockFactory; |
||
| 7 | use Symfony\Component\Lock\Store\FlockStore; |
||
| 8 | use Zenstruck\ScheduleBundle\Schedule; |
||
| 9 | use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule; |
||
| 10 | use Zenstruck\ScheduleBundle\Schedule\Exception\SkipTask; |
||
| 11 | use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\SingleServerHandler; |
||
| 12 | use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension; |
||
| 13 | use Zenstruck\ScheduleBundle\Schedule\ScheduleRunContext; |
||
| 14 | use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunContext; |
||
| 15 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockLogger; |
||
| 16 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder; |
||
| 17 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @author Kevin Bond <[email protected]> |
||
| 21 | */ |
||
| 22 | final class SingleServerExtensionTest extends TestCase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @test |
||
| 26 | */ |
||
| 27 | public function can_lock_schedule() |
||
| 28 | { |
||
| 29 | $logger = new MockLogger(); |
||
| 30 | $lockFactory = new LockFactory(new FlockStore()); |
||
| 31 | $lockFactory->setLogger($logger); |
||
| 32 | |||
| 33 | (new MockScheduleBuilder()) |
||
| 34 | ->addHandler(new SingleServerHandler($lockFactory)) |
||
| 35 | ->addExtension(new SingleServerExtension()) |
||
| 36 | ->run() |
||
| 37 | ; |
||
| 38 | |||
| 39 | $this->assertTrue($logger->hasMessageThatContains('Successfully acquired')); |
||
| 40 | $this->assertTrue($logger->hasMessageThatContains('Expiration defined')); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @test |
||
| 45 | */ |
||
| 46 | public function can_lock_task() |
||
| 47 | { |
||
| 48 | $logger = new MockLogger(); |
||
| 49 | $lockFactory = new LockFactory(new FlockStore()); |
||
| 50 | $lockFactory->setLogger($logger); |
||
| 51 | |||
| 52 | (new MockScheduleBuilder()) |
||
| 53 | ->addHandler(new SingleServerHandler($lockFactory)) |
||
| 54 | ->addTask((new MockTask())->onSingleServer()) |
||
| 55 | ->run() |
||
| 56 | ; |
||
| 57 | |||
| 58 | $this->assertTrue($logger->hasMessageThatContains('Successfully acquired')); |
||
| 59 | $this->assertTrue($logger->hasMessageThatContains('Expiration defined')); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @test |
||
| 64 | */ |
||
| 65 | public function provides_helpful_message_if_handler_not_configured() |
||
| 66 | { |
||
| 67 | $this->expectException(\LogicException::class); |
||
| 68 | $this->expectExceptionMessage('To use "onSingleServer" you must configure a lock factory (config path: "zenstruck_schedule.single_server_lock_factory")'); |
||
| 69 | |||
| 70 | (new MockScheduleBuilder()) |
||
| 71 | ->addExtension(new SingleServerExtension()) |
||
| 72 | ->run() |
||
| 73 | ; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @test |
||
| 78 | */ |
||
| 79 | public function skips_schedule_if_locked() |
||
| 80 | { |
||
| 81 | $schedule1 = new Schedule(); |
||
| 82 | $schedule1->addCommand('my:command'); |
||
| 83 | $schedule1->onSingleServer(); |
||
| 84 | $schedule2 = new Schedule(); |
||
| 85 | $schedule2->addCommand('my:command'); |
||
| 86 | $schedule2->onSingleServer(); |
||
| 87 | |||
| 88 | $handler = new SingleServerHandler(new LockFactory(new FlockStore())); |
||
| 89 | |||
| 90 | $handler->filterSchedule(new ScheduleRunContext($schedule1), $schedule1->getExtensions()[0]); |
||
| 91 | |||
| 92 | $this->expectException(SkipSchedule::class); |
||
| 93 | $this->expectExceptionMessage('Schedule running on another server.'); |
||
| 94 | |||
| 95 | $handler->filterSchedule(new ScheduleRunContext($schedule2), $schedule2->getExtensions()[0]); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @test |
||
| 100 | */ |
||
| 101 | public function skips_task_if_locked() |
||
| 102 | { |
||
| 103 | $schedule1 = new Schedule(); |
||
| 104 | $task1 = $schedule1->addCommand('my:command')->onSingleServer(); |
||
| 105 | $schedule2 = new Schedule(); |
||
| 106 | $task2 = $schedule2->addCommand('my:command')->onSingleServer(); |
||
| 107 | |||
| 108 | $handler = new SingleServerHandler(new LockFactory(new FlockStore())); |
||
| 109 | |||
| 110 | $handler->filterTask(new TaskRunContext(new ScheduleRunContext($schedule1), $task1), $task1->getExtensions()[0]); |
||
| 111 | |||
| 112 | $this->expectException(SkipTask::class); |
||
| 113 | $this->expectExceptionMessage('Task running on another server.'); |
||
| 114 | |||
| 115 | $handler->filterTask(new TaskRunContext(new ScheduleRunContext($schedule2), $task2), $task2->getExtensions()[0]); |
||
| 116 | } |
||
| 117 | } |
||
| 118 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths