55namespace YanGusik \BalancedQueue \Tests \Feature ;
66
77use Illuminate \Contracts \Queue \ShouldQueue ;
8+ use ReflectionClass ;
9+ use ReflectionException ;
810use YanGusik \BalancedQueue \Jobs \BalancedDispatchable ;
11+ use YanGusik \BalancedQueue \Queue \BalancedRedisQueue ;
912use YanGusik \BalancedQueue \Tests \TestCase ;
1013
1114class TestJobWithUserId implements ShouldQueue
@@ -54,36 +57,51 @@ public function handle(): void
5457
5558class 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