Test Setup Failed
Push — master ( aa9039...94c501 )
by Derek
03:32
created

LoggerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 13.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 11
loc 80
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 11 11 2
A assertPreconditions() 0 4 1
A setUp() 0 6 2
A tearDown() 0 3 1
A tearDownAfterClass() 0 6 2
A testLogToFileWithFileHandler() 0 10 1
A testLoggerThrowsExceptionWithInvalidLogLevel() 0 6 1
A testGetHandlerReturnsHandler() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Subreality\Dilmun\Tests\Nabu;
3
4
use Subreality\Dilmun\Nabu\LoggerHandler\File;
5
use Subreality\Dilmun\Nabu\Logger;
6
7
/**
8
 * Class LoggerTest
9
 * @package Subreality\Dilmun\Tests\Nabu
10
 */
11
class LoggerTest extends \PHPUnit_Framework_TestCase
12
{
13
    public static $full_log_location;
14
15
    /**
16
     * @var File $log_file
17
     */
18
    public static $log_file;
19
20 View Code Duplication
    public static function setUpBeforeClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        self::$full_log_location = DILMUN_LOGS_DIR . "Logger_test.log";
23
24
        if (file_exists(self::$full_log_location)) {
25
            unlink(self::$full_log_location);
26
        }
27
28
        self::$log_file = new File(self::$full_log_location);
29
        self::$log_file->initialize();
30
    }
31
32
    protected function assertPreconditions()
33
    {
34
        $this->assertFileExists(self::$full_log_location);
35
    }
36
37
    protected function setUp()
38
    {
39
        if (file_exists(self::$full_log_location)) {
40
            self::$log_file->clearFile();
41
        }
42
    }
43
44
    protected function tearDown()
45
    {
46
    }
47
48
    public static function tearDownAfterClass()
49
    {
50
        if (file_exists(self::$full_log_location)) {
51
            unlink(self::$full_log_location);
52
        }
53
    }
54
55
    public function testLogToFileWithFileHandler()
56
    {
57
        $logger = new Logger(self::$log_file);
58
59
        $returned_message = $logger->log("debug", "Logging works!");
60
61
        $logged_message = file_get_contents(self::$full_log_location);
62
63
        $this->assertContains($returned_message, $logged_message);
64
    }
65
66
    /**
67
     * @expectedException \InvalidArgumentException
68
     */
69
    public function testLoggerThrowsExceptionWithInvalidLogLevel()
70
    {
71
        $logger = new Logger(self::$log_file);
72
73
        $logger->log("this is not a valid level", "this should never be logged");
74
    }
75
76
    public function testGetHandlerReturnsHandler()
77
    {
78
        $logger = new Logger(self::$log_file);
79
80
        $handler = $logger->getHandler();
81
82
        if ($handler instanceof File) {
83
            $handler_is_file = true;
84
        } else {
85
            $handler_is_file = false;
86
        }
87
88
        $this->assertTrue($handler_is_file);
89
    }
90
}
91