From 206942a2a2230f843f5ac24ddfa3183c9419eba8 Mon Sep 17 00:00:00 2001 From: PabloKowalczyk <11366345+PabloKowalczyk@users.noreply.github.com> Date: Tue, 1 Nov 2022 05:27:28 +0000 Subject: [PATCH] Do not require BlockingStoreInterface in preventOverlapping() (#44) --- src/Event.php | 19 +++++++------------ tests/Unit/EventTest.php | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Event.php b/src/Event.php index d5b96e0..f7206ab 100644 --- a/src/Event.php +++ b/src/Event.php @@ -18,13 +18,12 @@ use Crunz\Pinger\PingableTrait; use Crunz\Process\Process; use Crunz\Task\TaskException; -use Symfony\Component\Lock\BlockingStoreInterface; use Symfony\Component\Lock\Exception\InvalidArgumentException; use Symfony\Component\Lock\Factory; use Symfony\Component\Lock\Lock; use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\PersistingStoreInterface; use Symfony\Component\Lock\Store\FlockStore; -use Symfony\Component\Lock\StoreInterface; class Event implements PingableInterface { @@ -166,7 +165,7 @@ class Event implements PingableInterface * The symfony lock factory that is used to acquire locks. If the value is null, but preventOverlapping = true * crunz falls back to filesystem locks. * - * @var Factory|LockFactory|null + * @var LockFactory|null */ private $lockFactory; /** @var string[] */ @@ -715,28 +714,24 @@ public function user($user) * By default, the lock is acquired through file system locks. Alternatively, you can pass a symfony lock store * that will be responsible for the locking. * - * @param StoreInterface|BlockingStoreInterface $store + * @param PersistingStoreInterface|object $store * * @return $this */ public function preventOverlapping(object $store = null) { - if (null !== $store && !($store instanceof BlockingStoreInterface || $store instanceof StoreInterface)) { - $legacyClass = StoreInterface::class; - $newClass = BlockingStoreInterface::class; + if (null !== $store && !($store instanceof PersistingStoreInterface)) { + $expectedClass = PersistingStoreInterface::class; $actualClass = \get_class($store); throw new \RuntimeException( - "Instance of '{$newClass}' or '{$legacyClass}' is expected, '{$actualClass}' provided" + "Instance of '{$expectedClass}' is expected, '{$actualClass}' provided" ); } $lockStore = $store ?: $this->createDefaultLockStore(); $this->preventOverlapping = true; - $this->lockFactory = \class_exists(Factory::class) - ? new Factory($lockStore) - : new LockFactory($lockStore) - ; + $this->lockFactory = new LockFactory($lockStore); // Skip the event if it's locked (processing) $this->skip(function () { diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 64f3de6..58e6719 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -10,7 +10,8 @@ use Crunz\Tests\TestCase\Faker; use Crunz\Tests\TestCase\TestClock; use Crunz\Tests\TestCase\UnitTestCase; -use Symfony\Component\Lock\BlockingStoreInterface; +use Symfony\Component\Lock\PersistingStoreInterface; +use Symfony\Component\Lock\Store\PdoStore; use Symfony\Component\Lock\Store\SemaphoreStore; final class EventTest extends UnitTestCase @@ -412,6 +413,19 @@ public function test_hourly_at_with_invalid_minute( $event->hourlyAt($minute); } + public function test_non_blocking_store_can_be_passed_to_prevent_overlapping(): void + { + // Arrange + $store = new PdoStore(''); + $event = $this->createEvent(); + + // Expect + $this->expectNotToPerformAssertions(); + + // Act + $event->preventOverlapping($store); + } + /** @return iterable */ public function deprecatedEveryProvider(): iterable { @@ -452,7 +466,7 @@ public function hourlyAtInvalidProvider(): iterable ]; } - private function assertPreventOverlapping(BlockingStoreInterface $store = null): void + private function assertPreventOverlapping(PersistingStoreInterface $store = null): void { $event = $this->createPreventOverlappingEvent($store); $event2 = $this->createPreventOverlappingEvent($store); @@ -462,7 +476,7 @@ private function assertPreventOverlapping(BlockingStoreInterface $store = null): self::assertFalse($event2->isDue(new \DateTimeZone('UTC'))); } - private function createPreventOverlappingEvent(BlockingStoreInterface $store = null): Event + private function createPreventOverlappingEvent(PersistingStoreInterface $store = null): Event { $command = "php -r 'sleep(2);'";