Skip to content

Commit 2bad80f

Browse files
authored
Improve test coverage (#225)
1 parent e718685 commit 2bad80f

5 files changed

Lines changed: 165 additions & 1 deletion

File tree

tests/Unit/Middleware/WithoutOverlappingMiddlewareTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Tests\Unit\Middleware;
66

7+
use Illuminate\Contracts\Cache\Lock;
8+
use Illuminate\Contracts\Cache\Repository;
79
use Illuminate\Support\Facades\Cache;
810
use Mockery\MockInterface;
911
use Tests\Fixtures\TestActivity;
@@ -111,4 +113,60 @@ public function testAllowsMultipleActivityInstances(): void
111113
$this->assertNull(Cache::get($middleware1->getWorkflowSemaphoreKey()));
112114
$this->assertSame(0, count(Cache::get($middleware1->getActivitySemaphoreKey())));
113115
}
116+
117+
public function testUnknownTypeDoesNotCallNext(): void
118+
{
119+
$this->app->make('cache')
120+
->store()
121+
->clear();
122+
123+
$job = $this->mock(TestActivity::class, static function (MockInterface $mock) {
124+
$mock->shouldReceive('release')
125+
->once();
126+
});
127+
128+
$middleware = new WithoutOverlappingMiddleware(1, 999);
129+
130+
$middleware->handle($job, function ($job) {
131+
$this->fail('Should not call next when type is unknown');
132+
});
133+
134+
$this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey()));
135+
$this->assertNull(Cache::get($middleware->getActivitySemaphoreKey()));
136+
}
137+
138+
public function testReleaseWhenCompareAndSetFails(): void
139+
{
140+
$this->app->make('cache')
141+
->store()
142+
->clear();
143+
144+
$job = $this->mock(TestWorkflow::class, static function (MockInterface $mock) {
145+
$mock->shouldReceive('release')
146+
->once();
147+
});
148+
149+
$lock = $this->mock(Lock::class, static function (MockInterface $mock) {
150+
$mock->shouldReceive('get')
151+
->once()
152+
->andReturn(false);
153+
});
154+
155+
$cache = $this->mock(Repository::class, static function (MockInterface $mock) use ($lock) {
156+
$mock->shouldReceive('lock')
157+
->once()
158+
->andReturn($lock);
159+
$mock->shouldReceive('get')
160+
->andReturn([]);
161+
});
162+
163+
$middleware = new WithoutOverlappingMiddleware(1, WithoutOverlappingMiddleware::WORKFLOW);
164+
165+
$middleware->handle($job, function ($job) {
166+
$this->fail('Should not call next when lock is not acquired');
167+
});
168+
169+
$this->assertNull(Cache::get($middleware->getWorkflowSemaphoreKey()));
170+
$this->assertNull(Cache::get($middleware->getActivitySemaphoreKey()));
171+
}
114172
}

tests/Unit/Serializers/SerializeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ public static function dataProvider(): array
5858
];
5959
}
6060

61+
public function testSerializableReturnsFalseForClosure(): void
62+
{
63+
$this->assertFalse(Serializer::serializable(static function () {
64+
return 'test';
65+
}));
66+
}
67+
6168
private function testSerializeUnserialize($data, $serializer, $unserializer): void
6269
{
6370
config([
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit\Traits;
6+
7+
use Tests\TestCase;
8+
use Workflow\Traits\MonitorQueueConnection;
9+
10+
class MonitorQueueConnectionTest extends TestCase
11+
{
12+
public function testReturnsDefaultConnection(): void
13+
{
14+
config([
15+
'queue.default' => 'sync',
16+
'workflows.monitor_connection' => config('queue.default'),
17+
]);
18+
19+
$instance = $this->makeAnonymousTraitInstance();
20+
21+
$this->assertSame(config('queue.default'), $instance->viaConnection());
22+
}
23+
24+
public function testReturnsDefaultQueue(): void
25+
{
26+
config([
27+
'queue.default' => 'sync',
28+
'workflows.monitor_connection' => config('queue.default'),
29+
]);
30+
31+
$instance = $this->makeAnonymousTraitInstance();
32+
33+
$this->assertSame('default', $instance->viaQueue());
34+
}
35+
36+
public function testReturnsCustomConnection(): void
37+
{
38+
config([
39+
'queue.default' => 'sync',
40+
'workflows.monitor_connection' => 'custom_connection',
41+
]);
42+
43+
$instance = $this->makeAnonymousTraitInstance();
44+
45+
$this->assertSame('custom_connection', $instance->viaConnection());
46+
}
47+
48+
public function testReturnsCustomQueue(): void
49+
{
50+
config([
51+
'queue.default' => 'sync',
52+
'workflows.monitor_connection' => config('queue.default'),
53+
'workflows.monitor_queue' => 'custom_queue',
54+
]);
55+
56+
$instance = $this->makeAnonymousTraitInstance();
57+
58+
$this->assertSame('custom_queue', $instance->viaQueue());
59+
}
60+
61+
private function makeAnonymousTraitInstance(): object
62+
{
63+
return new class() {
64+
use MonitorQueueConnection;
65+
};
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit\Migrations;
6+
7+
use Illuminate\Support\Facades\Schema;
8+
use Tests\TestCase;
9+
10+
final class MigrationsTest extends TestCase
11+
{
12+
public function testDownMethodsDropTables(): void
13+
{
14+
$this->assertTrue(Schema::hasTable('workflows'));
15+
$this->assertTrue(Schema::hasTable('workflow_logs'));
16+
$this->assertTrue(Schema::hasTable('workflow_signals'));
17+
$this->assertTrue(Schema::hasTable('workflow_timers'));
18+
$this->assertTrue(Schema::hasTable('workflow_exceptions'));
19+
$this->assertTrue(Schema::hasTable('workflow_relationships'));
20+
21+
$this->artisan('migrate:reset', [
22+
'--path' => dirname(__DIR__, 3) . '/src/migrations',
23+
'--realpath' => true,
24+
])->run();
25+
26+
$this->assertFalse(Schema::hasTable('workflows'));
27+
$this->assertFalse(Schema::hasTable('workflow_logs'));
28+
$this->assertFalse(Schema::hasTable('workflow_signals'));
29+
$this->assertFalse(Schema::hasTable('workflow_timers'));
30+
$this->assertFalse(Schema::hasTable('workflow_exceptions'));
31+
$this->assertFalse(Schema::hasTable('workflow_relationships'));
32+
}
33+
}

tests/bootstrap.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
require_once __DIR__ . '/../vendor/autoload.php';
6-
require_once __DIR__ . '/classAliases.php';
76

87
use PHPUnit\Event\Facade;
98
use Tests\TestSuiteSubscriber;

0 commit comments

Comments
 (0)