Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Consumption/LimitAttemptsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LimitAttemptsExtension implements MessageResultExtensionInterface

/**
* @param int|null $maxAttempts The maximum number of times a job may be attempted. $maxAttempts defined on a Job will override this value.
* @return void
*/
public function __construct(
protected readonly ?int $maxAttempts = null,
Expand Down
1 change: 0 additions & 1 deletion src/Consumption/LimitConsumedMessagesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class LimitConsumedMessagesExtension implements PreConsumeExtensionInterface, Po

/**
* @param int $messageLimit The number of messages to process before exiting.
* @return void
*/
public function __construct(
protected readonly int $messageLimit,
Expand Down
1 change: 0 additions & 1 deletion src/Consumption/RemoveUniqueJobIdFromCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class RemoveUniqueJobIdFromCacheExtension implements MessageResultExtensionInter
{
/**
* @param string $cache Cache engine name.
* @return void
*/
public function __construct(
protected readonly string $cache,
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testBootstrapNoConfig()
$this->expectExceptionMessage('Missing `Queue` configuration key, please check the CakePHP Queue documentation to complete the plugin setup');
Configure::delete('Queue');
$plugin = new QueuePlugin();
$app = $this->getMockBuilder(Application::class)->disableOriginalConstructor()->getMock();
$app = $this->createStub(Application::class);
$plugin->bootstrap($app);
}

Expand All @@ -41,7 +41,7 @@ public function testBootstrapWithConfig()
];
Configure::write('Queue', ['default' => $queueConfig]);
$plugin = new QueuePlugin();
$app = $this->getMockBuilder(Application::class)->disableOriginalConstructor()->getMock();
$app = $this->createStub(Application::class);
$plugin->bootstrap($app);
$queueConfig['url'] = [
'transport' => 'null:',
Expand Down
84 changes: 61 additions & 23 deletions tests/TestCase/Queue/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,39 @@
use PHPUnit\Framework\Attributes\DataProvider;
use TestApp\TestProcessor;
use TestApp\WelcomeMailer;
use Traversable;

class ProcessorTest extends TestCase
{
use QueueTestTrait;

public static $lastProcessMessage;
/**
* Convert EventList to array in a backwards-compatible way.
*
* In CakePHP 5.3.0+ EventList implements Traversable but array access is deprecated.
* In older versions, EventList only supports array access.
*
* @param \Cake\Event\EventList $events The event list to convert.
* @return array<\Cake\Event\EventInterface>
*/
protected function eventListToArray(EventList $events): array
{
if ($events instanceof Traversable) {
/** @var array<\Cake\Event\EventInterface> */
return iterator_to_array($events);
}

$result = [];
$count = $events->count();
for ($i = 0; $i < $count; $i++) {
$event = $events[$i];
if ($event !== null) {
$result[] = $event;
}
}

return $result;
}

/**
* Data provider for testProcess method
Expand All @@ -59,25 +86,27 @@ public static function dataProviderTestProcess(): array
* @param string $jobMethod The method name to run
* @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.
* @param string $dispatchedEvent The dispatched event based on process result.
* @return void
*/
#[DataProvider('dataProviderTestProcess')]
public function testProcess($jobMethod, $expected, $logMessage, $dispatchedEvent)
public function testProcess(string $jobMethod, string $expected, string $logMessage, string $dispatchedEvent): void
{
$messageBody = [
'class' => [TestProcessor::class, $jobMethod],
'args' => [],
];
$connectionFactory = new NullConnectionFactory();
$context = $connectionFactory->createContext();
$queueMessage = new NullMessage(json_encode($messageBody));
$queueMessage = new NullMessage((string)json_encode($messageBody));
$message = new Message($queueMessage, $context);

$events = new EventList();
$logger = new ArrayLog();
$processor = new Processor($logger);
$processor->getEventManager()->setEventList($events);
/** @var \Cake\Event\EventManager $eventManager */
$eventManager = $processor->getEventManager();
$eventManager->setEventList($events);

$actual = $processor->process($queueMessage, $context);
$this->assertSame($expected, $actual);
Expand All @@ -88,17 +117,18 @@ public function testProcess($jobMethod, $expected, $logMessage, $dispatchedEvent
$this->assertStringContainsString($logMessage, $logs[0]);

$this->assertSame(3, $events->count());
$this->assertSame('Processor.message.seen', $events[0]->getName());
$this->assertEquals(['queueMessage' => $queueMessage], $events[0]->getData());
$eventsList = $this->eventListToArray($events);
$this->assertSame('Processor.message.seen', $eventsList[0]->getName());
$this->assertEquals(['queueMessage' => $queueMessage], $eventsList[0]->getData());

// Events should contain a message with the same payload.
$this->assertSame('Processor.message.start', $events[1]->getName());
$data = $events[1]->getData();
$this->assertSame('Processor.message.start', $eventsList[1]->getName());
$data = $eventsList[1]->getData();
$this->assertArrayHasKey('message', $data);
$this->assertSame($message->jsonSerialize(), $data['message']->jsonSerialize());

$this->assertSame($dispatchedEvent, $events[2]->getName());
$data = $events[2]->getData();
$this->assertSame($dispatchedEvent, $eventsList[2]->getName());
$data = $eventsList[2]->getData();
$this->assertArrayHasKey('message', $data);
$this->assertSame($message->jsonSerialize(), $data['message']->jsonSerialize());

Expand All @@ -121,12 +151,14 @@ public function testProcessNotValidCallable()
];
$connectionFactory = new NullConnectionFactory();
$context = $connectionFactory->createContext();
$queueMessage = new NullMessage(json_encode($messageBody));
$queueMessage = new NullMessage((string)json_encode($messageBody));

$events = new EventList();
$logger = new ArrayLog();
$processor = new Processor($logger);
$processor->getEventManager()->setEventList($events);
/** @var \Cake\Event\EventManager $eventManager */
$eventManager = $processor->getEventManager();
$eventManager->setEventList($events);

$result = $processor->process($queueMessage, $context);
$this->assertSame(InteropProcessor::REJECT, $result);
Expand All @@ -137,8 +169,9 @@ public function testProcessNotValidCallable()
$this->assertStringContainsString('Invalid callable for message. Rejecting message from queue', $logs[0]);

$this->assertSame(2, $events->count());
$this->assertSame('Processor.message.seen', $events[0]->getName());
$this->assertSame('Processor.message.invalid', $events[1]->getName());
$eventsList = $this->eventListToArray($events);
$this->assertSame('Processor.message.seen', $eventsList[0]->getName());
$this->assertSame('Processor.message.invalid', $eventsList[1]->getName());
}

/**
Expand All @@ -156,20 +189,23 @@ public function testProcessWillRequeueOnException()
];
$connectionFactory = new NullConnectionFactory();
$context = $connectionFactory->createContext();
$queueMessage = new NullMessage(json_encode($messageBody));
$queueMessage = new NullMessage((string)json_encode($messageBody));

$events = new EventList();
$logger = new ArrayLog();
$processor = new Processor($logger);
$processor->getEventManager()->setEventList($events);
/** @var \Cake\Event\EventManager $eventManager */
$eventManager = $processor->getEventManager();
$eventManager->setEventList($events);

$result = $processor->process($queueMessage, $context);
$this->assertEquals(InteropProcessor::REQUEUE, $result);

// Verify timing information is present in exception event
$this->assertSame(3, $events->count());
$this->assertSame('Processor.message.exception', $events[2]->getName());
$data = $events[2]->getData();
$eventsList = $this->eventListToArray($events);
$this->assertSame('Processor.message.exception', $eventsList[2]->getName());
$data = $eventsList[2]->getData();
$this->assertArrayHasKey('duration', $data);
$this->assertIsInt($data['duration']);
$this->assertGreaterThanOrEqual(0, $data['duration']);
Expand All @@ -193,11 +229,13 @@ public function testProcessJobObject()
];
$connectionFactory = new NullConnectionFactory();
$context = $connectionFactory->createContext();
$queueMessage = new NullMessage(json_encode($messageBody));
$queueMessage = new NullMessage((string)json_encode($messageBody));
$processor = new Processor();

$result = $processor->process($queueMessage, $context);
$logs = Log::engine('debug')->read();
/** @var \Cake\Log\Engine\ArrayLog $debugLog */
$debugLog = Log::engine('debug');
$logs = $debugLog->read();
Log::drop('debug');

$this->assertCount(1, $logs);
Expand All @@ -219,12 +257,12 @@ public function testProcessMessage()
];
$connectionFactory = new NullConnectionFactory();
$context = $connectionFactory->createContext();
$queueMessage = new NullMessage(json_encode($messageBody));
$queueMessage = new NullMessage((string)json_encode($messageBody));
$message = new Message($queueMessage, $context);
$processor = new Processor();

$result = $processor->processMessage($message);
$this->assertSame(InteropProcessor::ACK, $result);
$this->assertNotEmpty(TestProcessor::$lastProcessMessage);
$this->assertInstanceOf(Message::class, TestProcessor::$lastProcessMessage);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/QueueManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testSetMultipleConfigs()
public function testSetConfigWithInvalidConfigValue()
{
$this->expectException(LogicException::class);
QueueManager::setConfig('test', null);
QueueManager::setConfig('test');
}

public function testSetConfigInvalidKeyValue()
Expand Down