LogTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace think\tests;
  3. use InvalidArgumentException;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use org\bovigo\vfs\vfsStream;
  7. use PHPUnit\Framework\TestCase;
  8. use think\Log;
  9. use think\log\ChannelSet;
  10. class LogTest extends TestCase
  11. {
  12. use InteractsWithApp;
  13. /** @var Log|MockInterface */
  14. protected $log;
  15. protected function tearDown(): void
  16. {
  17. m::close();
  18. }
  19. protected function setUp(): void
  20. {
  21. $this->prepareApp();
  22. $this->log = new Log($this->app);
  23. }
  24. public function testGetConfig()
  25. {
  26. $config = [
  27. 'default' => 'file',
  28. ];
  29. $this->config->shouldReceive('get')->with('log')->andReturn($config);
  30. $this->assertEquals($config, $this->log->getConfig());
  31. $this->expectException(InvalidArgumentException::class);
  32. $this->log->getChannelConfig('foo');
  33. }
  34. public function testChannel()
  35. {
  36. $this->assertInstanceOf(ChannelSet::class, $this->log->channel(['file', 'mail']));
  37. }
  38. public function testLogManagerInstances()
  39. {
  40. $this->config->shouldReceive('get')->with("log.channels.single", null)->andReturn(['type' => 'file']);
  41. $channel1 = $this->log->channel('single');
  42. $channel2 = $this->log->channel('single');
  43. $this->assertSame($channel1, $channel2);
  44. }
  45. public function testFileLog()
  46. {
  47. $root = vfsStream::setup();
  48. $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
  49. $this->config->shouldReceive('get')->with("log.channels.file", null)
  50. ->andReturn(['type' => 'file', 'path' => $root->url()]);
  51. $this->log->info('foo');
  52. $this->assertEquals([['info', 'foo']], array_map(fn($log) => [$log->type, $log->message], $this->log->getLog()));
  53. $this->log->clear();
  54. $this->assertEmpty($this->log->getLog());
  55. $this->log->error('foo');
  56. $this->log->emergency('foo');
  57. $this->log->alert('foo');
  58. $this->log->critical('foo');
  59. $this->log->warning('foo');
  60. $this->log->notice('foo');
  61. $this->log->debug('foo');
  62. $this->log->sql('foo');
  63. $this->log->custom('foo');
  64. $this->assertEquals([
  65. ['error', 'foo'],
  66. ['emergency', 'foo'],
  67. ['alert', 'foo'],
  68. ['critical', 'foo'],
  69. ['warning', 'foo'],
  70. ['notice', 'foo'],
  71. ['debug', 'foo'],
  72. ['sql', 'foo'],
  73. ['custom', 'foo'],
  74. ], array_map(fn($log) => [$log->type, $log->message], $this->log->getLog()));
  75. $this->log->write('foo');
  76. $this->assertTrue($root->hasChildren());
  77. $this->assertEmpty($this->log->getLog());
  78. $this->log->close();
  79. $this->log->info('foo');
  80. $this->assertEmpty($this->log->getLog());
  81. }
  82. public function testSave()
  83. {
  84. $root = vfsStream::setup();
  85. $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
  86. $this->config->shouldReceive('get')->with("log.channels.file", null)
  87. ->andReturn(['type' => 'file', 'path' => $root->url()]);
  88. $this->log->info('foo');
  89. $this->log->save();
  90. $this->assertTrue($root->hasChildren());
  91. }
  92. }