Skip to content

Commit 62ae616

Browse files
committed
Fix tests
- Update tests to be compatible with phpunit 11/12 - Implement QueueMananger::configured() to allow easier lookup and thus also teardowns in tests - Conslidated DebugLogTrait into new QueueTestTrait - Bump min phpunit to 10.5.32
1 parent 66d9ae4 commit 62ae616

13 files changed

+180
-115
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"cakephp/bake": "^3.0.0",
3131
"cakephp/cakephp-codesniffer": "^5.0",
3232
"enqueue/fs": "^0.10",
33-
"phpunit/phpunit": "^10.1.0 || ^11.1.3 || ^12.0.9"
33+
"phpunit/phpunit": "^10.5.32 || ^11.1.3 || ^12.0.9"
3434
},
3535
"suggest": {
3636
"cakephp/bake": "Required if you want to generate jobs.",

src/QueueManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ public static function getConfig(string $key): mixed
147147
return static::$_config[$key] ?? null;
148148
}
149149

150+
/**
151+
* Get the configured queue keys.
152+
*
153+
* @return array List of configured queue configuration keys.
154+
*/
155+
public static function configured(): array
156+
{
157+
return array_keys(static::$_config);
158+
}
159+
150160
/**
151161
* Remove a configured queue adapter.
152162
*

tests/TestCase/Command/RequeueCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Cake\Core\Configure;
2121
use Cake\Log\Log;
2222
use Cake\Queue\QueueManager;
23-
use Cake\Queue\Test\TestCase\DebugLogTrait;
23+
use Cake\Queue\Test\TestCase\QueueTestTrait;
2424
use Cake\TestSuite\TestCase;
2525
use TestApp\Job\LogToDebugJob;
2626

@@ -32,7 +32,7 @@
3232
class RequeueCommandTest extends TestCase
3333
{
3434
use ConsoleIntegrationTestTrait;
35-
use DebugLogTrait;
35+
use QueueTestTrait;
3636

3737
protected array $fixtures = [
3838
'plugin.Cake/Queue.FailedJobs',

tests/TestCase/Command/WorkerCommandTest.php

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
use Cake\Queue\QueueManager;
2323
use Cake\Queue\Test\test_app\src\Job\LogToDebugWithServiceJob;
2424
use Cake\Queue\Test\test_app\src\Queue\TestCustomProcessor;
25-
use Cake\Queue\Test\TestCase\DebugLogTrait;
25+
use Cake\Queue\Test\TestCase\QueueTestTrait;
26+
use Cake\TestSuite\LogTestTrait;
2627
use Cake\TestSuite\TestCase;
28+
use PHPUnit\Framework\Attributes\DataProvider;
29+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
2730
use TestApp\Job\LogToDebugJob;
2831
use TestApp\Job\RequeueJob;
2932
use TestApp\WelcomeMailerListener;
@@ -36,7 +39,8 @@
3639
class WorkerCommandTest extends TestCase
3740
{
3841
use ConsoleIntegrationTestTrait;
39-
use DebugLogTrait;
42+
use QueueTestTrait;
43+
use LogTestTrait;
4044

4145
/**
4246
* Test that command description prints out
@@ -49,9 +53,8 @@ public function testDescriptionOutput()
4953

5054
/**
5155
* Test that queue will run for one second
52-
*
53-
* @runInSeparateProcess
5456
*/
57+
#[RunInSeparateProcess]
5558
public function testQueueProcessesStart()
5659
{
5760
Configure::write('Queue', [
@@ -66,9 +69,8 @@ public function testQueueProcessesStart()
6669

6770
/**
6871
* Test that queue will run for one second with valid listener
69-
*
70-
* @runInSeparateProcess
7172
*/
73+
#[RunInSeparateProcess]
7274
public function testQueueProcessesWithListener()
7375
{
7476
Configure::write('Queue', [
@@ -84,9 +86,8 @@ public function testQueueProcessesWithListener()
8486

8587
/**
8688
* Test that queue will abort when the passed config is not present in the app configuration.
87-
*
88-
* @runInSeparateProcess
8989
*/
90+
#[RunInSeparateProcess]
9091
public function testQueueWillAbortWithMissingConfig()
9192
{
9293
Configure::write('Queue', [
@@ -103,9 +104,8 @@ public function testQueueWillAbortWithMissingConfig()
103104

104105
/**
105106
* Test that queue will abort with invalid listener
106-
*
107-
* @runInSeparateProcess
108107
*/
108+
#[RunInSeparateProcess]
109109
public function testQueueProcessesWithInvalidListener()
110110
{
111111
Configure::write('Queue', [
@@ -122,9 +122,8 @@ public function testQueueProcessesWithInvalidListener()
122122

123123
/**
124124
* Test that queue will write to specified logger option
125-
*
126-
* @runInSeparateProcess
127125
*/
126+
#[RunInSeparateProcess]
128127
public function testQueueProcessesWithLogger()
129128
{
130129
Configure::write('Queue', [
@@ -157,10 +156,9 @@ public static function dataProviderCallableTypes(): array
157156

158157
/**
159158
* Start up the worker queue, push a job, and see that it processes
160-
*
161-
* @dataProvider dataProviderCallableTypes
162-
* @runInSeparateProcess
163159
*/
160+
#[RunInSeparateProcess]
161+
#[DataProvider('dataProviderCallableTypes')]
164162
public function testQueueProcessesJob($callable)
165163
{
166164
$config = [
@@ -186,9 +184,8 @@ public function testQueueProcessesJob($callable)
186184

187185
/**
188186
* Set the processor name, Start up the worker queue, push a job, and see that it processes
189-
*
190-
* @runInSeparateProcess
191187
*/
188+
#[RunInSeparateProcess]
192189
public function testQueueProcessesJobWithProcessor()
193190
{
194191
$config = [
@@ -213,9 +210,8 @@ public function testQueueProcessesJobWithProcessor()
213210

214211
/**
215212
* Test non-default queue name
216-
*
217-
* @runInSeparateProcess
218213
*/
214+
#[RunInSeparateProcess]
219215
public function testQueueProcessesJobWithOtherQueue()
220216
{
221217
$config = [
@@ -241,9 +237,8 @@ public function testQueueProcessesJobWithOtherQueue()
241237

242238
/**
243239
* Test max-attempts option
244-
*
245-
* @runInSeparateProcess
246240
*/
241+
#[RunInSeparateProcess]
247242
public function testQueueProcessesJobWithMaxAttempts()
248243
{
249244
$config = [
@@ -269,9 +264,8 @@ public function testQueueProcessesJobWithMaxAttempts()
269264

270265
/**
271266
* Test DI service injection works in tasks
272-
*
273-
* @runInSeparateProcess
274267
*/
268+
#[RunInSeparateProcess]
275269
public function testQueueProcessesJobWithDIService()
276270
{
277271
$this->skipIf(version_compare(Configure::version(), '4.2', '<'), 'DI Container is only available since CakePHP 4.2');
@@ -297,9 +291,8 @@ public function testQueueProcessesJobWithDIService()
297291

298292
/**
299293
* Test that queue will process when a unique cache is configured.
300-
*
301-
* @runInSeparateProcess
302294
*/
295+
#[RunInSeparateProcess]
303296
public function testQueueProcessesWithUniqueCacheConfigured()
304297
{
305298
$config = [
@@ -324,9 +317,8 @@ public function testQueueProcessesWithUniqueCacheConfigured()
324317

325318
/**
326319
* Test that queue uses default processor when no processor is specified.
327-
*
328-
* @runInSeparateProcess
329320
*/
321+
#[RunInSeparateProcess]
330322
public function testQueueUsesDefaultProcessor()
331323
{
332324
$config = [
@@ -352,9 +344,8 @@ public function testQueueUsesDefaultProcessor()
352344

353345
/**
354346
* Test that queue uses custom processor when specified in configuration.
355-
*
356-
* @runInSeparateProcess
357347
*/
348+
#[RunInSeparateProcess]
358349
public function testQueueUsesCustomProcessor()
359350
{
360351
$config = [
@@ -383,9 +374,8 @@ public function testQueueUsesCustomProcessor()
383374

384375
/**
385376
* Test that queue aborts when custom processor class does not exist.
386-
*
387-
* @runInSeparateProcess
388377
*/
378+
#[RunInSeparateProcess]
389379
public function testQueueAbortsWithNonExistentProcessor()
390380
{
391381
$config = [
@@ -403,9 +393,8 @@ public function testQueueAbortsWithNonExistentProcessor()
403393

404394
/**
405395
* Test that queue aborts when custom processor does not implement Interop\Queue\Processor.
406-
*
407-
* @runInSeparateProcess
408396
*/
397+
#[RunInSeparateProcess]
409398
public function testQueueAbortsWithInvalidProcessor()
410399
{
411400
$config = [
@@ -423,9 +412,8 @@ public function testQueueAbortsWithInvalidProcessor()
423412

424413
/**
425414
* Test that custom processor works with listener configuration.
426-
*
427-
* @runInSeparateProcess
428415
*/
416+
#[RunInSeparateProcess]
429417
public function testCustomProcessorWithListener()
430418
{
431419
$config = [

tests/TestCase/Consumption/LimitAttemptsExtensionTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
use Cake\Queue\Consumption\LimitConsumedMessagesExtension;
1111
use Cake\Queue\Queue\Processor as QueueProcessor;
1212
use Cake\Queue\QueueManager;
13-
use Cake\Queue\Test\TestCase\DebugLogTrait;
13+
use Cake\Queue\Test\TestCase\QueueTestTrait;
14+
use Cake\TestSuite\LogTestTrait;
1415
use Cake\TestSuite\TestCase;
1516
use Enqueue\Consumption\ChainExtension;
1617
use Psr\Log\NullLogger;
@@ -19,7 +20,8 @@
1920

2021
class LimitAttemptsExtensionTest extends TestCase
2122
{
22-
use DebugLogTrait;
23+
use QueueTestTrait;
24+
use LogTestTrait;
2325

2426
public function setUp(): void
2527
{
@@ -28,16 +30,6 @@ public function setUp(): void
2830
EventManager::instance()->setEventList(new EventList());
2931
}
3032

31-
/**
32-
* @beforeClass
33-
* @after
34-
*/
35-
public static function dropConfigs()
36-
{
37-
Log::drop('debug');
38-
QueueManager::drop('default');
39-
}
40-
4133
public function testMessageShouldBeRequeuedIfMaxAttemptsIsNotSet()
4234
{
4335
$consume = $this->setupQueue();

tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,17 @@
99
use Cake\Queue\Consumption\RemoveUniqueJobIdFromCacheExtension;
1010
use Cake\Queue\Queue\Processor as QueueProcessor;
1111
use Cake\Queue\QueueManager;
12+
use Cake\Queue\Test\TestCase\QueueTestTrait;
13+
use Cake\TestSuite\LogTestTrait;
1214
use Cake\TestSuite\TestCase;
1315
use Enqueue\Consumption\ChainExtension;
1416
use Psr\Log\NullLogger;
1517
use TestApp\Job\UniqueJob;
1618

1719
class RemoveUniqueJobIdFromCacheExtensionTest extends TestCase
1820
{
19-
/**
20-
* @beforeClass
21-
* @after
22-
*/
23-
public static function dropConfigs()
24-
{
25-
Log::drop('debug');
26-
27-
QueueManager::drop('default');
28-
29-
$cacheKey = QueueManager::getConfig('default')['uniqueCacheKey'] ?? null;
30-
if ($cacheKey) {
31-
Cache::clear($cacheKey);
32-
Cache::drop($cacheKey);
33-
}
34-
}
21+
use QueueTestTrait;
22+
use LogTestTrait;
3523

3624
public function testJobIsRemovedFromCacheAfterProcessing()
3725
{

tests/TestCase/DebugLogTrait.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/TestCase/Listener/FailedJobsListenerTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@
2525
use Cake\Queue\Listener\FailedJobsListener;
2626
use Cake\Queue\Model\Table\FailedJobsTable;
2727
use Cake\Queue\QueueManager;
28+
use Cake\Queue\Test\TestCase\QueueTestTrait;
29+
use Cake\TestSuite\LogTestTrait;
2830
use Cake\TestSuite\TestCase;
2931
use Enqueue\Null\NullConnectionFactory;
3032
use Enqueue\Null\NullMessage;
33+
use PHPUnit\Framework\Attributes\DataProvider;
3134
use RuntimeException;
3235
use stdClass;
3336
use TestApp\Job\LogToDebugJob;
3437

3538
class FailedJobsListenerTest extends TestCase
3639
{
40+
use QueueTestTrait;
41+
use LogTestTrait;
42+
3743
protected array $fixtures = [
3844
'plugin.Cake/Queue.FailedJobs',
3945
];
@@ -48,13 +54,6 @@ public function setUp(): void
4854
]);
4955
}
5056

51-
public function tearDown(): void
52-
{
53-
parent::tearDown();
54-
55-
QueueManager::drop('example_config');
56-
}
57-
5857
public function testFailedJobIsAddedWhenEventIsFired()
5958
{
6059
$parsedBody = [
@@ -113,9 +112,9 @@ public static function storeFailedJobExceptionDataProvider()
113112
}
114113

115114
/**
116-
* @dataProvider storeFailedJobExceptionDataProvider
117115
* @return void
118116
*/
117+
#[DataProvider('storeFailedJobExceptionDataProvider')]
119118
public function testStoreFailedJobException($eventData, $exceptionMessage)
120119
{
121120
$tableLocator = $this

0 commit comments

Comments
 (0)