Passed
Push — master ( d47bdf...6296de )
by Kirill
03:33
created

testGetsLoggerWhenChannelPassedAndContainerDoesNotExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\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);
0 ignored issues
show
Bug introduced by
$logs 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

64
        $container->bind(LogsInterface::class, /** @scrutinizer ignore-type */ $logs);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$logs 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

94
        $container->bind(LogsInterface::class, /** @scrutinizer ignore-type */ $logs);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$logger of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Log\LoggerInterface expected by parameter $logger of Spiral\Tests\Logger\TraitTest::setLogger(). ( Ignorable by Annotation )

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

104
        $this->setLogger(/** @scrutinizer ignore-type */ $logger);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$logger of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Log\LoggerInterface expected by parameter $logger of Spiral\Tests\Logger\TraitTest::setLogger(). ( Ignorable by Annotation )

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

112
        $this->setLogger(/** @scrutinizer ignore-type */ $logger);
Loading history...
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