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\Monolog; |
13
|
|
|
|
14
|
|
|
use Monolog\Handler\NullHandler; |
15
|
|
|
use Monolog\Logger; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Spiral\Boot\BootloadManager; |
18
|
|
|
use Spiral\Config\ConfigManager; |
19
|
|
|
use Spiral\Config\ConfiguratorInterface; |
20
|
|
|
use Spiral\Config\LoaderInterface; |
21
|
|
|
use Spiral\Core\Container; |
22
|
|
|
use Spiral\Logger\ListenerRegistry; |
23
|
|
|
use Spiral\Logger\ListenerRegistryInterface; |
24
|
|
|
use Spiral\Logger\LogsInterface; |
25
|
|
|
use Spiral\Monolog\Bootloader\MonologBootloader; |
26
|
|
|
use Spiral\Monolog\Config\MonologConfig; |
27
|
|
|
use Spiral\Monolog\Exception\ConfigException; |
28
|
|
|
|
29
|
|
|
class HandlersTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var Container */ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
public function setUp(): void |
35
|
|
|
{ |
36
|
|
|
$this->container = new Container(); |
37
|
|
|
$this->container->bind(ConfiguratorInterface::class, new ConfigManager( |
|
|
|
|
38
|
|
|
new class() implements LoaderInterface { |
39
|
|
|
public function has(string $section): bool |
40
|
|
|
{ |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function load(string $section): array |
45
|
|
|
{ |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
)); |
50
|
|
|
$this->container->bindSingleton(ListenerRegistryInterface::class, new ListenerRegistry()); |
|
|
|
|
51
|
|
|
$this->container->get(BootloadManager::class)->bootload([MonologBootloader::class]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testNoHandlers(): void |
55
|
|
|
{ |
56
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig()); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$logger = $this->getLogger(); |
59
|
|
|
$this->assertSame('test', $logger->getName()); |
60
|
|
|
$this->assertCount(1, $logger->getHandlers()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDefaultHandler(): void |
64
|
|
|
{ |
65
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
66
|
|
|
'globalHandler' => Logger::DEBUG |
67
|
|
|
])); |
68
|
|
|
|
69
|
|
|
$logger = $this->getLogger(); |
70
|
|
|
$this->assertSame('test', $logger->getName()); |
71
|
|
|
$this->assertCount(1, $logger->getHandlers()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testInvalidHandler(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(ConfigException::class); |
77
|
|
|
|
78
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
79
|
|
|
'globalHandler' => Logger::DEBUG, |
80
|
|
|
'handlers' => [ |
81
|
|
|
'test' => [ |
82
|
|
|
['what?'] |
83
|
|
|
] |
84
|
|
|
] |
85
|
|
|
])); |
86
|
|
|
|
87
|
|
|
$this->getLogger(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testHandlerObject(): void |
91
|
|
|
{ |
92
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
93
|
|
|
'handlers' => [ |
94
|
|
|
'test' => [ |
95
|
|
|
new Container\Autowire(NullHandler::class) |
96
|
|
|
] |
97
|
|
|
] |
98
|
|
|
])); |
99
|
|
|
|
100
|
|
|
$logger = $this->getLogger(); |
101
|
|
|
|
102
|
|
|
$this->assertCount(2, $logger->getHandlers()); |
103
|
|
|
$this->assertInstanceOf(NullHandler::class, $logger->getHandlers()[0]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testBindedHandler(): void |
107
|
|
|
{ |
108
|
|
|
$this->container->bind('nullHandler', new NullHandler()); |
|
|
|
|
109
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
110
|
|
|
'handlers' => [ |
111
|
|
|
'test' => [ |
112
|
|
|
'nullHandler' |
113
|
|
|
] |
114
|
|
|
] |
115
|
|
|
])); |
116
|
|
|
|
117
|
|
|
$logger = $this->getLogger(); |
118
|
|
|
|
119
|
|
|
$this->assertCount(2, $logger->getHandlers()); |
120
|
|
|
$this->assertInstanceOf(NullHandler::class, $logger->getHandlers()[0]); |
121
|
|
|
$this->assertSame($this->container->get('nullHandler'), $logger->getHandlers()[0]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testConstructHandler(): void |
125
|
|
|
{ |
126
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
127
|
|
|
'handlers' => [ |
128
|
|
|
'test' => [ |
129
|
|
|
[ |
130
|
|
|
'class' => NullHandler::class |
131
|
|
|
] |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
])); |
135
|
|
|
|
136
|
|
|
$logger = $this->getLogger(); |
137
|
|
|
|
138
|
|
|
$this->assertCount(2, $logger->getHandlers()); |
139
|
|
|
$this->assertInstanceOf(NullHandler::class, $logger->getHandlers()[0]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testConstructWithOptionsHandler(): void |
143
|
|
|
{ |
144
|
|
|
$this->container->bind(MonologConfig::class, new MonologConfig([ |
|
|
|
|
145
|
|
|
'handlers' => [ |
146
|
|
|
'test' => [ |
147
|
|
|
[ |
148
|
|
|
'class' => NullHandler::class, |
149
|
|
|
'options' => [ |
150
|
|
|
'level' => Logger::CRITICAL |
151
|
|
|
] |
152
|
|
|
] |
153
|
|
|
] |
154
|
|
|
] |
155
|
|
|
])); |
156
|
|
|
|
157
|
|
|
$logger = $this->getLogger(); |
158
|
|
|
|
159
|
|
|
$this->assertCount(2, $logger->getHandlers()); |
160
|
|
|
$this->assertInstanceOf(NullHandler::class, $logger->getHandlers()[0]); |
161
|
|
|
$this->assertFalse($logger->getHandlers()[0]->isHandling(['level' => Logger::DEBUG])); |
162
|
|
|
$this->assertTrue($logger->getHandlers()[0]->isHandling(['level' => Logger::CRITICAL])); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
protected function getLogger(): Logger |
166
|
|
|
{ |
167
|
|
|
return $this->container->get(LogsInterface::class)->getLogger('test'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|