|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace G4\Runner; |
| 4 | + |
| 5 | +use G4\CleanCore\Application; |
| 6 | +use G4\Log\Logger as LogLogger; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class LoggerTest extends TestCase |
| 10 | +{ |
| 11 | + private Logger $logger; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + $this->logger = new Logger(); |
| 16 | + } |
| 17 | + |
| 18 | + public function testConstructorInitializesLogger(): void |
| 19 | + { |
| 20 | + $this->assertInstanceOf(Logger::class, $this->logger); |
| 21 | + } |
| 22 | + |
| 23 | + public function testSetLogger(): void |
| 24 | + { |
| 25 | + $logLoggerMock = $this->createMock(LogLogger::class); |
| 26 | + $this->logger->setLogger($logLoggerMock); |
| 27 | + |
| 28 | + // Test that logger is set by calling logRequest which should register shutdown function |
| 29 | + $applicationMock = $this->createMock(Application::class); |
| 30 | + $applicationMock->method('getRequest')->willReturn( |
| 31 | + $this->createMock(\G4\CleanCore\Request\Request::class) |
| 32 | + ); |
| 33 | + |
| 34 | + $this->logger->logRequest($applicationMock); |
| 35 | + |
| 36 | + // If no exception is thrown, the logger was set correctly |
| 37 | + $this->assertTrue(true); |
| 38 | + } |
| 39 | + |
| 40 | + public function testLogRequestWithoutLoggerDoesNothing(): void |
| 41 | + { |
| 42 | + $applicationMock = $this->createMock(Application::class); |
| 43 | + |
| 44 | + // Should not throw any exception |
| 45 | + $this->logger->logRequest($applicationMock); |
| 46 | + |
| 47 | + $this->assertTrue(true); |
| 48 | + } |
| 49 | + |
| 50 | + public function testLogResponseWithoutLoggerDoesNothing(): void |
| 51 | + { |
| 52 | + $applicationMock = $this->createMock(Application::class); |
| 53 | + $profilerMock = $this->createMock(Profiler::class); |
| 54 | + |
| 55 | + // Should not throw any exception |
| 56 | + $this->logger->logResponse($applicationMock, $profilerMock); |
| 57 | + |
| 58 | + $this->assertTrue(true); |
| 59 | + } |
| 60 | + |
| 61 | + public function testLogRequestWithLoggerRegistersShutdownFunction(): void |
| 62 | + { |
| 63 | + $logLoggerMock = $this->createMock(LogLogger::class); |
| 64 | + $this->logger->setLogger($logLoggerMock); |
| 65 | + |
| 66 | + $applicationMock = $this->createMock(Application::class); |
| 67 | + $requestMock = $this->createMock(\G4\CleanCore\Request\Request::class); |
| 68 | + $applicationMock->method('getRequest')->willReturn($requestMock); |
| 69 | + |
| 70 | + // This should register a shutdown function |
| 71 | + $this->logger->logRequest($applicationMock); |
| 72 | + |
| 73 | + // If no exception is thrown, the shutdown function was registered |
| 74 | + $this->assertTrue(true); |
| 75 | + } |
| 76 | + |
| 77 | + public function testLogResponseWithLoggerRegistersShutdownFunction(): void |
| 78 | + { |
| 79 | + $logLoggerMock = $this->createMock(LogLogger::class); |
| 80 | + $this->logger->setLogger($logLoggerMock); |
| 81 | + |
| 82 | + $applicationMock = $this->createMock(Application::class); |
| 83 | + $responseMock = $this->createMock(\G4\CleanCore\Response\Response::class); |
| 84 | + $applicationMock->method('getResponse')->willReturn($responseMock); |
| 85 | + |
| 86 | + $profilerMock = $this->createMock(Profiler::class); |
| 87 | + |
| 88 | + // This should register a shutdown function |
| 89 | + $this->logger->logResponse($applicationMock, $profilerMock); |
| 90 | + |
| 91 | + // If no exception is thrown, the shutdown function was registered |
| 92 | + $this->assertTrue(true); |
| 93 | + } |
| 94 | +} |
0 commit comments