|
19 | 19 |
|
20 | 20 | use Cake\Console\Arguments; |
21 | 21 | use Cake\Console\ConsoleIo; |
| 22 | +use Cake\Core\ContainerInterface; |
22 | 23 | use Cake\Queue\Command\SubprocessJobRunnerCommand; |
23 | 24 | use Cake\TestSuite\TestCase; |
24 | 25 | use Enqueue\Null\NullMessage; |
25 | 26 | use Interop\Queue\Processor; |
| 27 | +use Psr\Log\LoggerInterface; |
26 | 28 | use ReflectionClass; |
| 29 | +use RuntimeException; |
| 30 | +use stdClass; |
27 | 31 | use TestApp\Job\MultilineLogJob; |
28 | 32 | use TestApp\TestProcessor; |
29 | 33 |
|
@@ -316,4 +320,177 @@ public function testLogsRedirectedToStderr(): void |
316 | 320 | $this->assertStringContainsString('Processing step 3 completed', $stderr); |
317 | 321 | $this->assertStringContainsString('Job execution finished', $stderr); |
318 | 322 | } |
| 323 | + |
| 324 | + /** |
| 325 | + * Test defaultName method |
| 326 | + */ |
| 327 | + public function testDefaultName(): void |
| 328 | + { |
| 329 | + $this->assertSame('queue subprocess-runner', SubprocessJobRunnerCommand::defaultName()); |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * Test executeJob with invalid message class (non-existent) |
| 334 | + */ |
| 335 | + public function testExecuteJobWithInvalidMessageClass(): void |
| 336 | + { |
| 337 | + $jobData = [ |
| 338 | + 'messageClass' => 'NonExistentClass', |
| 339 | + 'body' => [ |
| 340 | + 'class' => [TestProcessor::class, 'processReturnAck'], |
| 341 | + 'args' => [], |
| 342 | + ], |
| 343 | + 'properties' => [], |
| 344 | + ]; |
| 345 | + |
| 346 | + $command = new SubprocessJobRunnerCommand(); |
| 347 | + $reflection = new ReflectionClass($command); |
| 348 | + $method = $reflection->getMethod('executeJob'); |
| 349 | + |
| 350 | + $this->expectException(RuntimeException::class); |
| 351 | + $this->expectExceptionMessage('Invalid message class'); |
| 352 | + |
| 353 | + $method->invoke($command, $jobData); |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * Test executeJob with non-QueueMessage class |
| 358 | + */ |
| 359 | + public function testExecuteJobWithNonQueueMessageClass(): void |
| 360 | + { |
| 361 | + $jobData = [ |
| 362 | + 'messageClass' => stdClass::class, |
| 363 | + 'body' => [ |
| 364 | + 'class' => [TestProcessor::class, 'processReturnAck'], |
| 365 | + 'args' => [], |
| 366 | + ], |
| 367 | + 'properties' => [], |
| 368 | + ]; |
| 369 | + |
| 370 | + $command = new SubprocessJobRunnerCommand(); |
| 371 | + $reflection = new ReflectionClass($command); |
| 372 | + $method = $reflection->getMethod('executeJob'); |
| 373 | + |
| 374 | + $this->expectException(RuntimeException::class); |
| 375 | + $this->expectExceptionMessage('Invalid message class'); |
| 376 | + |
| 377 | + $method->invoke($command, $jobData); |
| 378 | + } |
| 379 | + |
| 380 | + /** |
| 381 | + * Test configureLogging with fallback to NullLogger |
| 382 | + */ |
| 383 | + public function testConfigureLoggingConfiguresStderrLogger(): void |
| 384 | + { |
| 385 | + $jobData = ['logger' => 'stderr']; |
| 386 | + |
| 387 | + $command = new SubprocessJobRunnerCommand(); |
| 388 | + $reflection = new ReflectionClass($command); |
| 389 | + $method = $reflection->getMethod('configureLogging'); |
| 390 | + |
| 391 | + $logger = $method->invoke($command, $jobData); |
| 392 | + |
| 393 | + $this->assertInstanceOf(LoggerInterface::class, $logger); |
| 394 | + } |
| 395 | + |
| 396 | + /** |
| 397 | + * Test outputResult with valid JSON |
| 398 | + */ |
| 399 | + public function testOutputResultWithValidData(): void |
| 400 | + { |
| 401 | + $command = new SubprocessJobRunnerCommand(); |
| 402 | + $reflection = new ReflectionClass($command); |
| 403 | + $method = $reflection->getMethod('outputResult'); |
| 404 | + |
| 405 | + $io = $this->createMock(ConsoleIo::class); |
| 406 | + $io->expects($this->once()) |
| 407 | + ->method('out') |
| 408 | + ->with('{"success":true,"result":"ack"}'); |
| 409 | + |
| 410 | + $method->invoke($command, $io, ['success' => true, 'result' => 'ack']); |
| 411 | + } |
| 412 | + |
| 413 | + /** |
| 414 | + * Test outputResult with data that cannot be JSON encoded |
| 415 | + */ |
| 416 | + public function testOutputResultWithInvalidJsonData(): void |
| 417 | + { |
| 418 | + $command = new SubprocessJobRunnerCommand(); |
| 419 | + $reflection = new ReflectionClass($command); |
| 420 | + $method = $reflection->getMethod('outputResult'); |
| 421 | + |
| 422 | + $io = $this->createMock(ConsoleIo::class); |
| 423 | + $io->expects($this->never()) |
| 424 | + ->method('out'); |
| 425 | + |
| 426 | + // Create data with a resource which cannot be JSON encoded |
| 427 | + $resource = fopen('php://memory', 'r'); |
| 428 | + $this->assertIsResource($resource); |
| 429 | + $method->invoke($command, $io, ['resource' => $resource]); |
| 430 | + if (is_resource($resource)) { |
| 431 | + fclose($resource); |
| 432 | + } |
| 433 | + } |
| 434 | + |
| 435 | + /** |
| 436 | + * Test readInput with multiple chunks |
| 437 | + */ |
| 438 | + public function testReadInputWithLargeData(): void |
| 439 | + { |
| 440 | + // We can't easily mock STDIN, so we'll verify the method exists and is protected |
| 441 | + // Large data reading is already covered by the integration test (testLogsRedirectedToStderr) |
| 442 | + $reflection = new ReflectionClass(SubprocessJobRunnerCommand::class); |
| 443 | + $this->assertTrue($reflection->hasMethod('readInput')); |
| 444 | + |
| 445 | + $method = $reflection->getMethod('readInput'); |
| 446 | + $this->assertTrue($method->isProtected()); |
| 447 | + } |
| 448 | + |
| 449 | + /** |
| 450 | + * Test constructor with container |
| 451 | + */ |
| 452 | + public function testConstructorWithContainer(): void |
| 453 | + { |
| 454 | + $container = $this->createStub(ContainerInterface::class); |
| 455 | + $command = new SubprocessJobRunnerCommand($container); |
| 456 | + |
| 457 | + $this->assertInstanceOf(SubprocessJobRunnerCommand::class, $command); |
| 458 | + } |
| 459 | + |
| 460 | + /** |
| 461 | + * Test constructor without container |
| 462 | + */ |
| 463 | + public function testConstructorWithoutContainer(): void |
| 464 | + { |
| 465 | + $command = new SubprocessJobRunnerCommand(); |
| 466 | + |
| 467 | + $this->assertInstanceOf(SubprocessJobRunnerCommand::class, $command); |
| 468 | + } |
| 469 | + |
| 470 | + /** |
| 471 | + * Test executeJob when message body json_encode fails |
| 472 | + */ |
| 473 | + public function testExecuteJobWithJsonEncodeFailure(): void |
| 474 | + { |
| 475 | + // PHP's json_encode can fail with certain data (like invalid UTF-8) |
| 476 | + // However, in this code path json_encode is called on $data['body'] which is already decoded |
| 477 | + // So this edge case is hard to trigger. We'll test normal flow is covered. |
| 478 | + $jobData = [ |
| 479 | + 'messageClass' => NullMessage::class, |
| 480 | + 'body' => [ |
| 481 | + 'class' => [TestProcessor::class, 'processReturnAck'], |
| 482 | + 'args' => [], |
| 483 | + ], |
| 484 | + 'properties' => [], |
| 485 | + ]; |
| 486 | + |
| 487 | + $command = new SubprocessJobRunnerCommand(); |
| 488 | + $reflection = new ReflectionClass($command); |
| 489 | + $method = $reflection->getMethod('executeJob'); |
| 490 | + |
| 491 | + $result = $method->invoke($command, $jobData); |
| 492 | + |
| 493 | + // Verify it successfully encodes and processes |
| 494 | + $this->assertIsString($result); |
| 495 | + } |
319 | 496 | } |
0 commit comments