MongoDBFormatterTest.php 9.3 KB

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