Skip to content

Commit c78d2c4

Browse files
authored
Merge pull request #120 from beyondcode/dev
Dev
2 parents 0f2b166 + fdea121 commit c78d2c4

File tree

4 files changed

+61
-68
lines changed

4 files changed

+61
-68
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,27 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
php: ['8.4', '8.3', '8.2', '8.1', '8.0']
20-
laravel: ['8.*', '9.*', '10.*', '11.*', '12.*']
19+
php: ['8.4', '8.3', '8.2', '8.1']
20+
laravel: ['10.*', '11.*', '12.*', '13.*']
2121
dependency-version: [prefer-stable]
2222
exclude:
23-
- php: 8.0
24-
laravel: 10.*
25-
- php: 8.0
26-
laravel: 11.*
27-
- php: 8.0
28-
laravel: 12.*
2923
- php: 8.1
3024
laravel: 11.*
3125
- php: 8.1
3226
laravel: 12.*
27+
- php: 8.1
28+
laravel: 13.*
3329
- php: 8.2
34-
laravel: 8.*
35-
- php: 8.3
36-
laravel: 8.*
37-
- php: 8.3
38-
laravel: 9.*
39-
- php: 8.4
40-
laravel: 8.*
41-
- php: 8.4
42-
laravel: 9.*
30+
laravel: 13.*
4331
include:
44-
- laravel: 8.*
45-
testbench: 6.23
46-
- laravel: 9.*
47-
testbench: 7.*
4832
- laravel: 10.*
4933
testbench: 8.*
5034
- laravel: 11.*
5135
testbench: 9.*
5236
- laravel: 12.*
5337
testbench: 10.*
38+
- laravel: 13.*
39+
testbench: 11.*
5440

5541
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest
5642

