diff --git a/.phive/phars.xml b/.phive/phars.xml index f5aa330..a95d33f 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,4 +1,4 @@ - + diff --git a/composer.json b/composer.json index 410e2e3..c9d1609 100644 --- a/composer.json +++ b/composer.json @@ -22,15 +22,15 @@ }, "require": { "php": ">=8.1", - "cakephp/cakephp": "^5.0.0", + "cakephp/cakephp": "^5.1.0", "enqueue/simple-client": "^0.10", "psr/log": "^3.0" }, "require-dev": { - "cakephp/bake": "^3.0.0", + "cakephp/bake": "^3.5.1", "cakephp/cakephp-codesniffer": "^5.0", "enqueue/fs": "^0.10", - "phpunit/phpunit": "^10.1.0" + "phpunit/phpunit": "^10.5.32 || ^11.3.3 || ^12.0.9" }, "suggest": { "cakephp/bake": "Required if you want to generate jobs.", diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 3edb1fc..0f8d406 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,7 +1,9 @@ - + src/ + tests/ + tests/comparisons/* diff --git a/src/QueueManager.php b/src/QueueManager.php index aac4739..32cd914 100644 --- a/src/QueueManager.php +++ b/src/QueueManager.php @@ -147,6 +147,16 @@ public static function getConfig(string $key): mixed return static::$_config[$key] ?? null; } + /** + * Get the configured queue keys. + * + * @return array List of configured queue configuration keys. + */ + public static function configured(): array + { + return array_keys(static::$_config); + } + /** * Remove a configured queue adapter. * diff --git a/templates/bake/job.twig b/templates/bake/job.twig index 3a4705b..7dc6873 100644 --- a/templates/bake/job.twig +++ b/templates/bake/job.twig @@ -13,14 +13,14 @@ * @license https://opensource.org/licenses/MIT MIT License */ #} -skipIf(version_compare(Configure::version(), '4.2', '<'), 'DI Container is only available since CakePHP 4.2'); @@ -297,9 +290,8 @@ public function testQueueProcessesJobWithDIService() /** * Test that queue will process when a unique cache is configured. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testQueueProcessesWithUniqueCacheConfigured() { $config = [ @@ -324,9 +316,8 @@ public function testQueueProcessesWithUniqueCacheConfigured() /** * Test that queue uses default processor when no processor is specified. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testQueueUsesDefaultProcessor() { $config = [ @@ -352,9 +343,8 @@ public function testQueueUsesDefaultProcessor() /** * Test that queue uses custom processor when specified in configuration. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testQueueUsesCustomProcessor() { $config = [ @@ -383,9 +373,8 @@ public function testQueueUsesCustomProcessor() /** * Test that queue aborts when custom processor class does not exist. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testQueueAbortsWithNonExistentProcessor() { $config = [ @@ -403,9 +392,8 @@ public function testQueueAbortsWithNonExistentProcessor() /** * Test that queue aborts when custom processor does not implement Interop\Queue\Processor. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testQueueAbortsWithInvalidProcessor() { $config = [ @@ -423,9 +411,8 @@ public function testQueueAbortsWithInvalidProcessor() /** * Test that custom processor works with listener configuration. - * - * @runInSeparateProcess */ + #[RunInSeparateProcess] public function testCustomProcessorWithListener() { $config = [ diff --git a/tests/TestCase/Consumption/LimitAttemptsExtensionTest.php b/tests/TestCase/Consumption/LimitAttemptsExtensionTest.php index 7094d47..70ec9ab 100644 --- a/tests/TestCase/Consumption/LimitAttemptsExtensionTest.php +++ b/tests/TestCase/Consumption/LimitAttemptsExtensionTest.php @@ -10,7 +10,7 @@ use Cake\Queue\Consumption\LimitConsumedMessagesExtension; use Cake\Queue\Queue\Processor as QueueProcessor; use Cake\Queue\QueueManager; -use Cake\Queue\Test\TestCase\DebugLogTrait; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; use Enqueue\Consumption\ChainExtension; use Psr\Log\NullLogger; @@ -19,7 +19,7 @@ class LimitAttemptsExtensionTest extends TestCase { - use DebugLogTrait; + use QueueTestTrait; public function setUp(): void { @@ -28,16 +28,6 @@ public function setUp(): void EventManager::instance()->setEventList(new EventList()); } - /** - * @beforeClass - * @after - */ - public static function dropConfigs() - { - Log::drop('debug'); - QueueManager::drop('default'); - } - public function testMessageShouldBeRequeuedIfMaxAttemptsIsNotSet() { $consume = $this->setupQueue(); diff --git a/tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php b/tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php index 9d09e2a..ff8be08 100644 --- a/tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php +++ b/tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php @@ -9,6 +9,7 @@ use Cake\Queue\Consumption\RemoveUniqueJobIdFromCacheExtension; use Cake\Queue\Queue\Processor as QueueProcessor; use Cake\Queue\QueueManager; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; use Enqueue\Consumption\ChainExtension; use Psr\Log\NullLogger; @@ -16,22 +17,7 @@ class RemoveUniqueJobIdFromCacheExtensionTest extends TestCase { - /** - * @beforeClass - * @after - */ - public static function dropConfigs() - { - Log::drop('debug'); - - QueueManager::drop('default'); - - $cacheKey = QueueManager::getConfig('default')['uniqueCacheKey'] ?? null; - if ($cacheKey) { - Cache::clear($cacheKey); - Cache::drop($cacheKey); - } - } + use QueueTestTrait; public function testJobIsRemovedFromCacheAfterProcessing() { diff --git a/tests/TestCase/DebugLogTrait.php b/tests/TestCase/DebugLogTrait.php deleted file mode 100644 index e50006d..0000000 --- a/tests/TestCase/DebugLogTrait.php +++ /dev/null @@ -1,36 +0,0 @@ -debugLogCount($expected); - - $this->assertGreaterThanOrEqual(1, $found, "Did not find `{$expected}` in logs."); - } - - protected function assertDebugLogContainsExactly($expected, $times): void - { - $found = $this->debugLogCount($expected); - - $this->assertSame($times, $found, "Did not find `{$expected}` exactly {$times} times in logs."); - } - - protected function debugLogCount($seach) - { - $log = Log::engine('debug'); - $found = 0; - foreach ($log->read() as $line) { - if (strpos($line, $seach) !== false) { - $found++; - } - } - - return $found; - } -} diff --git a/tests/TestCase/Listener/FailedJobsListenerTest.php b/tests/TestCase/Listener/FailedJobsListenerTest.php index 9a9a71c..ab181a0 100644 --- a/tests/TestCase/Listener/FailedJobsListenerTest.php +++ b/tests/TestCase/Listener/FailedJobsListenerTest.php @@ -14,6 +14,7 @@ * @since 0.1.0 * @license https://opensource.org/licenses/MIT MIT License */ + namespace Cake\Queue\Test\TestCase\Mailer; use Cake\Event\Event; @@ -25,15 +26,19 @@ use Cake\Queue\Listener\FailedJobsListener; use Cake\Queue\Model\Table\FailedJobsTable; use Cake\Queue\QueueManager; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; use Enqueue\Null\NullConnectionFactory; use Enqueue\Null\NullMessage; +use PHPUnit\Framework\Attributes\DataProvider; use RuntimeException; use stdClass; use TestApp\Job\LogToDebugJob; class FailedJobsListenerTest extends TestCase { + use QueueTestTrait; + protected array $fixtures = [ 'plugin.Cake/Queue.FailedJobs', ]; @@ -48,13 +53,6 @@ public function setUp(): void ]); } - public function tearDown(): void - { - parent::tearDown(); - - QueueManager::drop('example_config'); - } - public function testFailedJobIsAddedWhenEventIsFired() { $parsedBody = [ @@ -113,9 +111,9 @@ public static function storeFailedJobExceptionDataProvider() } /** - * @dataProvider storeFailedJobExceptionDataProvider * @return void */ + #[DataProvider('storeFailedJobExceptionDataProvider')] public function testStoreFailedJobException($eventData, $exceptionMessage) { $tableLocator = $this diff --git a/tests/TestCase/Mailer/QueueTraitTest.php b/tests/TestCase/Mailer/QueueTraitTest.php index 93d067b..df5f241 100644 --- a/tests/TestCase/Mailer/QueueTraitTest.php +++ b/tests/TestCase/Mailer/QueueTraitTest.php @@ -18,11 +18,15 @@ use Cake\Mailer\Exception\MissingActionException; use Cake\Queue\QueueManager; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; use TestApp\WelcomeMailer; class QueueTraitTest extends TestCase { + use QueueTestTrait; + /** * Test that a MissingActionException is being thrown when * the push action is not found on the object with the QueueTrait @@ -38,10 +42,8 @@ public function testQueueTraitTestThrowsMissingActionException() /** * Test that QueueTrait calls push - * - * @runInSeparateProcess - * @return @void */ + #[RunInSeparateProcess] public function testQueueTraitCallsPush() { $queue = new WelcomeMailer(); diff --git a/tests/TestCase/Mailer/Transport/QueueTransportTest.php b/tests/TestCase/Mailer/Transport/QueueTransportTest.php index 61ff9a5..7da8041 100644 --- a/tests/TestCase/Mailer/Transport/QueueTransportTest.php +++ b/tests/TestCase/Mailer/Transport/QueueTransportTest.php @@ -20,10 +20,13 @@ use Cake\Mailer\Transport\SmtpTransport; use Cake\Queue\Mailer\Transport\QueueTransport; use Cake\Queue\QueueManager; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; class QueueTransportTest extends TestCase { + use QueueTestTrait; + private $fsQueuePath = TMP . DS . 'queue'; private function getFsQueueUrl(): string diff --git a/tests/TestCase/Queue/ProcessorTest.php b/tests/TestCase/Queue/ProcessorTest.php index 0da6b9c..c2eccf9 100644 --- a/tests/TestCase/Queue/ProcessorTest.php +++ b/tests/TestCase/Queue/ProcessorTest.php @@ -14,6 +14,7 @@ * @since 0.1.0 * @license https://opensource.org/licenses/MIT MIT License */ + namespace Cake\Queue\Test\TestCase\Queue; use Cake\Event\EventList; @@ -21,14 +22,18 @@ use Cake\Log\Log; use Cake\Queue\Job\Message; use Cake\Queue\Queue\Processor; +use Cake\Queue\Test\TestCase\QueueTestTrait; use Cake\TestSuite\TestCase; use Enqueue\Null\NullConnectionFactory; use Enqueue\Null\NullMessage; use Interop\Queue\Processor as InteropProcessor; +use PHPUnit\Framework\Attributes\DataProvider; use TestApp\TestProcessor; class ProcessorTest extends TestCase { + use QueueTestTrait; + public static $lastProcessMessage; /** @@ -54,9 +59,9 @@ public static function dataProviderTestProcess(): array * @param string $expected The expected process result. * @param string $logMessage The log message based on process result. * @param string $dispacthedEvent The dispatched event based on process result. - * @dataProvider dataProviderTestProcess * @return void */ + #[DataProvider('dataProviderTestProcess')] public function testProcess($jobMethod, $expected, $logMessage, $dispatchedEvent) { $messageBody = [ diff --git a/tests/TestCase/QueueManagerTest.php b/tests/TestCase/QueueManagerTest.php index ba3bdc6..04576a0 100644 --- a/tests/TestCase/QueueManagerTest.php +++ b/tests/TestCase/QueueManagerTest.php @@ -14,6 +14,7 @@ * @since 0.1.0 * @license https://www.opensource.org/licenses/mit-license.php MIT License */ + namespace Cake\Queue\Test\TestCase; use BadMethodCallException; @@ -32,7 +33,7 @@ */ class QueueManagerTest extends TestCase { - use DebugLogTrait; + use QueueTestTrait; private $fsQueuePath = TMP . DS . 'queue'; diff --git a/tests/TestCase/QueueTestTrait.php b/tests/TestCase/QueueTestTrait.php new file mode 100644 index 0000000..c056223 --- /dev/null +++ b/tests/TestCase/QueueTestTrait.php @@ -0,0 +1,109 @@ +debugLogCount($expected); + + $this->assertGreaterThanOrEqual(1, $found, "Did not find `{$expected}` in logs."); + } + + /** + * Assert that a message was found exactly N times in debug logs + * + * @param string $expected The message to search for in logs + * @param int $times The exact number of times the message should appear + * @return void + */ + protected function assertDebugLogContainsExactly($expected, $times): void + { + $found = $this->debugLogCount($expected); + + $this->assertSame($times, $found, "Did not find `{$expected}` exactly {$times} times in logs."); + } + + /** + * Count occurrences of a message in debug logs + * + * @param string $search The message to search for + * @return int The number of times the message was found + */ + protected function debugLogCount($search) + { + $log = Log::engine('debug'); + $found = 0; + foreach ($log->read() as $line) { + if (strpos($line, $search) !== false) { + $found++; + } + } + + return $found; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 75c23fd..5c6d7c9 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -54,10 +54,14 @@ @mkdir(CACHE . 'models'); // phpcs:enable +$cache_key = '_cake_translations_'; +if (Configure::version() <= '5.1.0') { + $cache_key = '_cake_core_'; +} Cache::setConfig([ - '_cake_core_' => [ + $cache_key => [ 'engine' => 'File', - 'prefix' => 'cake_core_', + 'prefix' => '_cake_translations_', 'serialize' => true, ], '_cake_model_' => [ diff --git a/tests/comparisons/JobTaskWithMaxAttempts.php b/tests/comparisons/JobTaskWithMaxAttempts.php index 99c4f4e..d8f4c91 100644 --- a/tests/comparisons/JobTaskWithMaxAttempts.php +++ b/tests/comparisons/JobTaskWithMaxAttempts.php @@ -14,7 +14,7 @@ class UploadJob implements JobInterface { /** * The maximum number of times the job may be attempted. - * + * * @var int|null */ public static $maxAttempts = 3; diff --git a/tests/comparisons/JobTaskWithUnique.php b/tests/comparisons/JobTaskWithUnique.php index cae4020..f0faf36 100644 --- a/tests/comparisons/JobTaskWithUnique.php +++ b/tests/comparisons/JobTaskWithUnique.php @@ -14,7 +14,7 @@ class UploadJob implements JobInterface { /** * Whether there should be only one instance of a job on the queue at a time. (optional property) - * + * * @var bool */ public static $shouldBeUnique = true;