Passed
Push — master ( 3312bd...c25b5e )
by Kirill
04:35
created

HandlersTest.php$0 ➔ testConstructHandler()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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(
0 ignored issues
show
Bug introduced by
new Spiral\Config\ConfigManager(new ClassNode()) of type Spiral\Config\ConfigManager is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

37
        $this->container->bind(ConfiguratorInterface::class, /** @scrutinizer ignore-type */ new ConfigManager(
Loading history...
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());
0 ignored issues
show
Bug introduced by
new Spiral\Logger\ListenerRegistry() of type Spiral\Logger\ListenerRegistry is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bindSingleton(). ( Ignorable by Annotation )

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

50
        $this->container->bindSingleton(ListenerRegistryInterface::class, /** @scrutinizer ignore-type */ new ListenerRegistry());
Loading history...
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());
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Config\MonologConfig() of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

56
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig());
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...Monolog\Logger::DEBUG)) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

65
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...rray(array('what?'))))) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

78
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...NullHandler::class))))) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

92
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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());
0 ignored issues
show
Bug introduced by
new Monolog\Handler\NullHandler() of type Monolog\Handler\NullHandler is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

108
        $this->container->bind('nullHandler', /** @scrutinizer ignore-type */ new NullHandler());
Loading history...
109
        $this->container->bind(MonologConfig::class, new MonologConfig([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...array('nullHandler')))) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

109
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...NullHandler::class))))) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

126
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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([
0 ignored issues
show
Bug introduced by
new Spiral\Monolog\Confi...\Logger::CRITICAL)))))) of type Spiral\Monolog\Config\MonologConfig is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

144
        $this->container->bind(MonologConfig::class, /** @scrutinizer ignore-type */ new MonologConfig([
Loading history...
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