From fea64fe5e8032e6d42386becdc5c9d588ac4edbf Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Fri, 22 Aug 2025 22:11:10 -0500 Subject: [PATCH] Adds unit test for Pre/PostCommandHandlerMiddleware(s) Signed-off-by: Joey Smith Signed-off-by: Joey Smith --- .../PostCommandHandlerMiddlewareTest.php | 133 ++++++++++++++++++ .../PreCommandHandlerMiddlewareTest.php | 131 +++++++++++++++++ 2 files changed, 264 insertions(+) create mode 100644 test/unit/Middleware/PostCommandHandlerMiddlewareTest.php create mode 100644 test/unit/Middleware/PreCommandHandlerMiddlewareTest.php diff --git a/test/unit/Middleware/PostCommandHandlerMiddlewareTest.php b/test/unit/Middleware/PostCommandHandlerMiddlewareTest.php new file mode 100644 index 0000000..43ab911 --- /dev/null +++ b/test/unit/Middleware/PostCommandHandlerMiddlewareTest.php @@ -0,0 +1,133 @@ +middleware = new PostCommandHandlerMiddleware(); + $this->command = $this->createMock(CommandInterface::class); + $this->handler = $this->createMock(CommandHandlerInterface::class); + } + + public function testMiddlewareImplementsCorrectInterface(): void + { + $this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); + } + + public function testProcessWithCommandResultReturnsUnwrappedResult(): void + { + $expectedResult = 'unwrapped result'; + $commandResult = new CommandResult( + $this->command, + CommandStatus::Success, + $expectedResult + ); + + // Handler should not be called when processing CommandResult + $this->handler->expects($this->never()) + ->method('handle'); + + $result = $this->middleware->process($commandResult, $this->handler); + + $this->assertEquals($expectedResult, $result); + } + + public function testProcessWithCommandResultReturnsResultForSuccessStatus(): void + { + $successResult = ['data' => 'success value']; + $commandResult = new CommandResult( + $this->command, + CommandStatus::Success, + $successResult + ); + + $result = $this->middleware->process($commandResult, $this->handler); + + $this->assertEquals($successResult, $result); + } + + public function testProcessWithCommandResultReturnsResultForFailureStatus(): void + { + $failureResult = new RuntimeException('Something went wrong'); + $commandResult = new CommandResult( + $this->command, + CommandStatus::Failure, + $failureResult + ); + + $result = $this->middleware->process($commandResult, $this->handler); + + $this->assertSame($failureResult, $result); + } + + public function testProcessWithRegularCommandDelegatesToHandler(): void + { + $expectedResult = 'handler result'; + + $this->handler->expects($this->once()) + ->method('handle') + ->with($this->identicalTo($this->command)) + ->willReturn($expectedResult); + + $result = $this->middleware->process($this->command, $this->handler); + + $this->assertEquals($expectedResult, $result); + } + + public function testProcessWithRegularCommandPassesCommandCorrectly(): void + { + $this->handler->expects($this->once()) + ->method('handle') + ->with($this->callback(function ($passedCommand) { + return $passedCommand === $this->command; + })) + ->willReturn('test result'); + + $this->middleware->process($this->command, $this->handler); + } + + public function testProcessCanHandleDifferentCommandResultTypes(): void + { + // Test with null result + $nullResult = new CommandResult($this->command, CommandStatus::Success, null); + $nullReturnValue = $this->middleware->process($nullResult, $this->handler); + $this->assertNull($nullReturnValue); + + // Test with array result + $arrayResult = new CommandResult($this->command, CommandStatus::Success, ['key' => 'value']); + $arrayReturnValue = $this->middleware->process($arrayResult, $this->handler); + $this->assertEquals(['key' => 'value'], $arrayReturnValue); + + // Test with object result + $objectResult = new CommandResult($this->command, CommandStatus::Success, new stdClass()); + $objectReturnValue = $this->middleware->process($objectResult, $this->handler); + $this->assertInstanceOf(stdClass::class, $objectReturnValue); + } +} diff --git a/test/unit/Middleware/PreCommandHandlerMiddlewareTest.php b/test/unit/Middleware/PreCommandHandlerMiddlewareTest.php new file mode 100644 index 0000000..47754dc --- /dev/null +++ b/test/unit/Middleware/PreCommandHandlerMiddlewareTest.php @@ -0,0 +1,131 @@ +middleware = new PreCommandHandlerMiddleware(); + $this->command = $this->createMock(CommandInterface::class); + $this->handler = $this->createMock(CommandHandlerInterface::class); + } + + public function testMiddlewareImplementsCorrectInterface(): void + { + $this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); + } + + public function testProcessDelegatesToHandler(): void + { + $expectedResult = 'handler result'; + + $this->handler->expects($this->once()) + ->method('handle') + ->with($this->identicalTo($this->command)) + ->willReturn($expectedResult); + + $result = $this->middleware->process($this->command, $this->handler); + + $this->assertEquals($expectedResult, $result); + } + + public function testProcessPassesCommandCorrectly(): void + { + $this->handler->expects($this->once()) + ->method('handle') + ->with($this->callback(function ($passedCommand) { + return $passedCommand === $this->command; + })) + ->willReturn('test result'); + + $this->middleware->process($this->command, $this->handler); + } + + public function testProcessReturnsHandlerResult(): void + { + $handlerResults = [ + 'string result', + ['array', 'result'], + 42, + null, + new stdClass(), + ]; + + foreach ($handlerResults as $expectedResult) { + $handler = $this->createMock(CommandHandlerInterface::class); + $handler->expects($this->once()) + ->method('handle') + ->willReturn($expectedResult); + + $result = $this->middleware->process($this->command, $handler); + + $this->assertSame($expectedResult, $result); + } + } + + public function testProcessCallsHandlerExactlyOnce(): void + { + $this->handler->expects($this->once()) + ->method('handle') + ->willReturn('result'); + + $this->middleware->process($this->command, $this->handler); + } + + public function testProcessWithDifferentCommands(): void + { + $command1 = $this->createMock(CommandInterface::class); + $command2 = $this->createMock(CommandInterface::class); + + $this->handler->expects($this->exactly(2)) + ->method('handle') + ->willReturnCallback(function ($command) use ($command1, $command2) { + if ($command === $command1) { + return 'result1'; + } + if ($command === $command2) { + return 'result2'; + } + return 'default'; + }); + + $result1 = $this->middleware->process($command1, $this->handler); + $result2 = $this->middleware->process($command2, $this->handler); + + $this->assertEquals('result1', $result1); + $this->assertEquals('result2', $result2); + } + + public function testMiddlewareCanBeInstantiatedMultipleTimes(): void + { + $middleware1 = new PreCommandHandlerMiddleware(); + $middleware2 = new PreCommandHandlerMiddleware(); + + $this->assertInstanceOf(PreCommandHandlerMiddleware::class, $middleware1); + $this->assertInstanceOf(PreCommandHandlerMiddleware::class, $middleware2); + $this->assertNotSame($middleware1, $middleware2); + } +}