Skip to content

Commit bbf7f25

Browse files
authored
Optimize models collector (barryvdh#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
1 parent f015e1b commit bbf7f25

File tree

6 files changed

+165
-12
lines changed

6 files changed

+165
-12
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
/build
12
/vendor
23
composer.phar
34
composer.lock
4-
.DS_Store
5+
.DS_Store
6+
.phpunit.result.cache

composer.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"symfony/debug": "^3|^4|^5",
1919
"symfony/finder": "^3|^4|^5"
2020
},
21+
"require-dev": {
22+
"orchestra/testbench": "^3.5|^4.0|^5.0",
23+
"phpunit/phpunit": "^6.0|^7.0|^8.5|^9.0"
24+
},
2125
"autoload": {
2226
"psr-4": {
2327
"Barryvdh\\Debugbar\\": "src/"
@@ -26,6 +30,11 @@
2630
"src/helpers.php"
2731
]
2832
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Barryvdh\\Debugbar\\Tests\\": "tests"
36+
}
37+
},
2938
"minimum-stability": "dev",
3039
"prefer-stable": true,
3140
"extra": {
@@ -40,8 +49,5 @@
4049
"Debugbar": "Barryvdh\\Debugbar\\Facade"
4150
}
4251
}
43-
},
44-
"require-dev": {
45-
"laravel/framework": "5.5.x"
4652
}
4753
}

phpunit.xml.dist

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Laravel Debugbar Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="junit" target="build/report.junit.xml"/>
24+
<log type="coverage-html" target="build/coverage"/>
25+
<log type="coverage-text" target="build/coverage.txt"/>
26+
<log type="coverage-clover" target="build/logs/clover.xml"/>
27+
</logging>
28+
<php>
29+
<env name="DEBUGBAR_ENABLED" value="true"/>
30+
<env name="DB_CONNECTION" value="testing"/>
31+
</php>
32+
</phpunit>

src/DataCollector/ModelsCollector.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use DebugBar\DataCollector\DataCollectorInterface;
77
use DebugBar\DataCollector\Renderable;
88
use Illuminate\Contracts\Events\Dispatcher;
9-
use Illuminate\Support\Str;
109

1110
/**
1211
* Collector for Models.
@@ -21,13 +20,11 @@ class ModelsCollector extends DataCollector implements DataCollectorInterface, R
2120
*/
2221
public function __construct(Dispatcher $events)
2322
{
24-
$events->listen('eloquent.*', function ($event, $models) {
25-
if (Str::contains($event, 'eloquent.retrieved')) {
26-
foreach (array_filter($models) as $model) {
27-
$class = get_class($model);
28-
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
29-
$this->count++;
30-
}
23+
$events->listen('eloquent.retrieved:*', function ($event, $models) {
24+
foreach (array_filter($models) as $model) {
25+
$class = get_class($model);
26+
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
27+
$this->count++;
3128
}
3229
});
3330
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\DataCollector;
4+
5+
use Barryvdh\Debugbar\Tests\TestCase;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Hash;
9+
10+
class ModelsCollectorTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
/** @test */
15+
public function it_collects_retrieved_models()
16+
{
17+
$this->loadLaravelMigrations();
18+
19+
$this->debugbar()->boot();
20+
21+
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
22+
$collector = $this->debugbar()->getCollector('models');
23+
24+
User::create([
25+
'name' => 'John Doe',
26+
'email' => '[email protected]',
27+
'password' => Hash::make('password'),
28+
]);
29+
30+
User::create([
31+
'name' => 'Jane Doe',
32+
'email' => '[email protected]',
33+
'password' => Hash::make('password'),
34+
]);
35+
36+
$this->assertEquals(
37+
['data' => [], 'count' => 0],
38+
$collector->collect()
39+
);
40+
41+
User::first();
42+
43+
$this->assertEquals(
44+
['data' => [User::class => 1], 'count' => 1],
45+
$collector->collect()
46+
);
47+
48+
Person::all();
49+
50+
$this->assertEquals(
51+
['data' => [User::class => 1, Person::class => 2], 'count' => 3],
52+
$collector->collect()
53+
);
54+
}
55+
}
56+
57+
class User extends Model
58+
{
59+
protected $table = 'users';
60+
protected $guarded = [];
61+
}
62+
63+
class Person extends User
64+
{
65+
}

tests/TestCase.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests;
4+
5+
use Barryvdh\Debugbar\Facade;
6+
use Barryvdh\Debugbar\ServiceProvider;
7+
use Orchestra\Testbench\TestCase as Orchestra;
8+
9+
class TestCase extends Orchestra
10+
{
11+
/** @var \Barryvdh\Debugbar\LaravelDebugbar */
12+
private $debugbar;
13+
14+
/**
15+
* Get package providers.
16+
*
17+
* @param \Illuminate\Foundation\Application $app
18+
*
19+
* @return array
20+
*/
21+
protected function getPackageProviders($app)
22+
{
23+
return [ServiceProvider::class];
24+
}
25+
26+
/**
27+
* Get package aliases.
28+
*
29+
* @param \Illuminate\Foundation\Application $app
30+
*
31+
* @return array
32+
*/
33+
protected function getPackageAliases($app)
34+
{
35+
return ['Debugbar' => Facade::class];
36+
}
37+
38+
public function getEnvironmentSetUp($app)
39+
{
40+
}
41+
42+
/**
43+
* Get the Laravel Debugbar instance.
44+
*
45+
* @return \Barryvdh\Debugbar\LaravelDebugbar
46+
*/
47+
public function debugbar()
48+
{
49+
return $this->debugbar ?? $this->debugbar = $this->app->debugbar;
50+
}
51+
}

0 commit comments

Comments
 (0)