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 SilverStripe\Control\Director; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Core\Environment; |
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(): void |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
// Init manually because we are running tests |
26
|
|
|
DebugBar::initDebugBar(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function tearDown(): void |
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
|
|
|
|
42
|
|
|
public function testLHelper() |
43
|
|
|
{ |
44
|
|
|
$msg = 'Test me'; |
45
|
|
|
l($msg); |
46
|
|
|
|
47
|
|
|
$debugbar = DebugBar::getDebugBar(); |
48
|
|
|
|
49
|
|
|
/** @var DebugBar\Bridge\MonologCollector $messagesCollector */ |
50
|
|
|
$messagesCollector = $debugbar->getCollector('messages'); |
51
|
|
|
$messages = $messagesCollector->getMessages(); |
52
|
|
|
$found = false; |
53
|
|
|
foreach ($messages as $message) { |
54
|
|
|
$txt = $message['message']; |
55
|
|
|
if (strpos($txt, $msg) !== false) { |
56
|
|
|
$found = true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$this->assertTrue($found); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testDHelper() |
63
|
|
|
{ |
64
|
|
|
$sql = 'SELECT * FROM Member'; |
65
|
|
|
ob_start(); |
66
|
|
|
d($this, 'test', $sql); |
67
|
|
|
$content = ob_get_clean(); |
68
|
|
|
$this->assertStringContainsString("Value for: 'test'", $content, "Value for test not found"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param callable $context |
73
|
|
|
* @param string $expected |
74
|
|
|
* @dataProvider whyDisabledProvider |
75
|
|
|
*/ |
76
|
|
|
public function testWhyDisabled($context, $expected) |
77
|
|
|
{ |
78
|
|
|
$context(); |
79
|
|
|
$this->assertSame($expected, DebugBar::whyDisabled()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array[] |
84
|
|
|
*/ |
85
|
|
|
public static function whyDisabledProvider() |
86
|
|
|
{ |
87
|
|
|
return array( |
88
|
|
|
array( |
89
|
|
|
function () { |
90
|
|
|
Injector::inst()->get(Kernel::class)->setEnvironment('live'); |
91
|
|
|
}, |
92
|
|
|
'Not in dev mode' |
93
|
|
|
), |
94
|
|
|
array( |
95
|
|
|
function () { |
96
|
|
|
Config::modify()->set(DebugBar::class, 'disabled', true); |
97
|
|
|
}, |
98
|
|
|
'Disabled by a constant or configuration' |
99
|
|
|
), |
100
|
|
|
// array( |
101
|
|
|
// function () { |
102
|
|
|
// // no-op |
103
|
|
|
// }, |
104
|
|
|
// 'In CLI mode' |
105
|
|
|
// ) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testNotLocalIp() |
110
|
|
|
{ |
111
|
|
|
Config::modify()->set(DebugBar::class, 'check_local_ip', false); |
112
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
113
|
|
|
|
114
|
|
|
Config::modify()->set(DebugBar::class, 'check_local_ip', true); |
115
|
|
|
$original = $_SERVER['REMOTE_ADDR']; |
116
|
|
|
$_SERVER['REMOTE_ADDR'] = '123.456.789.012'; |
117
|
|
|
$this->assertTrue(DebugBar::notLocalIp()); |
118
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
119
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
120
|
|
|
|
121
|
|
|
unset($_SERVER['REMOTE_ADDR']); |
122
|
|
|
$this->assertFalse(DebugBar::notLocalIp()); |
123
|
|
|
|
124
|
|
|
$_SERVER['REMOTE_ADDR'] = $original; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* For the database collector to be able to push messages to the message collector, it must be loaded |
129
|
|
|
* before the message collector. This test ensures that won't accidentally change in future. |
130
|
|
|
*/ |
131
|
|
|
public function testMessageCollectorIsLoadedAfterDatabaseCollector() |
132
|
|
|
{ |
133
|
|
|
$bar = DebugBar::getDebugBar(); |
134
|
|
|
|
135
|
|
|
$passedDatabaseCollector = false; |
136
|
|
|
foreach ($bar->getCollectors() as $collector) { |
137
|
|
|
if ($collector instanceof DatabaseCollector) { |
138
|
|
|
$passedDatabaseCollector = true; |
139
|
|
|
} |
140
|
|
|
if ($collector instanceof MessagesCollector) { |
141
|
|
|
$this->assertTrue($passedDatabaseCollector, 'Message collector must be after database collector'); |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|