Optimize models collector (#1051)

* Optimize models collector

Use a wildcard to listen to the `eloquent.retrieved:*` events
instead of listening to all the `eloquent.*` events.

* Add testsfor ModelsCollector
This commit is contained in:
Sébastien Nikolaou 2020-06-27 19:22:09 +03:00 committed by GitHub
parent f015e1b76d
commit bbf7f2521a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 165 additions and 12 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
/build
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
.phpunit.result.cache

View File

@ -18,6 +18,10 @@
"symfony/debug": "^3|^4|^5",
"symfony/finder": "^3|^4|^5"
},
"require-dev": {
"orchestra/testbench": "^3.5|^4.0|^5.0",
"phpunit/phpunit": "^6.0|^7.0|^8.5|^9.0"
},
"autoload": {
"psr-4": {
"Barryvdh\\Debugbar\\": "src/"
@ -26,6 +30,11 @@
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Barryvdh\\Debugbar\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
@ -40,8 +49,5 @@
"Debugbar": "Barryvdh\\Debugbar\\Facade"
}
}
},
"require-dev": {
"laravel/framework": "5.5.x"
}
}

32
phpunit.xml.dist Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Laravel Debugbar Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="DEBUGBAR_ENABLED" value="true"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>

View File

@ -6,7 +6,6 @@ use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\Renderable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str;
/**
* Collector for Models.
@ -21,13 +20,11 @@ class ModelsCollector extends DataCollector implements DataCollectorInterface, R
*/
public function __construct(Dispatcher $events)
{
$events->listen('eloquent.*', function ($event, $models) {
if (Str::contains($event, 'eloquent.retrieved')) {
foreach (array_filter($models) as $model) {
$class = get_class($model);
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
$this->count++;
}
$events->listen('eloquent.retrieved:*', function ($event, $models) {
foreach (array_filter($models) as $model) {
$class = get_class($model);
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
$this->count++;
}
});
}

View File

@ -0,0 +1,65 @@
<?php
namespace Barryvdh\Debugbar\Tests\DataCollector;
use Barryvdh\Debugbar\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
class ModelsCollectorTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_collects_retrieved_models()
{
$this->loadLaravelMigrations();
$this->debugbar()->boot();
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
$collector = $this->debugbar()->getCollector('models');
User::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => Hash::make('password'),
]);
User::create([
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'password' => Hash::make('password'),
]);
$this->assertEquals(
['data' => [], 'count' => 0],
$collector->collect()
);
User::first();
$this->assertEquals(
['data' => [User::class => 1], 'count' => 1],
$collector->collect()
);
Person::all();
$this->assertEquals(
['data' => [User::class => 1, Person::class => 2], 'count' => 3],
$collector->collect()
);
}
}
class User extends Model
{
protected $table = 'users';
protected $guarded = [];
}
class Person extends User
{
}

51
tests/TestCase.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace Barryvdh\Debugbar\Tests;
use Barryvdh\Debugbar\Facade;
use Barryvdh\Debugbar\ServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
class TestCase extends Orchestra
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar */
private $debugbar;
/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [ServiceProvider::class];
}
/**
* Get package aliases.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageAliases($app)
{
return ['Debugbar' => Facade::class];
}
public function getEnvironmentSetUp($app)
{
}
/**
* Get the Laravel Debugbar instance.
*
* @return \Barryvdh\Debugbar\LaravelDebugbar
*/
public function debugbar()
{
return $this->debugbar ?? $this->debugbar = $this->app->debugbar;
}
}