Test Failed
Push — master ( 1b7368...050678 )
by Fran
25:16 queued 22:49
created

LoggerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 5
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);
84
        $logger = null;
85
        unset($defaultConfig['logger.memory']);
86
        Config::save($defaultConfig, []);
87
    }
88
89
}