Skip to content

Instantly share code, notes, and snippets.

@zetrider
Created October 9, 2021 06:36
Show Gist options
  • Save zetrider/6388acabb7ee084322290011472f5705 to your computer and use it in GitHub Desktop.
Save zetrider/6388acabb7ee084322290011472f5705 to your computer and use it in GitHub Desktop.
Laravel test via DebugBar
<?php
namespace Tests\Feature;
use Tests\TestCase;
class RoutesTest extends TestCase
{
const PHP_VERSION = '7.4.22';
const MAX_TIME_DURATION_SECONDS = 1;
const MAX_MEMORY_MB = 50;
const MAX_VIEWS = 50;
const MAX_DB_QUERIES = 10;
const MAX_DB_DUBLICATES = 1;
const MAX_MODELS = 2000;
public function setUp(): void
{
parent::setUp();
debugbar()->enable();
}
/**
* Routes - get method
*
* @return array
*/
private function routes(): array
{
return [
route('posts'),
];
}
public function test_posts()
{
foreach ($this->routes() as $route) {
$this->get($route);
$this->debugBarPhp();
$this->debugBarMessages();
$this->debugBarTime();
$this->debugBarMemory();
$this->debugBarExceptions();
$this->debugBarViews();
$this->debugBarRoutes();
$this->debugBarDbQueries();
$this->debugBarModels();
$this->debugBarMail();
$this->debugBarGate();
$this->debugBarSession();
$this->debugBarRequest();
}
}
/**
* Check php version
*/
private function debugBarPhp()
{
$collector = debugbar()->getCollector('php');
$collect = $collector->collect();
$this->assertEquals($collect['version'], self::PHP_VERSION, 'Wrong php version');
}
/**
* Check messages
*/
private function debugBarMessages()
{
// $collector = debugbar()->getCollector('messages');
// $collect = $collector->collect();
// ...
}
/**
* Check time
*/
private function debugBarTime()
{
$collector = debugbar()->getCollector('time');
$collect = $collector->collect();
$this->assertGreaterThanOrEqual($collect['duration'], self::MAX_TIME_DURATION_SECONDS, 'To long');
}
/**
* Check memory
*/
private function debugBarMemory()
{
$collector = debugbar()->getCollector('memory');
$collect = $collector->collect();
$memory = $collect['peak_usage'] / 1024 / 1024;
$this->assertGreaterThanOrEqual($memory, self::MAX_MEMORY_MB, 'Used a lot of memory');
}
/**
* Check exceptions
*/
private function debugBarExceptions()
{
$collector = debugbar()->getCollector('exceptions');
$collect = $collector->collect();
$this->assertEquals($collect['count'], 0, 'Has exceptions');
}
/**
* Check views
*/
private function debugBarViews()
{
$collector = debugbar()->getCollector('views');
$collect = $collector->collect();
$this->assertGreaterThanOrEqual($collect['nb_templates'], self::MAX_VIEWS, 'Many views');
}
/**
* Check routes
*/
private function debugBarRoutes()
{
// $collector = debugbar()->getCollector('route');
// $collect = $collector->collect();
// ...
}
/**
* Check queries
*/
private function debugBarDbQueries()
{
$collector = debugbar()->getCollector('queries');
$collect = $collector->collect();
$queries = [];
foreach ($collect['statements'] as $statements) {
if (!array_key_exists($statements['sql'], $queries)) {
$queries[$statements['sql']] = 0;
}
$queries[$statements['sql']]++;
}
$max = max($queries);
$count = count($queries);
$this->assertGreaterThanOrEqual($count, self::MAX_DB_QUERIES, 'Many queries');
$this->assertGreaterThanOrEqual($max, self::MAX_DB_DUBLICATES, 'Dublicate queries');
}
/**
* Check models
*/
private function debugBarModels()
{
// Models
$collector = debugbar()->getCollector('models');
$collect = $collector->collect();
$this->assertGreaterThanOrEqual($collect['count'], self::MAX_MODELS, 'Many models');
}
/**
* Check mail
*/
private function debugBarMail()
{
// $collector = debugbar()->getCollector('swiftmailer_mails');
// $collect = $collector->collect();
// ...
}
/**
* Check mail
*/
private function debugBarGate()
{
// $collector = debugbar()->getCollector('gate');
// $collect = $collector->collect();
// ...
}
/**
* Check session
*/
private function debugBarSession()
{
// $collector = debugbar()->getCollector('session');
// $collect = $collector->collect();
// ...
}
/**
* Check request
*/
private function debugBarRequest()
{
// $collector = debugbar()->getCollector('request');
// $collect = $collector->collect();
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment