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