1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Particletree\Pqp; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
class ConsoleTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
public function testLog() |
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
|
|
|
$data = '12345'; |
46
|
|
|
|
47
|
|
|
$console = new Console(); |
48
|
|
|
$console->logMemory($data, 'PHP', true); |
49
|
|
|
$store = $this->getProtectedStore($console); |
50
|
|
|
$log = array_pop($store); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($data, $log['data']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testLogError() |
56
|
|
|
{ |
57
|
|
|
$error = new Exception('Test Exception'); |
58
|
|
|
|
59
|
|
|
$console = new Console(); |
60
|
|
|
$console->logError($error); |
61
|
|
|
$store = $this->getProtectedStore($console); |
62
|
|
|
$log = array_pop($store); |
63
|
|
|
|
64
|
|
|
$this->assertEquals($error->getMessage(), $log['data']); |
65
|
|
|
$this->assertEquals($error->getFile(), $log['file']); |
66
|
|
|
$this->assertEquals($error->getLine(), $log['line']); |
67
|
|
|
$this->assertEquals('error', $log['type']); |
68
|
|
|
|
69
|
|
|
$error = new Exception('Test Exception'); |
70
|
|
|
$message = 'override message'; |
71
|
|
|
|
72
|
|
|
$console = new Console(); |
73
|
|
|
$console->logError($error, $message); |
74
|
|
|
$store = $this->getProtectedStore($console); |
75
|
|
|
$log = array_pop($store); |
76
|
|
|
|
77
|
|
|
$this->assertEquals($message, $log['data']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testLogSpeed() |
81
|
|
|
{ |
82
|
|
|
$name = 'Test Speed'; |
83
|
|
|
|
84
|
|
|
$console = new Console(); |
85
|
|
|
$console->logSpeed($name); |
86
|
|
|
$store = $this->getProtectedStore($console); |
87
|
|
|
$log = array_pop($store); |
88
|
|
|
|
89
|
|
|
$this->assertEquals($name, $log['name']); |
90
|
|
|
$this->assertEquals('speed', $log['type']); |
91
|
|
|
|
92
|
|
|
$name = 'Literal Time'; |
93
|
|
|
$time = 12345.1231; |
94
|
|
|
|
95
|
|
|
$console = new Console(); |
96
|
|
|
$console->logSpeed($name, $time); |
97
|
|
|
$store = $this->getProtectedStore($console); |
98
|
|
|
$log = array_pop($store); |
99
|
|
|
|
100
|
|
|
$this->assertEquals($name, $log['name']); |
101
|
|
|
$this->assertEquals($time, $log['data']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetLogs() |
105
|
|
|
{ |
106
|
|
|
$store = array( |
107
|
|
|
array( |
108
|
|
|
'data' => 'a string', |
109
|
|
|
'type' => 'log' |
110
|
|
|
), |
111
|
|
|
array( |
112
|
|
|
'name' => '', |
113
|
|
|
'data' => 123, |
114
|
|
|
'data_type' => 'array', |
115
|
|
|
'type' => 'memory' |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$console = new Console(); |
120
|
|
|
|
121
|
|
|
$reflectedConsole = new ReflectionClass(get_class($console)); |
122
|
|
|
$reflectedProperty = $reflectedConsole->getProperty('store'); |
123
|
|
|
$reflectedProperty->setAccessible(true); |
124
|
|
|
$reflectedProperty->setValue($console, $store); |
125
|
|
|
|
126
|
|
|
$this->assertSame($store, $console->getLogs()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getProtectedStore(Console $console) |
130
|
|
|
{ |
131
|
|
|
$reflectedConsole = new ReflectionClass(get_class($console)); |
132
|
|
|
$reflectedProperty = $reflectedConsole->getProperty('store'); |
133
|
|
|
$reflectedProperty->setAccessible(true); |
134
|
|
|
return $reflectedProperty->getValue($console); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|