Skip to content

Commit

Permalink
Optimize models collector (barryvdh#1051)
Browse files Browse the repository at this point in the history
* Optimize models collector

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

* Add testsfor ModelsCollector
  • Loading branch information
sebdesign authored Jun 27, 2020
1 parent f015e1b commit bbf7f25
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/build
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
.phpunit.result.cache
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -26,6 +30,11 @@
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Barryvdh\\Debugbar\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
Expand All @@ -40,8 +49,5 @@
"Debugbar": "Barryvdh\\Debugbar\\Facade"
}
}
},
"require-dev": {
"laravel/framework": "5.5.x"
}
}
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -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>
13 changes: 5 additions & 8 deletions src/DataCollector/ModelsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\Renderable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str;

/**
* Collector for Models.
Expand All @@ -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++;
}
});
}
Expand Down
65 changes: 65 additions & 0 deletions tests/DataCollector/ModelsCollectorTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 51 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit bbf7f25

Please sign in to comment.