|
| 1 | +<?php |
| 2 | +namespace Packaged\Log\Tests; |
| 3 | + |
| 4 | +use Exception; |
| 5 | +use Packaged\Log\BasicGoogleCloudLogger; |
| 6 | +use Packaged\Log\ErrorLogLogger; |
| 7 | +use Packaged\Log\Log; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Log\LogLevel; |
| 10 | + |
| 11 | +class BasicGoogleCloudLoggerTest extends TestCase |
| 12 | +{ |
| 13 | + private $_tempFile; |
| 14 | + private $_handler; |
| 15 | + |
| 16 | + public function setUp() |
| 17 | + { |
| 18 | + $this->_tempFile = tempnam(sys_get_temp_dir(), 'packaged-log-'); |
| 19 | + $this->_handler = fopen($this->_tempFile, 'wb'); |
| 20 | + } |
| 21 | + |
| 22 | + public function tearDown() |
| 23 | + { |
| 24 | + fclose($this->_handler); |
| 25 | + unlink($this->_tempFile); |
| 26 | + } |
| 27 | + |
| 28 | + protected function _getLogContents() |
| 29 | + { |
| 30 | + return file_get_contents($this->_tempFile); |
| 31 | + } |
| 32 | + |
| 33 | + public function assertLastLog($test) |
| 34 | + { |
| 35 | + self::assertStringEndsWith($test . PHP_EOL, $this->_getLogContents()); |
| 36 | + } |
| 37 | + |
| 38 | + private function _getTestLogger($maxLevel = LogLevel::DEBUG) |
| 39 | + { |
| 40 | + $l = new BasicGoogleCloudLogger($maxLevel); |
| 41 | + $l->setHandle($this->_handler); |
| 42 | + return $l; |
| 43 | + } |
| 44 | + |
| 45 | + public function testLogger() |
| 46 | + { |
| 47 | + Log::bind($this->_getTestLogger()); |
| 48 | + |
| 49 | + Log::debug('debug: test'); |
| 50 | + self::assertLastLog('","severity":"debug","textPayload":"debug: test"}'); |
| 51 | + |
| 52 | + Log::info('info: test'); |
| 53 | + self::assertLastLog('","severity":"info","textPayload":"info: test"}'); |
| 54 | + |
| 55 | + Log::notice('notice: test'); |
| 56 | + self::assertLastLog('","severity":"notice","textPayload":"notice: test"}'); |
| 57 | + |
| 58 | + Log::warning('warning: test'); |
| 59 | + self::assertLastLog('","severity":"warning","textPayload":"warning: test"}'); |
| 60 | + |
| 61 | + Log::error('error: test'); |
| 62 | + self::assertLastLog('","severity":"error","textPayload":"error: test"}'); |
| 63 | + |
| 64 | + Log::critical('critical: test'); |
| 65 | + self::assertLastLog('","severity":"critical","textPayload":"critical: test"}'); |
| 66 | + |
| 67 | + Log::alert('alert: test'); |
| 68 | + self::assertLastLog('","severity":"alert","textPayload":"alert: test"}'); |
| 69 | + |
| 70 | + Log::emergency('emergency: test'); |
| 71 | + self::assertLastLog('","severity":"emergency","textPayload":"emergency: test"}'); |
| 72 | + } |
| 73 | + |
| 74 | + public function testLevelLog() |
| 75 | + { |
| 76 | + Log::bind($this->_getTestLogger(LogLevel::INFO)); |
| 77 | + |
| 78 | + Log::info('info: test'); |
| 79 | + self::assertLastLog('","severity":"info","textPayload":"info: test"}'); |
| 80 | + |
| 81 | + Log::debug('debug: test'); |
| 82 | + self::assertLastLog('","severity":"info","textPayload":"info: test"}'); |
| 83 | + } |
| 84 | + |
| 85 | + public function testExceptionLog() |
| 86 | + { |
| 87 | + Log::bind($this->_getTestLogger()); |
| 88 | + |
| 89 | + $e = new Exception('exception message', 123); |
| 90 | + Log::exception($e); |
| 91 | + self::assertContains(',"textPayload":"exception message"', $this->_getLogContents()); |
| 92 | + self::assertContains(',"severity":"critical"', $this->_getLogContents()); |
| 93 | + self::assertNotContains('"stace_trace":', $this->_getLogContents()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testExceptionTraceLog() |
| 97 | + { |
| 98 | + Log::bind($this->_getTestLogger()); |
| 99 | + |
| 100 | + $e = new Exception('exception message', 123); |
| 101 | + Log::exceptionWithTrace($e); |
| 102 | + self::assertContains('"textPayload":"exception message"', $this->_getLogContents()); |
| 103 | + self::assertContains('"severity":"critical"', $this->_getLogContents()); |
| 104 | + self::assertContains('"code":123', $this->_getLogContents()); |
| 105 | + self::assertContains('"line":100', $this->_getLogContents()); |
| 106 | + self::assertContains('BasicGoogleCloudLoggerTest.php', $this->_getLogContents()); |
| 107 | + self::assertContains('"stack_trace"', $this->_getLogContents()); |
| 108 | + } |
| 109 | + |
| 110 | + public function testContextLog() |
| 111 | + { |
| 112 | + Log::bind($this->_getTestLogger()); |
| 113 | + Log::debug('debug: test', ['test1' => 'value1', 'test2' => 'value2']); |
| 114 | + self::assertLastLog( |
| 115 | + '","severity":"debug","textPayload":"debug: test","jsonPayload":{"test1":"value1","test2":"value2"}}' |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments