|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Logger; |
|
13
|
|
|
|
|
14
|
|
|
use Mockery as m; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Psr\Log\NullLogger; |
|
18
|
|
|
use Spiral\Core\Container; |
|
19
|
|
|
use Spiral\Core\ContainerScope; |
|
20
|
|
|
use Spiral\Logger\LogsInterface; |
|
21
|
|
|
use Spiral\Logger\Traits\LoggerTrait; |
|
22
|
|
|
|
|
23
|
|
|
class TraitTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
use LoggerTrait; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->logger = null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testNoScope(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$logger = $this->getLogger(); |
|
35
|
|
|
$this->assertInstanceOf(NullLogger::class, $this->getLogger()); |
|
36
|
|
|
$this->assertSame($logger, $this->getLogger()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testSetLogger(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$logger = new NullLogger(); |
|
42
|
|
|
$this->setLogger($logger); |
|
43
|
|
|
$this->assertSame($logger, $this->getLogger()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testGetsLoggerWhenChannelNotPassedAndContainerExistsButItDoesNotHaveLogsInterface(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$container = new Container(); |
|
49
|
|
|
|
|
50
|
|
|
ContainerScope::runScope($container, function (): void { |
|
51
|
|
|
$this->assertInstanceOf(NullLogger::class, $this->getLogger()); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testGetsLoggerWhenChannelNotPassedAndContainerExistsAndLogsInterfaceHasLogger(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$logsInterfaceLogger = new NullLogger(); |
|
58
|
|
|
$logs = m::mock(LogsInterface::class); |
|
59
|
|
|
$logs->shouldReceive('getLogger') |
|
60
|
|
|
->with(static::class) |
|
61
|
|
|
->andReturn($logsInterfaceLogger); |
|
62
|
|
|
|
|
63
|
|
|
$container = new Container(); |
|
64
|
|
|
$container->bind(LogsInterface::class, $logs); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
ContainerScope::runScope($container, function () use ($logsInterfaceLogger): void { |
|
67
|
|
|
$this->assertEquals($logsInterfaceLogger, $this->getLogger()); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testGetsLoggerWhenChannelPassedAndContainerDoesNotExist(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->assertInstanceOf(NullLogger::class, $this->getLogger('test-channel')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testGetsLoggerWhenChannelPassedAndContainerExistsButItDoesNotHaveLogsInterface(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$container = new Container(); |
|
79
|
|
|
|
|
80
|
|
|
ContainerScope::runScope($container, function (): void { |
|
81
|
|
|
$this->assertInstanceOf(NullLogger::class, $this->getLogger('test-channel')); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testGetsLoggerWhenChannelPassedAndContainerExistsAndLogsInterfaceHasLogger(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$logsInterfaceLogger = new NullLogger(); |
|
88
|
|
|
$logs = m::mock(LogsInterface::class); |
|
89
|
|
|
$logs->shouldReceive('getLogger') |
|
90
|
|
|
->with('test-channel') |
|
91
|
|
|
->andReturn($logsInterfaceLogger); |
|
92
|
|
|
|
|
93
|
|
|
$container = new Container(); |
|
94
|
|
|
$container->bind(LogsInterface::class, $logs); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
ContainerScope::runScope($container, function () use ($logsInterfaceLogger): void { |
|
97
|
|
|
$this->assertEquals($logsInterfaceLogger, $this->getLogger('test-channel')); |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testGetsLoggerWhenChannelPassedAndLoggerSetButContainerDoesNotExists(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$logger = m::mock(LoggerInterface::class); |
|
104
|
|
|
$this->setLogger($logger); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$this->assertEquals($logger, $this->getLogger('test-channel')); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testGetsLoggerWhenChannelPassedAndLoggerSetAndContainerExistsButItDoesNotHaveLogsInterface(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$logger = m::mock(LoggerInterface::class); |
|
112
|
|
|
$this->setLogger($logger); |
|
|
|
|
|
|
113
|
|
|
$container = new Container(); |
|
114
|
|
|
|
|
115
|
|
|
ContainerScope::runScope($container, function () use ($logger): void { |
|
116
|
|
|
$this->assertEquals($logger, $this->getLogger('test-channel')); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|