Completed
Branch 6.0 (d30585)
by yun
04:17
created

LogTest::testFileLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 9.424

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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