|
12 | 12 |
|
13 | 13 | class AccessLogHandlerTest extends TestCase |
14 | 14 | { |
| 15 | + public function testCtorWithRelativePathThrows(): void |
| 16 | + { |
| 17 | + $this->expectException(\InvalidArgumentException::class); |
| 18 | + new AccessLogHandler('../access.log'); |
| 19 | + } |
| 20 | + |
| 21 | + public function testCtorWithPathToDirectoryThrows(): void |
| 22 | + { |
| 23 | + $this->expectException(\RuntimeException::class); |
| 24 | + new AccessLogHandler(__DIR__); |
| 25 | + } |
| 26 | + |
| 27 | + public function testCtorWithPathToNewFileWillCreateNewFile(): void |
| 28 | + { |
| 29 | + $path = tempnam(sys_get_temp_dir(), 'log'); |
| 30 | + assert(is_string($path)); |
| 31 | + unlink($path); |
| 32 | + |
| 33 | + new AccessLogHandler($path); |
| 34 | + |
| 35 | + $this->assertFileExists($path); |
| 36 | + unlink($path); |
| 37 | + } |
| 38 | + |
| 39 | + public function testInvokeWithDefaultPathWillLogMessageToConsole(): void |
| 40 | + { |
| 41 | + $handler = new AccessLogHandler(); |
| 42 | + |
| 43 | + $request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']); |
| 44 | + $response = new Response(200, [], "Hello\n"); |
| 45 | + |
| 46 | + $this->expectOutputRegex('#^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d 127\.0\.0\.1 "GET /users HTTP/1\.1" 200 6 0\.0\d\d' . PHP_EOL . '$#'); |
| 47 | + $handler($request, function () use ($response) { return $response; }); |
| 48 | + } |
| 49 | + |
| 50 | + public function testInvokeWithPathToNewFileWillCreateNewFileWithLogMessage(): void |
| 51 | + { |
| 52 | + $path = tempnam(sys_get_temp_dir(), 'log'); |
| 53 | + assert(is_string($path)); |
| 54 | + unlink($path); |
| 55 | + |
| 56 | + $handler = new AccessLogHandler($path); |
| 57 | + |
| 58 | + $request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']); |
| 59 | + $response = new Response(200, [], "Hello\n"); |
| 60 | + $handler($request, function () use ($response) { return $response; }); |
| 61 | + |
| 62 | + $log = file_get_contents($path); |
| 63 | + assert(is_string($log)); |
| 64 | + |
| 65 | + if (method_exists($this, 'assertMatchesRegularExpression')) { |
| 66 | + $this->assertMatchesRegularExpression('#^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d 127\.0\.0\.1 "GET /users HTTP/1\.1" 200 6 0\.0\d\d' . PHP_EOL . '$#', $log); |
| 67 | + } else { |
| 68 | + // legacy PHPUnit < 9.1 |
| 69 | + $this->assertRegExp('#^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d 127\.0\.0\.1 "GET /users HTTP/1\.1" 200 6 0\.0\d\d' . PHP_EOL . '$#', $log); |
| 70 | + } |
| 71 | + |
| 72 | + unset($handler); |
| 73 | + unlink($path); |
| 74 | + } |
| 75 | + |
| 76 | + public function testInvokeWithPathToExistingFileWillAppendLogMessage(): void |
| 77 | + { |
| 78 | + $path = tempnam(sys_get_temp_dir(), 'log'); |
| 79 | + assert(is_string($path)); |
| 80 | + file_put_contents($path, 'first' . PHP_EOL); |
| 81 | + |
| 82 | + $handler = new AccessLogHandler($path); |
| 83 | + |
| 84 | + $request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']); |
| 85 | + $response = new Response(200, [], "Hello\n"); |
| 86 | + $handler($request, function () use ($response) { return $response; }); |
| 87 | + |
| 88 | + $log = file_get_contents($path); |
| 89 | + assert(is_string($log)); |
| 90 | + |
| 91 | + if (method_exists($this, 'assertMatchesRegularExpression')) { |
| 92 | + $this->assertMatchesRegularExpression('#^first' . PHP_EOL . '\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d 127\.0\.0\.1 "GET /users HTTP/1\.1" 200 6 0\.0\d\d' . PHP_EOL . '$#', $log); |
| 93 | + } else { |
| 94 | + // legacy PHPUnit < 9.1 |
| 95 | + $this->assertRegExp('#^first' . PHP_EOL . '\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d 127\.0\.0\.1 "GET /users HTTP/1\.1" 200 6 0\.0\d\d' . PHP_EOL . '$#', $log); |
| 96 | + } |
| 97 | + |
| 98 | + unset($handler); |
| 99 | + unlink($path); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @doesNotPerformAssertions |
| 104 | + */ |
| 105 | + public function testInvokeWithDevNullWritesNothing(): void |
| 106 | + { |
| 107 | + $handler = new AccessLogHandler(DIRECTORY_SEPARATOR !== '\\' ? '/dev/null' : __DIR__ . '\\nul'); |
| 108 | + |
| 109 | + $request = new ServerRequest('GET', 'http://localhost:8080/users', [], '', '1.1', ['REMOTE_ADDR' => '127.0.0.1']); |
| 110 | + $response = new Response(200, [], "Hello\n"); |
| 111 | + $handler($request, function () use ($response) { return $response; }); |
| 112 | + } |
| 113 | + |
15 | 114 | public function testInvokeLogsRequest(): void |
16 | 115 | { |
17 | 116 | $handler = new AccessLogHandler(); |
|
0 commit comments