@@ -71,4 +57,4 @@ jobs:
7157
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
7258
7359
- name: Execute tests
74-
run: vendor/bin/phpunit
60+
run: vendor/bin/phpunit

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
],
1818
"require": {
1919
"php": "^7.1 || ^8.0",
20-
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0 || ^11.0 || ^12.0"
20+
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0"
2121
},
2222
"require-dev": {
2323
"laravel/legacy-factories": "^1.0",
24-
"orchestra/testbench": "^3.0 || ^4.0 || ^5.0 || ^6.0|^8.0 || ^9.0 || ^10.0",
25-
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0 || ^10.5 || ^11.5.3"
24+
"orchestra/testbench": "^3.0 || ^4.0 || ^5.0 || ^6.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
25+
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0 || ^10.5 || ^11.5.3 || ^12.5.12"
2626
},
2727
"autoload": {
2828
"psr-4": {

src/Outputs/Debugbar.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace BeyondCode\QueryDetector\Outputs;
44

5+
use DebugBar\DataCollector\MessagesCollector;
56
use Illuminate\Support\Collection;
67
use Symfony\Component\HttpFoundation\Response;
78

8-
use Barryvdh\Debugbar\Facade as LaravelDebugbarV3;
9-
use DebugBar\DataCollector\MessagesCollector;
10-
use Fruitcake\LaravelDebugbar\Facades\Debugbar as LaravelDebugbar;
11-
129
class Debugbar implements Output
1310
{
1411
protected $collector;
@@ -17,26 +14,52 @@ public function boot()
1714
{
1815
$this->collector = new MessagesCollector('N+1 Queries');
1916

20-
if (class_exists(\Fruitcake\LaravelDebugbar\Facades\Debugbar::class)) {
21-
if (!LaravelDebugbar::hasCollector($this->collector->getName())) {
22-
LaravelDebugbar::addCollector($this->collector);
23-
}
17+
$facade = $this->resolveDebugbarFacade();
18+
19+
if ($facade === null) {
2420
return;
2521
}
2622

27-
if (!LaravelDebugbarV3::hasCollector($this->collector->getName())) {
28-
LaravelDebugbarV3::addCollector($this->collector);
23+
if (! $facade::hasCollector($this->collector->getName())) {
24+
$facade::addCollector($this->collector);
2925
}
3026
}
3127

3228
public function output(Collection $detectedQueries, Response $response)
3329
{
3430
foreach ($detectedQueries as $detectedQuery) {
35-
$this->collector->addMessage(sprintf('Model: %s => Relation: %s - You should add `with(%s)` to eager-load this relation.',
31+
$this->collector->addMessage(sprintf(
32+
'Model: %s => Relation: %s - You should add `with(%s)` to eager-load this relation.',
3633
$detectedQuery['model'],
3734
$detectedQuery['relation'],
3835
$detectedQuery['relation']
3936
));
4037
}
4138
}
39+
40+
/**
41+
* Resolve the installed Debugbar facade at runtime.
42+
*
43+
* Supports barryvdh/laravel-debugbar (v3 and v4+) and
44+
* fruitcake/laravel-debugbar. Returns the first class
45+
* that the autoloader can locate, or null if none exist.
46+
*
47+
* @return string|null
48+
*/
49+
protected function resolveDebugbarFacade()
50+
{
51+
$candidates = [
52+
'Barryvdh\Debugbar\Facades\Debugbar',
53+
'Barryvdh\Debugbar\Facade',
54+
'Fruitcake\LaravelDebugbar\Facades\Debugbar',
55+
];
56+
57+
foreach ($candidates as $candidate) {
58+
if (class_exists($candidate)) {
59+
return $candidate;
60+
}
61+
}
62+
63+
return null;
64+
}
4265
}

tests/QueryDetectorTest.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
class QueryDetectorTest extends TestCase
1414
{
15-
/** @test */
16-
public function it_detects_n1_query_on_properties()
15+
public function test_it_detects_n1_query_on_properties()
1716
{
1817
Route::get('/', function (){
1918
$authors = Author::all();
@@ -34,8 +33,7 @@ public function it_detects_n1_query_on_properties()
3433
$this->assertSame('profile', $queries[0]['relation']);
3534
}
3635

37-
/** @test */
38-
public function it_detects_n1_query_on_multiple_requests()
36+
public function test_it_detects_n1_query_on_multiple_requests()
3937
{
4038
Route::get('/', function (){
4139
$authors = Author::get();
@@ -62,8 +60,7 @@ public function it_detects_n1_query_on_multiple_requests()
6260
$this->assertSame('profile', $queries[0]['relation']);
6361
}
6462

65-
/** @test */
66-
public function it_does_not_detect_a_false_n1_query_on_multiple_requests()
63+
public function test_it_does_not_detect_a_false_n1_query_on_multiple_requests()
6764
{
6865
Route::get('/', function (){
6966
$authors = Author::with("profile")->get();
@@ -82,8 +79,7 @@ public function it_does_not_detect_a_false_n1_query_on_multiple_requests()
8279
$this->assertCount(0, app(QueryDetector::class)->getDetectedQueries());
8380
}
8481

85-
/** @test */
86-
public function it_ignores_eager_loaded_relationships()
82+
public function test_it_ignores_eager_loaded_relationships()
8783
{
8884
Route::get('/', function (){
8985
$authors = Author::with('profile')->get();
@@ -100,8 +96,7 @@ public function it_ignores_eager_loaded_relationships()
10096
$this->assertCount(0, $queries);
10197
}
10298

103-
/** @test */
104-
public function it_detects_n1_queries_from_builder()
99+
public function test_it_detects_n1_queries_from_builder()
105100
{
106101
Route::get('/', function (){
107102
$authors = Author::with('profile')->get();
@@ -123,8 +118,7 @@ public function it_detects_n1_queries_from_builder()
123118
$this->assertSame(Post::class, $queries[0]['relation']);
124119
}
125120

126-
/** @test */
127-
public function it_detects_all_n1_queries()
121+
public function test_it_detects_all_n1_queries()
128122
{
129123
Route::get('/', function (){
130124
$authors = Author::with('profile')->get();
@@ -154,8 +148,7 @@ public function it_detects_all_n1_queries()
154148
$this->assertSame('author', $queries[1]['relation']);
155149
}
156150

157-
/** @test */
158-
public function it_detects_n1_queries_on_morph_relations()
151+
public function test_it_detects_n1_queries_on_morph_relations()
159152
{
160153
Route::get('/', function (){
161154
foreach (Post::all() as $post) {
@@ -174,8 +167,7 @@ public function it_detects_n1_queries_on_morph_relations()
174167
$this->assertSame('comments', $queries[0]['relation']);
175168
}
176169

177-
/** @test */
178-
public function it_detects_n1_queries_on_morph_relations_with_builder()
170+
public function test_it_detects_n1_queries_on_morph_relations_with_builder()
179171
{
180172
Route::get('/', function (){
181173
foreach (Post::all() as $post) {
@@ -194,8 +186,7 @@ public function it_detects_n1_queries_on_morph_relations_with_builder()
194186
$this->assertSame(Comment::class, $queries[0]['relation']);
195187
}
196188

197-
/** @test */
198-
public function it_can_be_disabled()
189+
public function test_it_can_be_disabled()
199190
{
200191
$this->app['config']->set('querydetector.enabled', false);
201192

@@ -212,8 +203,7 @@ public function it_can_be_disabled()
212203
$this->assertCount(0, $queries);
213204
}
214205

215-
/** @test */
216-
public function it_ignores_whitelisted_relations()
206+
public function test_it_ignores_whitelisted_relations()
217207
{
218208
$this->app['config']->set('querydetector.enabled', true);
219209
$this->app['config']->set('querydetector.except', [
@@ -235,8 +225,7 @@ public function it_ignores_whitelisted_relations()
235225
$this->assertCount(0, $queries);
236226
}
237227

238-
/** @test */
239-
public function it_ignores_whitelisted_relations_with_attributes()
228+
public function test_it_ignores_whitelisted_relations_with_attributes()
240229
{
241230
$this->app['config']->set('querydetector.enabled', true);
242231
$this->app['config']->set('querydetector.except', [
@@ -258,8 +247,7 @@ public function it_ignores_whitelisted_relations_with_attributes()
258247
$this->assertCount(0, $queries);
259248
}
260249

261-
/** @test */
262-
public function it_ignores_redirects()
250+
public function test_it_ignores_redirects()
263251
{
264252
Route::get('/', function (){
265253
foreach (Post::all() as $post) {
@@ -275,8 +263,7 @@ public function it_ignores_redirects()
275263
$this->assertCount(1, $queries);
276264
}
277265

278-
/** @test */
279-
public function it_fires_an_event_if_detects_n1_query()
266+
public function test_it_fires_an_event_if_detects_n1_query()
280267
{
281268
Event::fake();
282269

@@ -293,8 +280,7 @@ public function it_fires_an_event_if_detects_n1_query()
293280
Event::assertDispatched(QueryDetected::class);
294281
}
295282

296-
/** @test */
297-
public function it_does_not_fire_an_event_if_there_is_no_n1_query()
283+
public function test_it_does_not_fire_an_event_if_there_is_no_n1_query()
298284
{
299285
Event::fake();
300286

@@ -310,8 +296,7 @@ public function it_does_not_fire_an_event_if_there_is_no_n1_query()
310296

311297
Event::assertNotDispatched(QueryDetected::class);
312298
}
313-
/** @test */
314-
public function it_uses_the_trace_line_to_detect_queries()
299+
public function test_it_uses_the_trace_line_to_detect_queries()
315300
{
316301
Route::get('/', function (){
317302
$authors = Author::all();
@@ -337,8 +322,7 @@ public function it_uses_the_trace_line_to_detect_queries()
337322
$this->assertSame('profile', $queries[0]['relation']);
338323
}
339324

340-
/** @test */
341-
public function it_empty_queries()
325+
public function test_it_empty_queries()
342326
{
343327
Route::get('/', function (){
344328
$authors = Author::all();

0 commit comments

Comments
 (0)