MongoDBFormatterTest.php 9.2 KB

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