Passed
Push — master ( be5511...23299f )
by butschster
09:23 queued 01:59
created

testFinalizerShouldResetDefaultLogger()

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
eloc 20
nc 1
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A FactoryTest.php$1 ➔ has() 0 3 1
A FactoryTest.php$1 ➔ load() 0 3 1
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 Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
15
use Monolog\Handler\HandlerInterface;
16
use Monolog\Logger;
17
use Monolog\Processor\ProcessorInterface;
18
use Monolog\ResettableInterface;
19
use PHPUnit\Framework\TestCase;
20
use Psr\Log\LoggerInterface;
21
use Spiral\Boot\BootloadManager;
22
use Spiral\Boot\Finalizer;
23
use Spiral\Boot\FinalizerInterface;
24
use Spiral\Config\ConfigManager;
25
use Spiral\Config\ConfiguratorInterface;
26
use Spiral\Config\LoaderInterface;
27
use Spiral\Core\Container;
28
use Spiral\Logger\ListenerRegistry;
29
use Spiral\Logger\LogsInterface;
30
use Spiral\Monolog\Bootloader\MonologBootloader;
31
use Spiral\Monolog\Config\MonologConfig;
32
use Spiral\Monolog\LogFactory;
33
34
class FactoryTest extends TestCase
35
{
36
    use MockeryPHPUnitIntegration;
37
38
    public function testDefaultLogger(): void
39
    {
40
        $factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(), new Container());
41
        $logger = $factory->getLogger();
42
43
        $this->assertNotEmpty($logger);
44
        $this->assertSame($logger, $factory->getLogger());
45
        $this->assertSame($logger, $factory->getLogger(LogFactory::DEFAULT));
46
    }
47
48
    public function testInjection(): void
49
    {
50
        $factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(), new Container());
51
        $logger = $factory->getLogger();
52
53
        $container = new Container();
54
        $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

54
        $container->bind(ConfiguratorInterface::class, /** @scrutinizer ignore-type */ new ConfigManager(
Loading history...
55
            new class() implements LoaderInterface {
56
                public function has(string $section): bool
57
                {
58
                    return false;
59
                }
60
61
                public function load(string $section): array
62
                {
63
                    return [];
64
                }
65
            }
66
        ));
67
68
        $container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
0 ignored issues
show
Bug introduced by
$finalizer = Mockery::mo...alizerInterface::class) of type Mockery\LegacyMockInterface|Mockery\MockInterface 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

68
        $container->bind(FinalizerInterface::class, /** @scrutinizer ignore-type */ $finalizer = \Mockery::mock(FinalizerInterface::class));
Loading history...
69
        $finalizer->shouldReceive('addFinalizer')->once();
70
71
        $container->get(BootloadManager::class)->bootload([MonologBootloader::class]);
72
        $container->bind(LogFactory::class, $factory);
0 ignored issues
show
Bug introduced by
$factory of type Spiral\Monolog\LogFactory 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

72
        $container->bind(LogFactory::class, /** @scrutinizer ignore-type */ $factory);
Loading history...
73
74
        $this->assertSame($logger, $container->get(Logger::class));
75
        $this->assertSame($logger, $container->get(LoggerInterface::class));
76
    }
77
78
    public function testFinalizerShouldResetDefaultLogger()
79
    {
80
        $container = new Container();
81
        $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

81
        $container->bind(ConfiguratorInterface::class, /** @scrutinizer ignore-type */ new ConfigManager(
Loading history...
82
            new class() implements LoaderInterface {
83
                public function has(string $section): bool
84
                {
85
                    return false;
86
                }
87
88
                public function load(string $section): array
89
                {
90
                    return [];
91
                }
92
            }
93
        ));
94
95
        $container->bind(FinalizerInterface::class, $finalizer = new Finalizer());
0 ignored issues
show
Bug introduced by
$finalizer = new Spiral\Boot\Finalizer() of type Spiral\Boot\Finalizer 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

95
        $container->bind(FinalizerInterface::class, /** @scrutinizer ignore-type */ $finalizer = new Finalizer());
Loading history...
96
97
        $factory = new LogFactory(new MonologConfig([
98
            'handlers' => [
99
                'default' => [
100
                    $handler = \Mockery::mock(HandlerInterface::class, ResettableInterface::class)
101
                ]
102
            ],
103
            'processors' => [
104
                'default' => [
105
                    $processor = \Mockery::mock(ProcessorInterface::class, ResettableInterface::class)
106
                ]
107
            ]
108
        ]), new ListenerRegistry(), $container);
109
110
        $handler->shouldReceive('reset')->once();
111
        $processor->shouldReceive('reset')->once();
112
113
        $container->bind(LogFactory::class, $factory);
0 ignored issues
show
Bug introduced by
$factory of type Spiral\Monolog\LogFactory 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

113
        $container->bind(LogFactory::class, /** @scrutinizer ignore-type */ $factory);
Loading history...
114
        $container->get(BootloadManager::class)->bootload([MonologBootloader::class]);
115
        $container->get(LogsInterface::class)->getLogger();
116
        $finalizer->finalize();
117
    }
118
}
119