Completed
Push — 6.0 ( cb8a0b...bb9f45 )
by yun
06:00
created

LogTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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\App;
11
use think\Config;
12
use think\Container;
13
use think\Log;
14
use think\log\ChannelSet;
15
16
class LogTest extends TestCase
17
{
18
    /** @var App|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
19
    protected $app;
20
21
    /** @var Log|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    protected $log;
23
24
    /** @var Config|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    protected $config;
26
27
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
28
    {
29
        m::close();
30
    }
31
32
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
33
    {
34
        $this->app = m::mock(App::class)->makePartial();
35
36
        $this->app = m::mock(App::class)->makePartial();
37
        Container::setInstance($this->app);
38
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
39
        $this->config = m::mock(Config::class)->makePartial();
40
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
41
        $this->app->shouldReceive('runningInConsole')->andReturn(false);
42
43
        $this->log = new Log($this->app);
0 ignored issues
show
Bug introduced by
$this->app of type Mockery\Mock is incompatible with the type think\App expected by parameter $app of think\Log::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $this->log = new Log(/** @scrutinizer ignore-type */ $this->app);
Loading history...
44
    }
45
46
    public function testGetConfig()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGetConfig()
Loading history...
47
    {
48
        $config = [
49
            'default' => 'file',
50
        ];
51
52
        $this->config->shouldReceive('get')->with('log')->andReturn($config);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->config->/** @scrutinizer ignore-call */ 
53
                       shouldReceive('get')->with('log')->andReturn($config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        $this->assertEquals($config, $this->log->getConfig());
55
56
        $this->expectException(InvalidArgumentException::class);
57
        $this->log->getChannelConfig('foo');
58
    }
59
60
    public function testChannel()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testChannel()
Loading history...
61
    {
62
        $this->assertInstanceOf(ChannelSet::class, $this->log->channel(['file', 'mail']));
63
    }
64
65
    public function testLogManagerInstances()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testLogManagerInstances()
Loading history...
66
    {
67
        $this->config->shouldReceive('get')->with("log.channels.single", null)->andReturn(['type' => 'file']);
68
69
        $channel1 = $this->log->channel('single');
70
        $channel2 = $this->log->channel('single');
71
72
        $this->assertSame($channel1, $channel2);
73
    }
74
75
    public function testFileLog()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFileLog()
Loading history...
76
    {
77
        $root = vfsStream::setup();
78
79
        $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
80
81
        $this->config->shouldReceive('get')->with("log.channels.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
82
83
        $this->log->info('foo');
84
85
        $this->assertEquals($this->log->getLog(), ['info' => ['foo']]);
86
87
        $this->log->clear();
88
89
        $this->assertEmpty($this->log->getLog());
90
91
        $this->log->error('foo');
92
        $this->assertArrayHasKey('error', $this->log->getLog());
93
94
        $this->log->emergency('foo');
95
        $this->assertArrayHasKey('emergency', $this->log->getLog());
96
97
        $this->log->alert('foo');
98
        $this->assertArrayHasKey('alert', $this->log->getLog());
99
100
        $this->log->critical('foo');
101
        $this->assertArrayHasKey('critical', $this->log->getLog());
102
103
        $this->log->warning('foo');
104
        $this->assertArrayHasKey('warning', $this->log->getLog());
105
106
        $this->log->notice('foo');
107
        $this->assertArrayHasKey('notice', $this->log->getLog());
108
109
        $this->log->debug('foo');
110
        $this->assertArrayHasKey('debug', $this->log->getLog());
111
112
        $this->log->sql('foo');
113
        $this->assertArrayHasKey('sql', $this->log->getLog());
114
115
        $this->log->custom('foo');
0 ignored issues
show
Bug introduced by
The method custom() does not exist on think\Log. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $this->log->/** @scrutinizer ignore-call */ 
116
                    custom('foo');
Loading history...
116
        $this->assertArrayHasKey('custom', $this->log->getLog());
117
118
        $this->log->write('foo');
119
        $this->assertTrue($root->hasChildren());
120
        $this->assertEmpty($this->log->getLog());
121
122
        $this->log->close();
123
124
        $this->log->info('foo');
125
126
        $this->assertEmpty($this->log->getLog());
127
    }
128
129
    public function testSave()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSave()
Loading history...
130
    {
131
        $root = vfsStream::setup();
132
133
        $this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
134
135
        $this->config->shouldReceive('get')->with("log.channels.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
136
137
        $this->log->info('foo');
138
139
        $this->log->save();
140
141
        $this->assertTrue($root->hasChildren());
142
    }
143
144
}
145