MongoDbTargetTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace yiiunit\extensions\mongodb\log;
  3. use yii\log\Logger;
  4. use yii\mongodb\log\MongoDbTarget;
  5. use yiiunit\extensions\mongodb\TestCase;
  6. class MongoDbTargetTest extends TestCase
  7. {
  8. protected function tearDown()
  9. {
  10. $this->dropCollection('log');
  11. parent::tearDown();
  12. }
  13. /**
  14. * @return MongoDbTarget test log target
  15. */
  16. protected function createLogTarget()
  17. {
  18. return new MongoDbTarget([
  19. 'db' => $this->getConnection(),
  20. ]);
  21. }
  22. // Tests :
  23. public function testExport()
  24. {
  25. $target = $this->createLogTarget();
  26. $target->messages = [
  27. [
  28. 'test',
  29. Logger::LEVEL_WARNING,
  30. 'test',
  31. time() - 1,
  32. ],
  33. [
  34. 'info',
  35. Logger::LEVEL_INFO,
  36. 'test',
  37. time(),
  38. ]
  39. ];
  40. $target->export();
  41. $rows = $this->findAll($this->mongodb->getCollection($target->logCollection));
  42. $this->assertCount(2, $rows);
  43. $this->assertEquals($target->messages[0][0], $rows[0]['message']);
  44. $this->assertEquals($target->messages[0][1], $rows[0]['level']);
  45. $this->assertEquals($target->messages[0][2], $rows[0]['category']);
  46. $this->assertEquals($target->messages[0][3], $rows[0]['log_time']);
  47. $this->assertEquals($target->messages[1][0], $rows[1]['message']);
  48. $this->assertEquals($target->messages[1][1], $rows[1]['level']);
  49. $this->assertEquals($target->messages[1][2], $rows[1]['category']);
  50. $this->assertEquals($target->messages[1][3], $rows[1]['log_time']);
  51. }
  52. }