Completed
Push — master ( 680631...e33ad0 )
by Jacob
02:22
created

ConsoleTest::testGetLogs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
use Particletree\Pqp\Console;
4
5
class ConsoleTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
8
    public function __construct()
9
    {
10
    }
11
12 View Code Duplication
    public function testLog()
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...
13
    {
14
        $data = array(
15
            'key' => 'value'
16
        );
17
18
        $console = new Console();
19
        $console->log($data);
20
        $store = $this->getProtectedStore($console);
21
        $log = array_pop($store);
22
23
        $this->assertSame($data, $log['data']);
24
        $this->assertEquals('log', $log['type']);
25
    }
26
27
    public function testLogMemory()
28
    {
29
        $data = array(
30
            'key' => 'value'
31
        );
32
        $memory = strlen(serialize($data));
33
        $name = 'Test Array';
34
35
        $console = new Console();
36
        $console->logMemory($data, $name);
37
        $store = $this->getProtectedStore($console);
38
        $log = array_pop($store);
39
40
        $this->assertEquals($name, $log['name']);
41
        $this->assertEquals($memory, $log['data']);
42
        $this->assertEquals('array', $log['data_type']);
43
        $this->assertEquals('memory', $log['type']);
44
    }
45
46
    public function testLogError()
47
    {
48
        $error = new Exception('Test Exception');
49
50
        $console = new Console();
51
        $console->logError($error);
52
        $store = $this->getProtectedStore($console);
53
        $log = array_pop($store);
54
55
        $this->assertEquals($error->getMessage(), $log['data']);
56
        $this->assertEquals($error->getFile(), $log['file']);
57
        $this->assertEquals($error->getLine(), $log['line']);
58
        $this->assertEquals('error', $log['type']);
59
60
        $error = new Exception('Test Exception');
61
        $message = 'override message';
62
63
        $console = new Console();
64
        $console->logError($error, $message);
65
        $store = $this->getProtectedStore($console);
66
        $log = array_pop($store);
67
68
        $this->assertEquals($message, $log['data']);
69
    }
70
71 View Code Duplication
    public function testLogSpeed()
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...
72
    {
73
        $name = 'Test Speed';
74
75
        $console = new Console();
76
        $console->logSpeed($name);
77
        $store = $this->getProtectedStore($console);
78
        $log = array_pop($store);
79
80
        $this->assertEquals($name, $log['name']);
81
        $this->assertEquals('speed', $log['type']);
82
    }
83
84
    public function testGetLogs()
85
    {
86
        $store = array(
87
            array(
88
                'data' => 'a string',
89
                'type' => 'log'
90
            ),
91
            array(
92
                'name' => '',
93
                'data' => 123,
94
                'data_type' => 'array',
95
                'type' => 'memory'
96
            )
97
        );
98
99
        $console = new Console();
100
101
        $reflectedConsole = new ReflectionClass(get_class($console));
102
        $reflectedProperty = $reflectedConsole->getProperty('store');
103
        $reflectedProperty->setAccessible(true);
104
        $reflectedProperty->setValue($console, $store);
105
106
        $this->assertSame($store, $console->getLogs());
107
    }
108
109
    protected function getProtectedStore(Console $console)
110
    {
111
        $reflectedConsole = new ReflectionClass(get_class($console));
112
        $reflectedProperty = $reflectedConsole->getProperty('store');
113
        $reflectedProperty->setAccessible(true);
114
        return $reflectedProperty->getValue($console);
115
    }
116
}
117