Test Failed
Push — master ( a76412...1b7368 )
by Fran
13:33 queued 14s
created

LoggerTest::testLogSetup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace PSFS\tests\base;
4
5
use PHPUnit\Framework\TestCase;
6
use PSFS\base\config\Config;
7
use PSFS\base\Logger;
8
9
/**
10
 * Class DispatcherTest
11
 * @package PSFS\tests\base
12
 */
13
class LoggerTest extends TestCase
14
{
15
    /**
16
     * Test to check if the Logger has been created successful
17
     * @return Logger
18
     */
19
    public function getInstance()
20
    {
21
        $instance = Logger::getInstance();
22
23
        $this->assertNotNull($instance, 'Logger instance is null');
24
        $this->assertInstanceOf("\\PSFS\\base\\Logger", $instance, 'Instance is different than expected');
25
        return $instance;
26
    }
27
28
    /**
29
     * Test to check if the Singleton pattern works
30
     */
31
    public function testSingletonLoggerInstance()
32
    {
33
        $instance1 = $this->getInstance();
34
        $uid1 = Logger::getUid();
35
        $instance2 = $this->getInstance();
36
        $uid2 = $instance2->getLogUid();
37
38
        $this->assertEquals($instance1, $instance2, 'Singleton instances are not equals');
39
        $this->assertEquals($uid1, $uid2, 'Singleton instances are not equals');
40
    }
41
42
    /**
43
     * Test all the functionality for the logger class
44
     */
45
    public function testLogFunctions()
46
    {
47
        try {
48
            // Basic log
49
            Logger::log('Test normal log');
50
            // Warning log
51
            Logger::log('Test warning log', LOG_WARNING, [], true);
52
            // Info log
53
            Logger::log('Test info log', LOG_INFO, [], true);
54
            // Error log
55
            Logger::log('Test error log', LOG_ERR, [], true);
56
            // Critical log
57
            Logger::log('Test critical log', LOG_CRIT, [], true);
58
            // Debug log
59
            Logger::log('Test debug logs', LOG_DEBUG, [], true);
60
            // Other logs
61
            Logger::log('Test other logs', LOG_CRON, [], true);
62
        } catch (\Exception $e) {
63
            $this->assertFalse(true, $e->getMessage());
64
        } finally {
65
            $this->assertTrue(true, 'Finished Logger test function');
66
        }
67
    }
68
69
    /**
70
     * Test non default logger configurations set
71
     */
72
    public function testLogSetup()
73
    {
74
        // Add memory logger to test this functionality
75
        $config = Config::getInstance();
76
        $this->assertInstanceOf(Config::class, $config, 'Config interface');
77
        $defaultConfig = $config->dumpConfig();
78
        Config::save(array_merge($defaultConfig, ['logger.memory' => true, 'logger.phpFire' => true, 'profiling.enable' => true]), []);
79
80
        // Create a new logger instance
81
        $logger = new Logger(['test', true]);
82
        $this->assertInstanceOf(Logger::class, $logger, 'Logger interface');
83
        $logger->addLog('Test', \Monolog\Logger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant Monolog\Logger::DEBUG has been deprecated: Use \Monolog\Level::Debug ( Ignorable by Annotation )

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

83
        $logger->addLog('Test', /** @scrutinizer ignore-deprecated */ \Monolog\Logger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
84
        $logger = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $logger is dead and can be removed.
Loading history...
85
        unset($defaultConfig['logger.memory']);
86
        Config::save($defaultConfig, []);
87
    }
88
89
}