|
2 | 2 |
|
3 | 3 | namespace Barryvdh\Debugbar\Tests\DataCollector; |
4 | 4 |
|
5 | | -use Barryvdh\Debugbar\Tests\Models\Person; |
6 | | -use Barryvdh\Debugbar\Tests\Models\User; |
| 5 | +use Barryvdh\Debugbar\Tests\Jobs\OrderShipped; |
| 6 | +use Barryvdh\Debugbar\Tests\Jobs\SendNotification; |
7 | 7 | use Barryvdh\Debugbar\Tests\TestCase; |
| 8 | +use Illuminate\Database\Migrations\Migration; |
| 9 | +use Illuminate\Database\Schema\Blueprint; |
8 | 10 | use Illuminate\Foundation\Testing\RefreshDatabase; |
9 | | -use Illuminate\Support\Facades\Hash; |
| 11 | +use Illuminate\Support\Facades\Schema; |
10 | 12 |
|
11 | | -class ModelsCollectorTest extends TestCase |
| 13 | +class JobsCollectorTest extends TestCase |
12 | 14 | { |
13 | 15 | use RefreshDatabase; |
14 | 16 |
|
15 | | - public function testItCollectsRetrievedModels() |
| 17 | + protected function getEnvironmentSetUp($app) |
| 18 | + { |
| 19 | + $app['config']->set('debugbar.collectors.jobs', true); |
| 20 | + // The `sync` and `null` driver don't dispatch events |
| 21 | + // `database` or `redis` driver work great |
| 22 | + $app['config']->set('queue.default', 'database'); |
| 23 | + |
| 24 | + parent::getEnvironmentSetUp($app); |
| 25 | + } |
| 26 | + |
| 27 | + public function testItCollectsDispatchedJobs() |
16 | 28 | { |
17 | 29 | $this->loadLaravelMigrations(); |
| 30 | + $this->createJobsTable(); |
| 31 | + |
18 | 32 | debugbar()->boot(); |
19 | 33 |
|
20 | 34 | /** @var \DebugBar\DataCollector\ObjectCountCollector $collector */ |
21 | | - $collector = debugbar()->getCollector('models'); |
| 35 | + $collector = debugbar()->getCollector('jobs'); |
22 | 36 | $collector->setXdebugLinkTemplate(''); |
23 | | - $collector->collectCountSummary(false); |
24 | 37 | $collector->setKeyMap([]); |
25 | 38 | $data = []; |
26 | 39 |
|
27 | | - $this->assertEquals( |
28 | | - ['data' => $data, 'key_map' => [], 'count' => 0, 'is_counter' => true], |
29 | | - $collector->collect() |
30 | | - ); |
31 | | - |
32 | | - User::create([ |
33 | | - 'name' => 'John Doe', |
34 | | - |
35 | | - 'password' => Hash::make('password'), |
36 | | - ]); |
37 | | - |
38 | | - User::create([ |
39 | | - 'name' => 'Jane Doe', |
40 | | - |
41 | | - 'password' => Hash::make('password'), |
42 | | - ]); |
43 | | - |
44 | | - $data[User::class] = ['created' => 2]; |
45 | 40 | $this->assertEquals( |
46 | 41 | [ |
47 | 42 | 'data' => $data, |
48 | | - 'count' => 2, |
| 43 | + 'count' => 0, |
49 | 44 | 'is_counter' => true, |
50 | | - 'key_map' => [ |
51 | | - ], |
| 45 | + 'key_map' => [] |
52 | 46 | ], |
53 | 47 | $collector->collect() |
54 | 48 | ); |
55 | 49 |
|
56 | | - $user = User::first(); |
57 | | - |
58 | | - $data[User::class]['retrieved'] = 1; |
59 | | - $this->assertEquals( |
60 | | - ['data' => $data, 'key_map' => [], 'count' => 3, 'is_counter' => true], |
61 | | - $collector->collect() |
62 | | - ); |
63 | | - |
64 | | - $user->update(['name' => 'Jane Doe']); |
| 50 | + OrderShipped::dispatch(1); |
65 | 51 |
|
66 | | - $data[User::class]['updated'] = 1; |
| 52 | + $data[OrderShipped::class] = ['value' => 1]; |
67 | 53 | $this->assertEquals( |
68 | 54 | [ |
69 | 55 | 'data' => $data, |
70 | | - 'count' => 4, |
| 56 | + 'count' => 1, |
71 | 57 | 'is_counter' => true, |
72 | | - 'key_map' => [], |
| 58 | + 'key_map' => [] |
73 | 59 | ], |
74 | 60 | $collector->collect() |
75 | 61 | ); |
76 | 62 |
|
77 | | - Person::all(); |
| 63 | + dispatch(new SendNotification()); |
| 64 | + dispatch(new SendNotification()); |
| 65 | + dispatch(new SendNotification()); |
78 | 66 |
|
79 | | - $data[Person::class] = ['retrieved' => 2]; |
80 | | - $this->assertEquals( |
81 | | - ['data' => $data, 'key_map' => [], 'count' => 6, 'is_counter' => true], |
82 | | - $collector->collect() |
83 | | - ); |
84 | | - |
85 | | - $user->delete(); |
86 | | - |
87 | | - $data[User::class]['deleted'] = 1; |
| 67 | + $data[SendNotification::class] = ['value' => 3]; |
88 | 68 | $this->assertEquals( |
89 | 69 | [ |
90 | 70 | 'data' => $data, |
91 | | - 'count' => 7, |
| 71 | + 'count' => 4, |
92 | 72 | 'is_counter' => true, |
93 | | - 'key_map' => [ |
94 | | - ] |
| 73 | + 'key_map' => [] |
95 | 74 | ], |
96 | 75 | $collector->collect() |
97 | 76 | ); |
98 | 77 | } |
| 78 | + |
| 79 | + protected function createJobsTable() |
| 80 | + { |
| 81 | + (new class extends Migration |
| 82 | + { |
| 83 | + public function up() |
| 84 | + { |
| 85 | + if (Schema::hasTable('jobs')) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + Schema::create('jobs', function (Blueprint $table) { |
| 90 | + $table->bigIncrements('id'); |
| 91 | + $table->string('queue')->index(); |
| 92 | + $table->longText('payload'); |
| 93 | + $table->unsignedTinyInteger('attempts'); |
| 94 | + $table->unsignedInteger('reserved_at')->nullable(); |
| 95 | + $table->unsignedInteger('available_at'); |
| 96 | + $table->unsignedInteger('created_at'); |
| 97 | + }); |
| 98 | + } |
| 99 | + })->up(); |
| 100 | + } |
99 | 101 | } |
0 commit comments