MongoDBFormatterTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. use MongoDB\BSON\ObjectId;
  12. use MongoDB\BSON\Regex;
  13. use MongoDB\BSON\UTCDateTime;
  14. use Monolog\Logger;
  15. use Monolog\Test\TestCase;
  16. /**
  17. * @author Florian Plattner <me@florianplattner.de>
  18. */
  19. class MongoDBFormatterTest extends TestCase
  20. {
  21. public function setUp(): void
  22. {
  23. if (!class_exists('MongoDB\BSON\UTCDateTime')) {
  24. $this->markTestSkipped('ext-mongodb not installed');
  25. }
  26. }
  27. public function constructArgumentProvider()
  28. {
  29. return [
  30. [1, true, 1, true],
  31. [0, false, 0, false],
  32. ];
  33. }
  34. /**
  35. * @param $traceDepth
  36. * @param $traceAsString
  37. * @param $expectedTraceDepth
  38. * @param $expectedTraceAsString
  39. *
  40. * @dataProvider constructArgumentProvider
  41. */
  42. public function testConstruct($traceDepth, $traceAsString, $expectedTraceDepth, $expectedTraceAsString)
  43. {
  44. $formatter = new MongoDBFormatter($traceDepth, $traceAsString);
  45. $reflTrace = new \ReflectionProperty($formatter, 'exceptionTraceAsString');
  46. $reflTrace->setAccessible(true);
  47. $this->assertEquals($expectedTraceAsString, $reflTrace->getValue($formatter));
  48. $reflDepth = new \ReflectionProperty($formatter, 'maxNestingLevel');
  49. $reflDepth->setAccessible(true);
  50. $this->assertEquals($expectedTraceDepth, $reflDepth->getValue($formatter));
  51. }
  52. public function testSimpleFormat()
  53. {
  54. $record = $this->getRecord(
  55. message: 'some log message',
  56. level: Logger::WARNING,
  57. channel: 'test',
  58. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  59. );
  60. $formatter = new MongoDBFormatter();
  61. $formattedRecord = $formatter->format($record);
  62. $this->assertCount(7, $formattedRecord);
  63. $this->assertEquals('some log message', $formattedRecord['message']);
  64. $this->assertEquals([], $formattedRecord['context']);
  65. $this->assertEquals(Logger::WARNING, $formattedRecord['level']);
  66. $this->assertEquals(Logger::getLevelName(Logger::WARNING), $formattedRecord['level_name']);
  67. $this->assertEquals('test', $formattedRecord['channel']);
  68. $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $formattedRecord['datetime']);
  69. $this->assertEquals('1453410690123', $formattedRecord['datetime']->__toString());
  70. $this->assertEquals([], $formattedRecord['extra']);
  71. }
  72. public function testRecursiveFormat()
  73. {
  74. $someObject = new \stdClass();
  75. $someObject->foo = 'something';
  76. $someObject->bar = 'stuff';
  77. $record = $this->getRecord(
  78. message: 'some log message',
  79. context: [
  80. 'stuff' => new \DateTimeImmutable('1969-01-21T21:11:30.213000+00:00'),
  81. 'some_object' => $someObject,
  82. 'context_string' => 'some string',
  83. 'context_int' => 123456,
  84. 'except' => new \Exception('exception message', 987),
  85. ],
  86. level: Logger::WARNING,
  87. channel: 'test',
  88. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.213000+00:00'),
  89. );
  90. $formatter = new MongoDBFormatter();
  91. $formattedRecord = $formatter->format($record);
  92. $this->assertCount(5, $formattedRecord['context']);
  93. $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $formattedRecord['context']['stuff']);
  94. $this->assertEquals('-29731710213', $formattedRecord['context']['stuff']->__toString());
  95. $this->assertEquals(
  96. [
  97. 'foo' => 'something',
  98. 'bar' => 'stuff',
  99. 'class' => 'stdClass',
  100. ],
  101. $formattedRecord['context']['some_object']
  102. );
  103. $this->assertEquals('some string', $formattedRecord['context']['context_string']);
  104. $this->assertEquals(123456, $formattedRecord['context']['context_int']);
  105. $this->assertCount(5, $formattedRecord['context']['except']);
  106. $this->assertEquals('exception message', $formattedRecord['context']['except']['message']);
  107. $this->assertEquals(987, $formattedRecord['context']['except']['code']);
  108. $this->assertIsString($formattedRecord['context']['except']['file']);
  109. $this->assertIsInt($formattedRecord['context']['except']['code']);
  110. $this->assertIsString($formattedRecord['context']['except']['trace']);
  111. $this->assertEquals('Exception', $formattedRecord['context']['except']['class']);
  112. }
  113. public function testFormatDepthArray()
  114. {
  115. $record = $this->getRecord(
  116. message: 'some log message',
  117. context: [
  118. 'nest2' => [
  119. 'property' => 'anything',
  120. 'nest3' => [
  121. 'nest4' => 'value',
  122. 'property' => 'nothing',
  123. ],
  124. ],
  125. ],
  126. level: Logger::WARNING,
  127. channel: 'test',
  128. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  129. );
  130. $formatter = new MongoDBFormatter(2);
  131. $formattedResult = $formatter->format($record);
  132. $this->assertEquals(
  133. [
  134. 'nest2' => [
  135. 'property' => 'anything',
  136. 'nest3' => '[...]',
  137. ],
  138. ],
  139. $formattedResult['context']
  140. );
  141. }
  142. public function testFormatDepthArrayInfiniteNesting()
  143. {
  144. $record = $this->getRecord(
  145. message: 'some log message',
  146. context: [
  147. 'nest2' => [
  148. 'property' => 'something',
  149. 'nest3' => [
  150. 'property' => 'anything',
  151. 'nest4' => [
  152. 'property' => 'nothing',
  153. ],
  154. ],
  155. ],
  156. ],
  157. level: Logger::WARNING,
  158. channel: 'test',
  159. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  160. );
  161. $formatter = new MongoDBFormatter(0);
  162. $formattedResult = $formatter->format($record);
  163. $this->assertEquals(
  164. [
  165. 'nest2' => [
  166. 'property' => 'something',
  167. 'nest3' => [
  168. 'property' => 'anything',
  169. 'nest4' => [
  170. 'property' => 'nothing',
  171. ],
  172. ],
  173. ],
  174. ],
  175. $formattedResult['context']
  176. );
  177. }
  178. public function testFormatDepthObjects()
  179. {
  180. $someObject = new \stdClass();
  181. $someObject->property = 'anything';
  182. $someObject->nest3 = new \stdClass();
  183. $someObject->nest3->property = 'nothing';
  184. $someObject->nest3->nest4 = 'invisible';
  185. $record = $this->getRecord(
  186. message: 'some log message',
  187. context: [
  188. 'nest2' => $someObject,
  189. ],
  190. level: Logger::WARNING,
  191. channel: 'test',
  192. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  193. );
  194. $formatter = new MongoDBFormatter(2, true);
  195. $formattedResult = $formatter->format($record);
  196. $this->assertEquals(
  197. [
  198. 'nest2' => [
  199. 'property' => 'anything',
  200. 'nest3' => '[...]',
  201. 'class' => 'stdClass',
  202. ],
  203. ],
  204. $formattedResult['context']
  205. );
  206. }
  207. public function testFormatDepthException()
  208. {
  209. $record = $this->getRecord(
  210. message: 'some log message',
  211. context: [
  212. 'nest2' => new \Exception('exception message', 987),
  213. ],
  214. level: Logger::WARNING,
  215. channel: 'test',
  216. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  217. );
  218. $formatter = new MongoDBFormatter(2, false);
  219. $formattedRecord = $formatter->format($record);
  220. $this->assertEquals('exception message', $formattedRecord['context']['nest2']['message']);
  221. $this->assertEquals(987, $formattedRecord['context']['nest2']['code']);
  222. $this->assertEquals('[...]', $formattedRecord['context']['nest2']['trace']);
  223. }
  224. public function testBsonTypes()
  225. {
  226. $record = $this->getRecord(
  227. message: 'some log message',
  228. context: [
  229. 'objectid' => new ObjectId(),
  230. 'nest' => [
  231. 'timestamp' => new UTCDateTime(),
  232. 'regex' => new Regex('pattern'),
  233. ],
  234. ],
  235. level: Logger::WARNING,
  236. channel: 'test',
  237. datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
  238. );
  239. $formatter = new MongoDBFormatter();
  240. $formattedRecord = $formatter->format($record);
  241. $this->assertInstanceOf(ObjectId::class, $formattedRecord['context']['objectid']);
  242. $this->assertInstanceOf(UTCDateTime::class, $formattedRecord['context']['nest']['timestamp']);
  243. $this->assertInstanceOf(Regex::class, $formattedRecord['context']['nest']['regex']);
  244. }
  245. }