Source of file DebugBarTest.php
Size: 4,079 Bytes - Last Modified: 2021-12-23T10:00:22+00:00
/var/www/docs.ssmods.com/process/src/tests/DebugBarTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 | <?php namespace LeKoala\DebugBar\Test; use DebugBar\DataCollector\MessagesCollector; use LeKoala\DebugBar\Collector\DatabaseCollector; use LeKoala\DebugBar\DebugBar; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Kernel; use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\DB; /** * Tests for DebugBar */ class DebugBarTest extends SapphireTest { public function setUp() { parent::setUp(); // Init manually because we are running tests DebugBar::initDebugBar(); } public function tearDown() { DebugBar::clearDebugBar(); parent::tearDown(); } public function testInitIsWorking() { // De we have a debugbar instance $this->assertNotEmpty(DebugBar::getDebugBar()); } public function testLHelper() { $msg = 'Test me'; l($msg); $debugbar = DebugBar::getDebugBar(); /** @var DebugBar\Bridge\MonologCollector $messagesCollector */ $messagesCollector = $debugbar->getCollector('messages'); $messages = $messagesCollector->getMessages(); $found = false; foreach ($messages as $message) { $txt = $message['message']; if (strpos($txt, $msg) !== false) { $found = true; } } $this->assertTrue($found); } public function testDHelper() { $sql = 'SELECT * FROM Member'; ob_start(); d($this, 'test', $sql); $content = ob_get_clean(); $this->assertContains("Value for: 'test'", $content, "Value for test not found"); } /** * @param callable $context * @param string $expected * @dataProvider whyDisabledProvider */ public function testWhyDisabled($context, $expected) { $context(); $this->assertSame($expected, DebugBar::whyDisabled()); } /** * @return array[] */ public function whyDisabledProvider() { return array( array( function () { Injector::inst()->get(Kernel::class)->setEnvironment('live'); }, 'Not in dev mode' ), array( function () { Config::modify()->set(DebugBar::class, 'disabled', true); }, 'Disabled by a constant or configuration' ), array( function () { // no-op }, 'In CLI mode' ) ); } public function testNotLocalIp() { Config::modify()->set(DebugBar::class, 'check_local_ip', false); $this->assertFalse(DebugBar::notLocalIp()); Config::modify()->set(DebugBar::class, 'check_local_ip', true); $original = $_SERVER['REMOTE_ADDR']; $_SERVER['REMOTE_ADDR'] = '123.456.789.012'; $this->assertTrue(DebugBar::notLocalIp()); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $this->assertFalse(DebugBar::notLocalIp()); unset($_SERVER['REMOTE_ADDR']); $this->assertFalse(DebugBar::notLocalIp()); $_SERVER['REMOTE_ADDR'] = $original; } /** * For the database collector to be able to push messages to the message collector, it must be loaded * before the message collector. This test ensures that won't accidentally change in future. */ public function testMessageCollectorIsLoadedAfterDatabaseCollector() { $bar = DebugBar::getDebugBar(); $passedDatabaseCollector = false; foreach ($bar->getCollectors() as $collector) { if ($collector instanceof DatabaseCollector) { $passedDatabaseCollector = true; } if ($collector instanceof MessagesCollector) { $this->assertTrue($passedDatabaseCollector, 'Message collector must be after database collector'); break; } } } } |