Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Serializers/SerializeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/Traits/MonitorQueueConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Traits;

use Tests\TestCase;
use Workflow\Traits\MonitorQueueConnection;

class MonitorQueueConnectionTest extends TestCase
{
public function testReturnsDefaultConnection(): void
{
config([
'queue.default' => '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;
};
}
}
33 changes: 33 additions & 0 deletions tests/Unit/migrations/MigrationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Migrations;

use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

final class MigrationsTest extends TestCase
{
public function testDownMethodsDropTables(): void
{
$this->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'));
}
}
1 change: 0 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down