LogTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\App;
  9. use think\Config;
  10. use think\Container;
  11. use think\Log;
  12. use think\log\ChannelSet;
  13. class LogTest extends TestCase
  14. {
  15. /** @var App|MockInterface */
  16. protected $app;
  17. /** @var Log|MockInterface */
  18. protected $log;
  19. /** @var Config|MockInterface */
  20. protected $config;
  21. protected function tearDown(): void
  22. {
  23. m::close();
  24. }
  25. protected function setUp()
  26. {
  27. $this->app = m::mock(App::class)->makePartial();
  28. Container::setInstance($this->app);
  29. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  30. $this->config = m::mock(Config::class)->makePartial();
  31. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  32. $this->app->shouldReceive('runningInConsole')->andReturn(false);
  33. $this->log = new Log($this->app);
  34. }
  35. public function testGetConfig()
  36. {
  37. $config = [
  38. 'default' => 'file',
  39. ];
  40. $this->config->shouldReceive('get')->with('log')->andReturn($config);
  41. $this->assertEquals($config, $this->log->getConfig());
  42. $this->expectException(InvalidArgumentException::class);
  43. $this->log->getChannelConfig('foo');
  44. }
  45. public function testChannel()
  46. {
  47. $this->assertInstanceOf(ChannelSet::class, $this->log->channel(['file', 'mail']));
  48. }
  49. public function testLogManagerInstances()
  50. {
  51. $this->config->shouldReceive('get')->with("log.channels.single", null)->andReturn(['type' => 'file']);
  52. $channel1 = $this->log->channel('single');
  53. $channel2 = $this->log->channel('single');
  54. $this->assertSame($channel1, $channel2);
  55. }
  56. public function testFileLog()
  57. {
  58. $root = vfsStream::setup();
  59. $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
  60. $this->config->shouldReceive('get')->with("log.channels.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
  61. $this->log->info('foo');
  62. $this->assertEquals($this->log->getLog(), ['info' => ['foo']]);
  63. $this->log->clear();
  64. $this->assertEmpty($this->log->getLog());
  65. $this->log->error('foo');
  66. $this->assertArrayHasKey('error', $this->log->getLog());
  67. $this->log->emergency('foo');
  68. $this->assertArrayHasKey('emergency', $this->log->getLog());
  69. $this->log->alert('foo');
  70. $this->assertArrayHasKey('alert', $this->log->getLog());
  71. $this->log->critical('foo');
  72. $this->assertArrayHasKey('critical', $this->log->getLog());
  73. $this->log->warning('foo');
  74. $this->assertArrayHasKey('warning', $this->log->getLog());
  75. $this->log->notice('foo');
  76. $this->assertArrayHasKey('notice', $this->log->getLog());
  77. $this->log->debug('foo');
  78. $this->assertArrayHasKey('debug', $this->log->getLog());
  79. $this->log->sql('foo');
  80. $this->assertArrayHasKey('sql', $this->log->getLog());
  81. $this->log->custom('foo');
  82. $this->assertArrayHasKey('custom', $this->log->getLog());
  83. $this->log->write('foo');
  84. $this->assertTrue($root->hasChildren());
  85. $this->assertEmpty($this->log->getLog());
  86. $this->log->close();
  87. $this->log->info('foo');
  88. $this->assertEmpty($this->log->getLog());
  89. }
  90. public function testSave()
  91. {
  92. $root = vfsStream::setup();
  93. $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
  94. $this->config->shouldReceive('get')->with("log.channels.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
  95. $this->log->info('foo');
  96. $this->log->save();
  97. $this->assertTrue($root->hasChildren());
  98. }
  99. }