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