diff --git a/tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php b/tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php index c0502a2b..7fe65714 100644 --- a/tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php +++ b/tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php @@ -4,6 +4,8 @@ namespace Tests\Unit\Middleware; +use Illuminate\Contracts\Cache\Lock; +use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Facades\Cache; use Mockery\MockInterface; use Tests\Fixtures\TestActivity; @@ -111,4 +113,60 @@ public function testAllowsMultipleActivityInstances(): void $this->assertNull(Cache::get($middleware1->getWorkflowSemaphoreKey())); $this->assertSame(0, count(Cache::get($middleware1->getActivitySemaphoreKey()))); } + + public function testUnknownTypeDoesNotCallNext(): void + { + $this->app->make('cache') + ->store() + ->clear(); + + $job = $this->mock(TestActivity::class, static function (MockInterface $mock) { + $mock->shouldReceive('release') + ->once(); + }); + + $middleware = new WithoutOverlappingMiddleware(1, 999); + + $middleware->handle($job, function ($job) { + $this->fail('Should not call next when type is unknown'); + }); + + $this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey())); + $this->assertNull(Cache::get($middleware->getActivitySemaphoreKey())); + } + + public function testReleaseWhenCompareAndSetFails(): void + { + $this->app->make('cache') + ->store() + ->clear(); + + $job = $this->mock(TestWorkflow::class, static function (MockInterface $mock) { + $mock->shouldReceive('release') + ->once(); + }); + + $lock = $this->mock(Lock::class, static function (MockInterface $mock) { + $mock->shouldReceive('get') + ->once() + ->andReturn(false); + }); + + $cache = $this->mock(Repository::class, static function (MockInterface $mock) use ($lock) { + $mock->shouldReceive('lock') + ->once() + ->andReturn($lock); + $mock->shouldReceive('get') + ->andReturn([]); + }); + + $middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW); + + $middleware->handle($job, function ($job) { + $this->fail('Should not call next when lock is not acquired'); + }); + + $this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey())); + $this->assertNull(Cache::get($middleware->getActivitySemaphoreKey())); + } } diff --git a/tests/Unit/Serializers/SerializeTest.php b/tests/Unit/Serializers/SerializeTest.php index 3f35e541..399e0386 100644 --- a/tests/Unit/Serializers/SerializeTest.php +++ b/tests/Unit/Serializers/SerializeTest.php @@ -58,6 +58,13 @@ public static function dataProvider(): array ]; } + public function testSerializableReturnsFalseForClosure(): void + { + $this->assertFalse(Serializer::serializable(static function () { + return 'test'; + })); + } + private function testSerializeUnserialize($data, $serializer, $unserializer): void { config([ diff --git a/tests/Unit/Traits/MonitorQueueConnectionTest.php b/tests/Unit/Traits/MonitorQueueConnectionTest.php new file mode 100644 index 00000000..f3756d02 --- /dev/null +++ b/tests/Unit/Traits/MonitorQueueConnectionTest.php @@ -0,0 +1,67 @@ + 'sync', + 'workflows.monitor_connection' => config('queue.default'), + ]); + + $instance = $this->makeAnonymousTraitInstance(); + + $this->assertSame(config('queue.default'), $instance->viaConnection()); + } + + public function testReturnsDefaultQueue(): void + { + config([ + 'queue.default' => 'sync', + 'workflows.monitor_connection' => config('queue.default'), + ]); + + $instance = $this->makeAnonymousTraitInstance(); + + $this->assertSame('default', $instance->viaQueue()); + } + + public function testReturnsCustomConnection(): void + { + config([ + 'queue.default' => 'sync', + 'workflows.monitor_connection' => 'custom_connection', + ]); + + $instance = $this->makeAnonymousTraitInstance(); + + $this->assertSame('custom_connection', $instance->viaConnection()); + } + + public function testReturnsCustomQueue(): void + { + config([ + 'queue.default' => 'sync', + 'workflows.monitor_connection' => config('queue.default'), + 'workflows.monitor_queue' => 'custom_queue', + ]); + + $instance = $this->makeAnonymousTraitInstance(); + + $this->assertSame('custom_queue', $instance->viaQueue()); + } + + private function makeAnonymousTraitInstance(): object + { + return new class() { + use MonitorQueueConnection; + }; + } +} diff --git a/tests/Unit/migrations/MigrationsTest.php b/tests/Unit/migrations/MigrationsTest.php new file mode 100644 index 00000000..ae24cc0b --- /dev/null +++ b/tests/Unit/migrations/MigrationsTest.php @@ -0,0 +1,33 @@ +assertTrue(Schema::hasTable('workflows')); + $this->assertTrue(Schema::hasTable('workflow_logs')); + $this->assertTrue(Schema::hasTable('workflow_signals')); + $this->assertTrue(Schema::hasTable('workflow_timers')); + $this->assertTrue(Schema::hasTable('workflow_exceptions')); + $this->assertTrue(Schema::hasTable('workflow_relationships')); + + $this->artisan('migrate:reset', [ + '--path' => dirname(__DIR__, 3) . '/src/migrations', + '--realpath' => true, + ])->run(); + + $this->assertFalse(Schema::hasTable('workflows')); + $this->assertFalse(Schema::hasTable('workflow_logs')); + $this->assertFalse(Schema::hasTable('workflow_signals')); + $this->assertFalse(Schema::hasTable('workflow_timers')); + $this->assertFalse(Schema::hasTable('workflow_exceptions')); + $this->assertFalse(Schema::hasTable('workflow_relationships')); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 00d2f112..25b78bcc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,7 +3,6 @@ declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; -require_once __DIR__ . '/classAliases.php'; use PHPUnit\Event\Facade; use Tests\TestSuiteSubscriber;