MongoDBFormatterTest.php 9.2 KB

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