Test injecting Debugbar (#1088)

* Test injecting Debugbar

* composer fix-style

* Update phpunit.xml.dist

* Override enabled

* composer fix-style

* Cleanup

Co-authored-by: laravel-debugbar <laravel-debugbar@users.noreply.github.com>
This commit is contained in:
Barry vd. Heuvel 2020-08-17 14:59:58 +02:00 committed by GitHub
parent 51c8ea3ab2
commit 9162a23532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 10 deletions

View File

@ -16,10 +16,10 @@ class ModelsCollectorTest extends TestCase
{
$this->loadLaravelMigrations();
$this->debugbar()->boot();
debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
$collector = $this->debugbar()->getCollector('models');
$collector = debugbar()->getCollector('models');
User::create([
'name' => 'John Doe',

View File

@ -14,10 +14,10 @@ class QueryCollectorTest extends TestCase
{
$this->loadLaravelMigrations();
$this->debugbar()->boot();
debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\QueryCollector $collector */
$collector = $this->debugbar()->getCollector('queries');
$collector = debugbar()->getCollector('queries');
$collector->addQuery(
"SELECT ('[1, 2, 3]'::jsonb ?? ?) as a, ('[4, 5, 6]'::jsonb ??| ?) as b, 'hello world ? example ??' as c",
[3, '{4}'],

55
tests/DebugbarTest.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace Barryvdh\Debugbar\Tests;
use Barryvdh\Debugbar\LaravelDebugbar;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Routing\Router;
use Illuminate\Support\Str;
class DebugbarTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
// Force the Debugbar to Enable on test/cli applications
$app->resolving(LaravelDebugbar::class, function ($debugbar) {
$refObject = new \ReflectionObject($debugbar);
$refProperty = $refObject->getProperty('enabled');
$refProperty->setAccessible(true);
$refProperty->setValue($debugbar, true);
});
}
public function testItInjectsOnPlainText()
{
$crawler = $this->call('GET', 'web/plain');
$this->assertTrue(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}
public function testItInjectsOnHtml()
{
$crawler = $this->call('GET', 'web/html');
$this->assertTrue(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}
public function testItDoesntInjectOnJson()
{
$crawler = $this->call('GET', 'api/ping');
$this->assertFalse(Str::contains($crawler->content(), 'debugbar'));
$this->assertEquals(200, $crawler->getStatusCode());
}
}

View File

@ -4,6 +4,7 @@ namespace Barryvdh\Debugbar\Tests;
use Barryvdh\Debugbar\Facade;
use Barryvdh\Debugbar\ServiceProvider;
use Illuminate\Routing\Router;
use Orchestra\Testbench\TestCase as Orchestra;
class TestCase extends Orchestra
@ -35,17 +36,49 @@ class TestCase extends Orchestra
return ['Debugbar' => Facade::class];
}
public function getEnvironmentSetUp($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
/** @var Router $router */
$router = $app['router'];
$this->addWebRoutes($router);
$this->addApiRoutes($router);
}
/**
* Get the Laravel Debugbar instance.
*
* @return \Barryvdh\Debugbar\LaravelDebugbar
* @param Router $router
*/
public function debugbar()
protected function addWebRoutes(Router $router)
{
return $this->debugbar ?? $this->debugbar = $this->app->debugbar;
$router->get('web/plain', [
'uses' => function () {
return 'PONG';
}
]);
$router->get('web/html', [
'uses' => function () {
return '<html><head></head><body>Pong</body></html>';
}
]);
}
/**
* @param Router $router
*/
protected function addApiRoutes(Router $router)
{
$router->get('api/ping', [
'uses' => function () {
return response()->json(['status' => 'pong']);
}
]);
}
}