|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use DebugBar\DataCollector\MessagesCollector; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Tests for DebugBar |
|
7
|
|
|
*/ |
|
8
|
|
|
class DebugBarTest extends SapphireTest |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
public function setUp() |
|
11
|
|
|
{ |
|
12
|
|
|
parent::setUp(); |
|
13
|
|
|
|
|
14
|
|
|
// Init manually because we are running tests |
|
15
|
|
|
DebugBar::initDebugBar(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function tearDown() |
|
19
|
|
|
{ |
|
20
|
|
|
DebugBar::clearDebugBar(); |
|
21
|
|
|
|
|
22
|
|
|
parent::tearDown(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testInitIsWorking() |
|
26
|
|
|
{ |
|
27
|
|
|
// De we have a debugbar instance |
|
28
|
|
|
$this->assertNotEmpty(DebugBar::getDebugBar()); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
// Do we have a db proxy |
|
31
|
|
|
if (method_exists('DB', 'get_conn')) { |
|
32
|
|
|
$conn = DB::get_conn(); |
|
33
|
|
|
} else { |
|
34
|
|
|
$conn = DB::getConn(); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$class = get_class($conn); |
|
38
|
|
|
$this->assertContains($class, ['DebugBarDatabaseNewProxy', 'DebugBarDatabaseProxy']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testLHelper() |
|
42
|
|
|
{ |
|
43
|
|
|
$msg = 'Test me'; |
|
44
|
|
|
l($msg); |
|
45
|
|
|
|
|
46
|
|
|
$debugbar = DebugBar::getDebugBar(); |
|
47
|
|
|
|
|
48
|
|
|
/* @var $messagesCollector DebugBar\DataCollector\MessagesCollector */ |
|
49
|
|
|
$messagesCollector = $debugbar->getCollector('messages'); |
|
50
|
|
|
$messages = $messagesCollector->getMessages(); |
|
51
|
|
|
$found = false; |
|
52
|
|
|
foreach ($messages as $message) { |
|
53
|
|
|
$txt = $message['message']; |
|
54
|
|
|
if (strpos($txt, $msg) !== false) { |
|
55
|
|
|
$found = true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
$this->assertTrue($found); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testDHelper() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->markTestSkipped( |
|
|
|
|
|
|
64
|
|
|
'This test needs to be looked at again, the output buffering is not capturing the result' |
|
65
|
|
|
); |
|
66
|
|
|
$sql = 'SELECT * FROM Member'; |
|
67
|
|
|
ob_start(); |
|
68
|
|
|
// Passing a SapphireTest as first arg prevent exit |
|
69
|
|
|
d($this, 'test', $sql); |
|
70
|
|
|
$content = ob_get_clean(); |
|
71
|
|
|
$this->assertTrue((bool) strpos($content, "Value for: 'test'"), "Value for test not found"); |
|
|
|
|
|
|
72
|
|
|
$this->assertTrue((bool) strpos($content, 'sf-dump'), "Symfony dumper not found"); |
|
|
|
|
|
|
73
|
|
|
$this->assertTrue( |
|
|
|
|
|
|
74
|
|
|
(bool)strpos($content, '<span style="font-weight:bold;">SELECT</span>'), |
|
75
|
|
|
"Sql formatted query not found" |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param callable $context |
|
81
|
|
|
* @param string $expected |
|
82
|
|
|
* @dataProvider whyDisabledProvider |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testWhyDisabled($context, $expected) |
|
85
|
|
|
{ |
|
86
|
|
|
$context(); |
|
87
|
|
|
$this->assertSame($expected, DebugBar::whyDisabled()); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function whyDisabledProvider() |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
|
|
[ |
|
97
|
|
|
function () { |
|
98
|
|
|
Director::set_environment_type('live'); |
|
|
|
|
|
|
99
|
|
|
}, |
|
100
|
|
|
'Not in dev mode' |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
function () { |
|
104
|
|
|
Config::inst()->update('DebugBar', 'disabled', true); |
|
105
|
|
|
}, |
|
106
|
|
|
'Disabled by a constant or configuration' |
|
107
|
|
|
], |
|
108
|
|
|
[ |
|
109
|
|
|
function () { |
|
110
|
|
|
// no-op |
|
111
|
|
|
}, |
|
112
|
|
|
'In CLI mode' |
|
113
|
|
|
] |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testNotLocalIp() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
Config::inst()->update('DebugBar', 'check_local_ip', false); |
|
120
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
Config::inst()->update('DebugBar', 'check_local_ip', true); |
|
123
|
|
|
$original = $_SERVER['REMOTE_ADDR']; |
|
124
|
|
|
$_SERVER['REMOTE_ADDR'] = '123.456.789.012'; |
|
125
|
|
|
$this->assertTrue(DebugBar::notLocalIp()); |
|
|
|
|
|
|
126
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
|
127
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
unset($_SERVER['REMOTE_ADDR']); |
|
130
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
$_SERVER['REMOTE_ADDR'] = $original; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* For the database collector to be able to push messages to the message collector, it must be loaded |
|
137
|
|
|
* before the message collector. This test ensures that won't accidentally change in future. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function testMessageCollectorIsLoadedAfterDatabaseCollector() |
|
140
|
|
|
{ |
|
141
|
|
|
$bar = DebugBar::getDebugBar(); |
|
142
|
|
|
|
|
143
|
|
|
$passedDatabaseCollector = false; |
|
144
|
|
|
foreach ($bar->getCollectors() as $collector) { |
|
145
|
|
|
if ($collector instanceof DebugBarDatabaseCollector) { |
|
146
|
|
|
$passedDatabaseCollector = true; |
|
147
|
|
|
} |
|
148
|
|
|
if ($collector instanceof MessagesCollector) { |
|
149
|
|
|
$this->assertTrue($passedDatabaseCollector, 'Message collector must be after database collector'); |
|
|
|
|
|
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
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.