From 146a2049259446f4930077a538c87d56461e4d97 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Wed, 9 Apr 2025 12:43:18 -0500 Subject: [PATCH 1/9] Collect other eloquent model events --- src/LaravelDebugbar.php | 14 +++++--- src/Resources/laravel-debugbar.css | 13 ++++++-- src/Support/Clockwork/Converter.php | 11 ++++++- tests/DataCollector/ModelsCollectorTest.php | 36 ++++++++++++++++++--- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index 5ca7e3172..e9a186dda 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -470,11 +470,15 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) { if ($this->shouldCollect('models', true) && $events) { try { $this->addCollector(new ObjectCountCollector('models')); - $events->listen('eloquent.retrieved:*', function ($event, $models) { - foreach (array_filter($models) as $model) { - $this['models']->countClass($model); - } - }); + $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; + $this['models']->setKeyMap(array_combine($eventList, array_map('ucfirst', $eventList))); + foreach ($eventList as $event) { + $events->listen("eloquent.{$event}: *", function ($event, $models) { + $event = explode(': ', $event); + $count = count(array_filter($models)); + $this['models']->countClass($event[1], $count, explode('.', $event[0])[1]); + }); + } } catch (Exception $e) { $this->addCollectorException('Cannot add Models Collector', $e); } diff --git a/src/Resources/laravel-debugbar.css b/src/Resources/laravel-debugbar.css index 0147606a4..e7a3f9eaf 100644 --- a/src/Resources/laravel-debugbar.css +++ b/src/Resources/laravel-debugbar.css @@ -227,7 +227,8 @@ div.phpdebugbar .phpdebugbar-header select { } dl.phpdebugbar-widgets-kvlist dt, -dl.phpdebugbar-widgets-kvlist dd { +dl.phpdebugbar-widgets-kvlist dd, +table.phpdebugbar-widgets-tablevar td { min-height: 20px; line-height: 20px; padding: 4px 5px 5px; @@ -239,7 +240,8 @@ dl.phpdebugbar-widgets-kvlist dd.phpdebugbar-widgets-value.phpdebugbar-widgets-p background: transparent; } -dl.phpdebugbar-widgets-kvlist dt { +dl.phpdebugbar-widgets-kvlist dt, +table.phpdebugbar-widgets-tablevar td:first-child { width: calc(25% - 10px); } @@ -459,7 +461,8 @@ div.phpdebugbar-widgets-sqlqueries table.phpdebugbar-widgets-params td.phpdebugb margin-left: 3px; } -ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item:nth-child(even) { +ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item:nth-child(even), +table.phpdebugbar-widgets-tablevar tr:nth-child(even) { background-color: var(--debugbar-background-alt); } @@ -476,6 +479,10 @@ div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugb float: left; } +table.phpdebugbar-widgets-tablevar td { + border: 0; +} + div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugbar-widgets-value.phpdebugbar-widgets-info { color: #1299DA; } diff --git a/src/Support/Clockwork/Converter.php b/src/Support/Clockwork/Converter.php index 6a7b590b2..6dc646428 100644 --- a/src/Support/Clockwork/Converter.php +++ b/src/Support/Clockwork/Converter.php @@ -117,7 +117,16 @@ public function convert($data) $output['modelsCreated'] = []; $output['modelsUpdated'] = []; $output['modelsDeleted'] = []; - $output['modelsRetrieved'] = $data['models']['data']; + $output['modelsRetrieved'] = []; + + foreach ($data['models']['data'] as $model => $value) { + foreach ($value as $event => $count) { + $eventKey = 'models' . ucfirst($event); + if (isset($output[$eventKey])) { + $output[$eventKey][$model] = $count; + } + } + } } if (isset($data['views']['templates'])) { diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php index 02ca92ac5..9ff1379a7 100644 --- a/tests/DataCollector/ModelsCollectorTest.php +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -14,6 +14,10 @@ class ModelsCollectorTest extends TestCase public function testItCollectsRetrievedModels() { + $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; + $keyMap = array_combine($eventList, array_map('ucfirst', $eventList)); + $data = []; + $this->loadLaravelMigrations(); debugbar()->boot(); @@ -22,6 +26,11 @@ public function testItCollectsRetrievedModels() $collector = debugbar()->getCollector('models'); $collector->setXdebugLinkTemplate(''); + $this->assertEquals( + ['data' => $data, 'count' => 0, 'key_map' => $keyMap, 'is_counter' => true], + $collector->collect() + ); + User::create([ 'name' => 'John Doe', 'email' => 'john@example.com', @@ -34,22 +43,41 @@ public function testItCollectsRetrievedModels() 'password' => Hash::make('password'), ]); + $data[User::class] = ['created' => 2]; $this->assertEquals( - ['data' => [], 'count' => 0, 'is_counter' => true], + ['data' => $data, 'key_map' => $keyMap, 'count' => 2, 'is_counter' => true], $collector->collect() ); - User::first(); + $user = User::first(); + $data[User::class]['retrieved'] = 1; $this->assertEquals( - ['data' => [User::class => 1], 'count' => 1, 'is_counter' => true], + ['data' => $data, 'key_map' => $keyMap, 'count' => 3, 'is_counter' => true], + $collector->collect() + ); + + $user->update(['name' => 'Jane Doe']); + + $data[User::class]['updated'] = 1; + $this->assertEquals( + ['data' => $data, 'key_map' => $keyMap, 'count' => 4, 'is_counter' => true], $collector->collect() ); Person::all(); + $data[Person::class] = ['retrieved' => 2]; + $this->assertEquals( + ['data' => $data, 'key_map' => $keyMap, 'count' => 6, 'is_counter' => true], + $collector->collect() + ); + + $user->delete(); + + $data[User::class]['deleted'] = 1; $this->assertEquals( - ['data' => [User::class => 1, Person::class => 2], 'count' => 3, 'is_counter' => true], + ['data' => $data, 'key_map' => $keyMap, 'count' => 7, 'is_counter' => true], $collector->collect() ); } From 8781b5d19a006205763d4dcf4fecc71dd1e2dbce Mon Sep 17 00:00:00 2001 From: erikn69 Date: Fri, 11 Apr 2025 12:03:17 -0500 Subject: [PATCH 2/9] Fix JobsCollector tests --- tests/DataCollector/JobsCollectorTest.php | 10 +++++++--- tests/DataCollector/ModelsCollectorTest.php | 8 +++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/DataCollector/JobsCollectorTest.php b/tests/DataCollector/JobsCollectorTest.php index 151126181..79ca7cba9 100644 --- a/tests/DataCollector/JobsCollectorTest.php +++ b/tests/DataCollector/JobsCollectorTest.php @@ -34,16 +34,19 @@ public function testItCollectsDispatchedJobs() /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ $collector = debugbar()->getCollector('jobs'); $collector->setXdebugLinkTemplate(''); + $keyMap = ['value' => 'Count']; + $data = []; $this->assertEquals( - ['data' => [], 'count' => 0, 'is_counter' => true], + ['data' => $data, 'count' => 0, 'key_map' => $keyMap, 'is_counter' => true], $collector->collect() ); OrderShipped::dispatch(1); + $data[OrderShipped::class] = ['value' => 1]; $this->assertEquals( - ['data' => [OrderShipped::class => 1], 'count' => 1, 'is_counter' => true], + ['data' => $data, 'count' => 1, 'key_map' => $keyMap, 'is_counter' => true], $collector->collect() ); @@ -51,8 +54,9 @@ public function testItCollectsDispatchedJobs() dispatch(new SendNotification()); dispatch(new SendNotification()); + $data[SendNotification::class] = ['value' => 3]; $this->assertEquals( - ['data' => [OrderShipped::class => 1, SendNotification::class => 3], 'count' => 4, 'is_counter' => true], + ['data' => $data, 'count' => 4, 'key_map' => $keyMap, 'is_counter' => true], $collector->collect() ); } diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php index 9ff1379a7..c91048acb 100644 --- a/tests/DataCollector/ModelsCollectorTest.php +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -14,17 +14,15 @@ class ModelsCollectorTest extends TestCase public function testItCollectsRetrievedModels() { - $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; - $keyMap = array_combine($eventList, array_map('ucfirst', $eventList)); - $data = []; - $this->loadLaravelMigrations(); - debugbar()->boot(); /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ $collector = debugbar()->getCollector('models'); $collector->setXdebugLinkTemplate(''); + $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; + $keyMap = array_combine($eventList, array_map('ucfirst', $eventList)); + $data = []; $this->assertEquals( ['data' => $data, 'count' => 0, 'key_map' => $keyMap, 'is_counter' => true], From 138cbda5bb9c47ac552042559b815dfcd69339fa Mon Sep 17 00:00:00 2001 From: erikn69 Date: Fri, 11 Apr 2025 12:12:44 -0500 Subject: [PATCH 3/9] Support summary badges on ModelsCollector --- src/LaravelDebugbar.php | 1 + tests/DataCollector/JobsCollectorTest.php | 8 ++++---- tests/DataCollector/ModelsCollectorTest.php | 16 ++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index e9a186dda..be5ce1253 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -472,6 +472,7 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) { $this->addCollector(new ObjectCountCollector('models')); $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; $this['models']->setKeyMap(array_combine($eventList, array_map('ucfirst', $eventList))); + $this['models']->collectCountSummary(true); foreach ($eventList as $event) { $events->listen("eloquent.{$event}: *", function ($event, $models) { $event = explode(': ', $event); diff --git a/tests/DataCollector/JobsCollectorTest.php b/tests/DataCollector/JobsCollectorTest.php index 79ca7cba9..9610cd19a 100644 --- a/tests/DataCollector/JobsCollectorTest.php +++ b/tests/DataCollector/JobsCollectorTest.php @@ -34,11 +34,11 @@ public function testItCollectsDispatchedJobs() /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ $collector = debugbar()->getCollector('jobs'); $collector->setXdebugLinkTemplate(''); - $keyMap = ['value' => 'Count']; + $collector->setKeyMap([]); $data = []; $this->assertEquals( - ['data' => $data, 'count' => 0, 'key_map' => $keyMap, 'is_counter' => true], + ['data' => $data, 'count' => 0, 'key_map' => [], 'is_counter' => true], $collector->collect() ); @@ -46,7 +46,7 @@ public function testItCollectsDispatchedJobs() $data[OrderShipped::class] = ['value' => 1]; $this->assertEquals( - ['data' => $data, 'count' => 1, 'key_map' => $keyMap, 'is_counter' => true], + ['data' => $data, 'count' => 1, 'key_map' => [], 'is_counter' => true], $collector->collect() ); @@ -56,7 +56,7 @@ public function testItCollectsDispatchedJobs() $data[SendNotification::class] = ['value' => 3]; $this->assertEquals( - ['data' => $data, 'count' => 4, 'key_map' => $keyMap, 'is_counter' => true], + ['data' => $data, 'count' => 4, 'key_map' => [], 'is_counter' => true], $collector->collect() ); } diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php index c91048acb..405dff6eb 100644 --- a/tests/DataCollector/ModelsCollectorTest.php +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -20,12 +20,12 @@ public function testItCollectsRetrievedModels() /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ $collector = debugbar()->getCollector('models'); $collector->setXdebugLinkTemplate(''); - $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; - $keyMap = array_combine($eventList, array_map('ucfirst', $eventList)); + $collector->collectCountSummary(false); + $collector->setKeyMap([]); $data = []; $this->assertEquals( - ['data' => $data, 'count' => 0, 'key_map' => $keyMap, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 0, 'is_counter' => true], $collector->collect() ); @@ -43,7 +43,7 @@ public function testItCollectsRetrievedModels() $data[User::class] = ['created' => 2]; $this->assertEquals( - ['data' => $data, 'key_map' => $keyMap, 'count' => 2, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 2, 'is_counter' => true], $collector->collect() ); @@ -51,7 +51,7 @@ public function testItCollectsRetrievedModels() $data[User::class]['retrieved'] = 1; $this->assertEquals( - ['data' => $data, 'key_map' => $keyMap, 'count' => 3, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 3, 'is_counter' => true], $collector->collect() ); @@ -59,7 +59,7 @@ public function testItCollectsRetrievedModels() $data[User::class]['updated'] = 1; $this->assertEquals( - ['data' => $data, 'key_map' => $keyMap, 'count' => 4, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 4, 'is_counter' => true], $collector->collect() ); @@ -67,7 +67,7 @@ public function testItCollectsRetrievedModels() $data[Person::class] = ['retrieved' => 2]; $this->assertEquals( - ['data' => $data, 'key_map' => $keyMap, 'count' => 6, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 6, 'is_counter' => true], $collector->collect() ); @@ -75,7 +75,7 @@ public function testItCollectsRetrievedModels() $data[User::class]['deleted'] = 1; $this->assertEquals( - ['data' => $data, 'key_map' => $keyMap, 'count' => 7, 'is_counter' => true], + ['data' => $data, 'key_map' => [], 'count' => 7, 'is_counter' => true], $collector->collect() ); } From e6590f0b1fb4f63c3cd806f9b59a609c3a60f603 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Tue, 15 Apr 2025 10:44:22 -0500 Subject: [PATCH 4/9] remove `eloquent.restored` event --- src/LaravelDebugbar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index be5ce1253..59b6444cb 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -470,7 +470,7 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) { if ($this->shouldCollect('models', true) && $events) { try { $this->addCollector(new ObjectCountCollector('models')); - $eventList = ['retrieved', 'created', 'updated', 'deleted', 'restored']; + $eventList = ['retrieved', 'created', 'updated', 'deleted']; $this['models']->setKeyMap(array_combine($eventList, array_map('ucfirst', $eventList))); $this['models']->collectCountSummary(true); foreach ($eventList as $event) { From 20e3d5d0bbe4cf7c5d8100950fa3a0136c2e70f6 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 8 May 2025 15:01:46 +0200 Subject: [PATCH 5/9] Update ModelsCollectorTest.php --- tests/DataCollector/ModelsCollectorTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php index e879811c6..4a42ba3a1 100644 --- a/tests/DataCollector/ModelsCollectorTest.php +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -45,10 +45,9 @@ public function testItCollectsRetrievedModels() $this->assertEquals( [ 'data' => $data, - 'count' => 0, + 'count' => 2, 'is_counter' => true, 'key_map' => [ - 'value' => 'Count' ], ], $collector->collect() From 46919ecaad2691e53ff5a0e359f479173e898cf4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 8 May 2025 15:02:12 +0200 Subject: [PATCH 6/9] Update JobsCollectorTest.php --- tests/DataCollector/JobsCollectorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DataCollector/JobsCollectorTest.php b/tests/DataCollector/JobsCollectorTest.php index 90eb3ab35..817a4f433 100644 --- a/tests/DataCollector/JobsCollectorTest.php +++ b/tests/DataCollector/JobsCollectorTest.php @@ -42,7 +42,7 @@ public function testItCollectsDispatchedJobs() 'data' => $data, 'count' => 0, 'is_counter' => true, - 'key_map' => ['value' => 'Count'] + 'key_map' => [] ], $collector->collect() ); From 041a46576c3bb56b6e1f43aa7525c84be4a5922f Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 8 May 2025 15:07:52 +0200 Subject: [PATCH 7/9] Update ModelsCollectorTest.php --- tests/DataCollector/ModelsCollectorTest.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/DataCollector/ModelsCollectorTest.php b/tests/DataCollector/ModelsCollectorTest.php index 4a42ba3a1..4247a8ea2 100644 --- a/tests/DataCollector/ModelsCollectorTest.php +++ b/tests/DataCollector/ModelsCollectorTest.php @@ -66,14 +66,10 @@ public function testItCollectsRetrievedModels() $data[User::class]['updated'] = 1; $this->assertEquals( [ - 'data' => [ - $data - ], - 'count' => 1, + 'data' => $data, + 'count' => 4, 'is_counter' => true, - 'key_map' => [ - 'value' => 'Count' - ], + 'key_map' => [], ], $collector->collect() ); @@ -92,10 +88,9 @@ public function testItCollectsRetrievedModels() $this->assertEquals( [ 'data' => $data, - 'count' => 3, + 'count' => 7, 'is_counter' => true, 'key_map' => [ - 'value' => 'Count' ] ], $collector->collect() From 2b86bc9560de2dde43debf1da3db0c7bb9f624a5 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 8 May 2025 15:08:06 +0200 Subject: [PATCH 8/9] Update JobsCollectorTest.php --- tests/DataCollector/JobsCollectorTest.php | 108 +++++++++++----------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/tests/DataCollector/JobsCollectorTest.php b/tests/DataCollector/JobsCollectorTest.php index 817a4f433..4247a8ea2 100644 --- a/tests/DataCollector/JobsCollectorTest.php +++ b/tests/DataCollector/JobsCollectorTest.php @@ -2,100 +2,98 @@ namespace Barryvdh\Debugbar\Tests\DataCollector; -use Barryvdh\Debugbar\Tests\Jobs\OrderShipped; -use Barryvdh\Debugbar\Tests\Jobs\SendNotification; +use Barryvdh\Debugbar\Tests\Models\Person; +use Barryvdh\Debugbar\Tests\Models\User; use Barryvdh\Debugbar\Tests\TestCase; -use Illuminate\Database\Migrations\Migration; -use Illuminate\Database\Schema\Blueprint; use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Hash; -class JobsCollectorTest extends TestCase +class ModelsCollectorTest extends TestCase { use RefreshDatabase; - protected function getEnvironmentSetUp($app) - { - $app['config']->set('debugbar.collectors.jobs', true); - // The `sync` and `null` driver don't dispatch events - // `database` or `redis` driver work great - $app['config']->set('queue.default', 'database'); - - parent::getEnvironmentSetUp($app); - } - - public function testItCollectsDispatchedJobs() + public function testItCollectsRetrievedModels() { $this->loadLaravelMigrations(); - $this->createJobsTable(); - debugbar()->boot(); /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ - $collector = debugbar()->getCollector('jobs'); + $collector = debugbar()->getCollector('models'); $collector->setXdebugLinkTemplate(''); + $collector->collectCountSummary(false); $collector->setKeyMap([]); $data = []; + $this->assertEquals( + ['data' => $data, 'key_map' => [], 'count' => 0, 'is_counter' => true], + $collector->collect() + ); + + 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'), + ]); + + $data[User::class] = ['created' => 2]; $this->assertEquals( [ 'data' => $data, - 'count' => 0, + 'count' => 2, 'is_counter' => true, - 'key_map' => [] + 'key_map' => [ + ], ], $collector->collect() ); - OrderShipped::dispatch(1); + $user = User::first(); + + $data[User::class]['retrieved'] = 1; + $this->assertEquals( + ['data' => $data, 'key_map' => [], 'count' => 3, 'is_counter' => true], + $collector->collect() + ); + + $user->update(['name' => 'Jane Doe']); - $data[OrderShipped::class] = ['value' => 1]; + $data[User::class]['updated'] = 1; $this->assertEquals( [ 'data' => $data, - 'count' => 1, + 'count' => 4, 'is_counter' => true, - 'key_map' => ['value' => 'Count'] + 'key_map' => [], ], $collector->collect() ); - dispatch(new SendNotification()); - dispatch(new SendNotification()); - dispatch(new SendNotification()); + Person::all(); - $data[SendNotification::class] = ['value' => 3]; + $data[Person::class] = ['retrieved' => 2]; + $this->assertEquals( + ['data' => $data, 'key_map' => [], 'count' => 6, 'is_counter' => true], + $collector->collect() + ); + + $user->delete(); + + $data[User::class]['deleted'] = 1; $this->assertEquals( [ 'data' => $data, - 'count' => 4, + 'count' => 7, 'is_counter' => true, - 'key_map' => ['value' => 'Count'] + 'key_map' => [ + ] ], $collector->collect() ); } - - protected function createJobsTable() - { - (new class extends Migration - { - public function up() - { - if (Schema::hasTable('jobs')) { - return; - } - - Schema::create('jobs', function (Blueprint $table) { - $table->bigIncrements('id'); - $table->string('queue')->index(); - $table->longText('payload'); - $table->unsignedTinyInteger('attempts'); - $table->unsignedInteger('reserved_at')->nullable(); - $table->unsignedInteger('available_at'); - $table->unsignedInteger('created_at'); - }); - } - })->up(); - } } From 0814be6b64be9650059abcc03c1160e1929b02af Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 8 May 2025 15:09:20 +0200 Subject: [PATCH 9/9] Update JobsCollectorTest.php --- tests/DataCollector/JobsCollectorTest.php | 108 +++++++++++----------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/tests/DataCollector/JobsCollectorTest.php b/tests/DataCollector/JobsCollectorTest.php index 4247a8ea2..b64fdb5e8 100644 --- a/tests/DataCollector/JobsCollectorTest.php +++ b/tests/DataCollector/JobsCollectorTest.php @@ -2,98 +2,100 @@ namespace Barryvdh\Debugbar\Tests\DataCollector; -use Barryvdh\Debugbar\Tests\Models\Person; -use Barryvdh\Debugbar\Tests\Models\User; +use Barryvdh\Debugbar\Tests\Jobs\OrderShipped; +use Barryvdh\Debugbar\Tests\Jobs\SendNotification; use Barryvdh\Debugbar\Tests\TestCase; +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Schema; -class ModelsCollectorTest extends TestCase +class JobsCollectorTest extends TestCase { use RefreshDatabase; - public function testItCollectsRetrievedModels() + protected function getEnvironmentSetUp($app) + { + $app['config']->set('debugbar.collectors.jobs', true); + // The `sync` and `null` driver don't dispatch events + // `database` or `redis` driver work great + $app['config']->set('queue.default', 'database'); + + parent::getEnvironmentSetUp($app); + } + + public function testItCollectsDispatchedJobs() { $this->loadLaravelMigrations(); + $this->createJobsTable(); + debugbar()->boot(); /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ - $collector = debugbar()->getCollector('models'); + $collector = debugbar()->getCollector('jobs'); $collector->setXdebugLinkTemplate(''); - $collector->collectCountSummary(false); $collector->setKeyMap([]); $data = []; - $this->assertEquals( - ['data' => $data, 'key_map' => [], 'count' => 0, 'is_counter' => true], - $collector->collect() - ); - - 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'), - ]); - - $data[User::class] = ['created' => 2]; $this->assertEquals( [ 'data' => $data, - 'count' => 2, + 'count' => 0, 'is_counter' => true, - 'key_map' => [ - ], + 'key_map' => [] ], $collector->collect() ); - $user = User::first(); - - $data[User::class]['retrieved'] = 1; - $this->assertEquals( - ['data' => $data, 'key_map' => [], 'count' => 3, 'is_counter' => true], - $collector->collect() - ); - - $user->update(['name' => 'Jane Doe']); + OrderShipped::dispatch(1); - $data[User::class]['updated'] = 1; + $data[OrderShipped::class] = ['value' => 1]; $this->assertEquals( [ 'data' => $data, - 'count' => 4, + 'count' => 1, 'is_counter' => true, - 'key_map' => [], + 'key_map' => [] ], $collector->collect() ); - Person::all(); + dispatch(new SendNotification()); + dispatch(new SendNotification()); + dispatch(new SendNotification()); - $data[Person::class] = ['retrieved' => 2]; - $this->assertEquals( - ['data' => $data, 'key_map' => [], 'count' => 6, 'is_counter' => true], - $collector->collect() - ); - - $user->delete(); - - $data[User::class]['deleted'] = 1; + $data[SendNotification::class] = ['value' => 3]; $this->assertEquals( [ 'data' => $data, - 'count' => 7, + 'count' => 4, 'is_counter' => true, - 'key_map' => [ - ] + 'key_map' => [] ], $collector->collect() ); } + + protected function createJobsTable() + { + (new class extends Migration + { + public function up() + { + if (Schema::hasTable('jobs')) { + return; + } + + Schema::create('jobs', function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('queue')->index(); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + }); + } + })->up(); + } }