|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Particletree\Pqp\Console; |
|
4
|
|
|
|
|
5
|
|
|
class ConsoleTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
public function __construct() |
|
9
|
|
|
{ |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
View Code Duplication |
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
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.