1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Particletree\Pqp; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_Testcase; |
6
|
|
|
|
7
|
|
|
// namespace hack on microtime functionality |
8
|
|
|
function microtime() |
9
|
|
|
{ |
10
|
|
|
return 1450355136.5706; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
// namespace hack on included files functionality |
14
|
|
|
function get_included_files() |
15
|
|
|
{ |
16
|
|
|
return array( |
17
|
|
|
'index.php', |
18
|
|
|
'src/Class.php' |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// namespace hack on filesize |
23
|
|
|
function filesize($filename) |
24
|
|
|
{ |
25
|
|
|
return strlen($filename) * 100; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// namespace hack on memory usage |
29
|
|
|
function memory_get_peak_usage() |
30
|
|
|
{ |
31
|
|
|
return 123456789; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// namespace hack on ini settings |
35
|
|
|
function ini_get($setting) |
36
|
|
|
{ |
37
|
|
|
if ($setting == 'memory_limit') { |
38
|
|
|
return '128M'; |
39
|
|
|
} elseif ($setting == 'max_execution_time') { |
40
|
|
|
return '30'; |
41
|
|
|
} |
42
|
|
|
return \ini_get($setting); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
public function testConstruct() |
49
|
|
|
{ |
50
|
|
|
$startTime = microtime(true); |
51
|
|
|
|
52
|
|
|
$profiler = new PhpQuickProfiler(); |
53
|
|
|
$this->assertAttributeEquals($startTime, 'startTime', $profiler); |
54
|
|
|
|
55
|
|
|
$profiler = new PhpQuickProfiler($startTime); |
56
|
|
|
$this->assertAttributeEquals($startTime, 'startTime', $profiler); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSetConsole() |
60
|
|
|
{ |
61
|
|
|
$console = new Console(); |
62
|
|
|
$profiler = new PhpQuickProfiler(); |
63
|
|
|
$profiler->setConsole($console); |
64
|
|
|
|
65
|
|
|
$this->assertAttributeSame($console, 'console', $profiler); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testSetDisplay() |
69
|
|
|
{ |
70
|
|
|
$display = new Display(); |
71
|
|
|
$profiler = new PhpQuickProfiler(); |
72
|
|
|
$profiler->setDisplay($display); |
73
|
|
|
|
74
|
|
|
$this->assertAttributeSame($display, 'display', $profiler); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGatherFileData() |
78
|
|
|
{ |
79
|
|
|
$files = get_included_files(); |
80
|
|
|
$profiler = new PhpQuickProfiler(); |
81
|
|
|
$gatheredFileData = $profiler->gatherFileData(); |
82
|
|
|
|
83
|
|
|
$this->assertInternalType('array', $gatheredFileData); |
84
|
|
|
$this->assertEquals(count($files), count($gatheredFileData)); |
85
|
|
|
foreach ($gatheredFileData as $fileData) { |
86
|
|
|
$this->assertInternalType('array', $fileData); |
87
|
|
|
$this->assertArrayHasKey('name', $fileData); |
88
|
|
|
$this->assertContains($fileData['name'], $files); |
89
|
|
|
$this->assertArrayHasKey('size', $fileData); |
90
|
|
|
$this->assertEquals($fileData['size'], filesize($fileData['name'])); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGatherMemoryData() |
95
|
|
|
{ |
96
|
|
|
$memoryUsage = memory_get_peak_usage(); |
97
|
|
|
$allowedLimit = ini_get('memory_limit'); |
98
|
|
|
$profiler = new PhpQuickProfiler(); |
99
|
|
|
$gatheredMemoryData = $profiler->gatherMemoryData(); |
100
|
|
|
|
101
|
|
|
$this->assertInternalType('array', $gatheredMemoryData); |
102
|
|
|
$this->assertEquals(2, count($gatheredMemoryData)); |
103
|
|
|
$this->assertArrayHasKey('used', $gatheredMemoryData); |
104
|
|
|
$this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
105
|
|
|
$this->assertArrayHasKey('allowed', $gatheredMemoryData); |
106
|
|
|
$this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testSetProfiledQueries() |
110
|
|
|
{ |
111
|
|
|
$profiledQueries = array( |
112
|
|
|
'sql' => 'SELECT * FROM example', |
113
|
|
|
'time' => 25 |
114
|
|
|
); |
115
|
|
|
$profiler = new PhpQuickProfiler(); |
116
|
|
|
$profiler->setProfiledQueries($profiledQueries); |
117
|
|
|
|
118
|
|
|
$this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testGatherSpeedData() |
122
|
|
|
{ |
123
|
|
|
$elapsedTime = 1.234; |
124
|
|
|
$startTime = microtime(true) - $elapsedTime; |
125
|
|
|
$allowedTime = ini_get('max_execution_time'); |
126
|
|
|
$profiler = new PhpQuickProfiler($startTime); |
127
|
|
|
$gatheredSpeedData = $profiler->gatherSpeedData(); |
128
|
|
|
|
129
|
|
|
$this->assertInternalType('array', $gatheredSpeedData); |
130
|
|
|
$this->assertEquals(2, count($gatheredSpeedData)); |
131
|
|
|
$this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
132
|
|
|
$this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
133
|
|
|
$this->assertArrayHasKey('allowed', $gatheredSpeedData); |
134
|
|
|
$this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|