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