LogTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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()
  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)->andReturn(['type' => 'file', 'path' => $root->url()]);
  50. $this->log->info('foo');
  51. $this->assertEquals($this->log->getLog(), ['info' => ['foo']]);
  52. $this->log->clear();
  53. $this->assertEmpty($this->log->getLog());
  54. $this->log->error('foo');
  55. $this->assertArrayHasKey('error', $this->log->getLog());
  56. $this->log->emergency('foo');
  57. $this->assertArrayHasKey('emergency', $this->log->getLog());
  58. $this->log->alert('foo');
  59. $this->assertArrayHasKey('alert', $this->log->getLog());
  60. $this->log->critical('foo');
  61. $this->assertArrayHasKey('critical', $this->log->getLog());
  62. $this->log->warning('foo');
  63. $this->assertArrayHasKey('warning', $this->log->getLog());
  64. $this->log->notice('foo');
  65. $this->assertArrayHasKey('notice', $this->log->getLog());
  66. $this->log->debug('foo');
  67. $this->assertArrayHasKey('debug', $this->log->getLog());
  68. $this->log->sql('foo');
  69. $this->assertArrayHasKey('sql', $this->log->getLog());
  70. $this->log->custom('foo');
  71. $this->assertArrayHasKey('custom', $this->log->getLog());
  72. $this->log->write('foo');
  73. $this->assertTrue($root->hasChildren());
  74. $this->assertEmpty($this->log->getLog());
  75. $this->log->close();
  76. $this->log->info('foo');
  77. $this->assertEmpty($this->log->getLog());
  78. }
  79. public function testSave()
  80. {
  81. $root = vfsStream::setup();
  82. $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
  83. $this->config->shouldReceive('get')->with("log.channels.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
  84. $this->log->info('foo');
  85. $this->log->save();
  86. $this->assertTrue($root->hasChildren());
  87. }
  88. }