Skip to content

Commit f23c116

Browse files
author
Balazs Sebesteny
committed
fixes #6 : fix tests and make partitionKey public to have access to it from the BalancedRedisQueue
1 parent 013cba9 commit f23c116

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/Jobs/BalancedDispatchable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ trait BalancedDispatchable
3939
/**
4040
* The partition key for this job.
4141
*/
42-
protected ?string $partitionKey = null;
42+
public ?string $partitionKey = null;
4343

4444
/**
4545
* Set the partition key for this job.

tests/Feature/BalancedDispatchableTest.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace YanGusik\BalancedQueue\Tests\Feature;
66

77
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use ReflectionClass;
9+
use ReflectionException;
810
use YanGusik\BalancedQueue\Jobs\BalancedDispatchable;
11+
use YanGusik\BalancedQueue\Queue\BalancedRedisQueue;
912
use YanGusik\BalancedQueue\Tests\TestCase;
1013

1114
class TestJobWithUserId implements ShouldQueue
@@ -54,36 +57,51 @@ public function handle(): void
5457

5558
class BalancedDispatchableTest extends TestCase
5659
{
60+
/**
61+
* @throws ReflectionException
62+
*/
5763
public function test_partition_key_from_user_id_property(): void
5864
{
5965
$job = new TestJobWithUserId(userId: 42);
6066

61-
$this->assertEquals('42', $job->getPartitionKey());
67+
$this->assertEquals('42', $this->resolvePartition($job));
6268
}
6369

70+
/**
71+
* @throws ReflectionException
72+
*/
6473
public function test_partition_key_from_custom_method(): void
6574
{
6675
$job = new TestJobWithCustomPartition(customKey: 'abc123');
6776

68-
$this->assertEquals('custom:abc123', $job->getPartitionKey());
77+
$this->assertEquals('custom:abc123', $this->resolvePartition($job));
6978
}
7079

80+
/**
81+
* @throws ReflectionException
82+
*/
7183
public function test_partition_key_default_when_no_property(): void
7284
{
7385
$job = new TestJobWithoutPartition(data: 'test');
7486

75-
$this->assertEquals('default', $job->getPartitionKey());
87+
$this->assertEquals('default', $this->resolvePartition($job));
7688
}
7789

90+
/**
91+
* @throws ReflectionException
92+
*/
7893
public function test_on_partition_sets_explicit_key(): void
7994
{
8095
$job = new TestJobWithUserId(userId: 42);
8196
$job->onPartition('explicit-key');
8297

8398
// Explicit key takes precedence over userId property
84-
$this->assertEquals('explicit-key', $job->getPartitionKey());
99+
$this->assertEquals('explicit-key', $this->resolvePartition($job));
85100
}
86101

102+
/**
103+
* @throws ReflectionException
104+
*/
87105
public function test_on_partition_returns_self(): void
88106
{
89107
$job = new TestJobWithUserId(userId: 1);
@@ -92,11 +110,25 @@ public function test_on_partition_returns_self(): void
92110
$this->assertSame($job, $result);
93111
}
94112

113+
/**
114+
* @throws ReflectionException
115+
*/
95116
public function test_on_partition_accepts_integer(): void
96117
{
97118
$job = new TestJobWithUserId(userId: 1);
98119
$job->onPartition(999);
99120

100-
$this->assertEquals('999', $job->getPartitionKey());
121+
$this->assertEquals('999', $this->resolvePartition($job));
122+
}
123+
124+
/**
125+
* @throws ReflectionException
126+
*/
127+
private function resolvePartition($job): string
128+
{
129+
$queue = resolve(BalancedRedisQueue::class);
130+
$class = new ReflectionClass($queue);
131+
$method = $class->getMethod('resolvePartition');
132+
return $method->invokeArgs($queue, [$job]);
101133
}
102134
}

0 commit comments

Comments
 (0)