You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-28Lines changed: 47 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,11 @@
1
-
# This package provides a simple trait to check how many queries a test suite has performed.
1
+
# Laravel Database Queries Counter
2
2
3
3
[](https://packagist.org/packages/lucasdotdev/laravel-database-queries-counter)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
15
-
16
-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
8
+
This package provides a simple way to check how many queries a test suite has performed.
17
9
18
10
## Installation
19
11
@@ -23,37 +15,64 @@ You can install the package via composer:
This is the contents of the published config file:
26
+
Add the `CountsQueries` trait to your test suite class to access the package methods, like `startCountingQueries`, `stopCountingQueries`, and `assertDatabaseQueriesCount`, as demonstrated below, where we assert that an index route does not perform N+1 queries to load the posts from a blog:
use LucasDotDev\DBQueriesCounter\Traits\CountsQueries;
36
+
use Tests\TestCase;
37
+
38
+
class PostTest extends TestCase
39
+
{
40
+
use CountsQueries;
41
+
42
+
public function testIndexPageDoesNotPerfomNPlusOneQueries()
43
+
{
44
+
Post::factory()->times(10)->create();
45
+
46
+
$user = User::factory()->create();
47
+
$this->actingAs($user);
48
+
49
+
$this->startCountingQueries();
50
+
51
+
$response = $this->get(route('posts.index'));
52
+
53
+
$this->stopCountingQueries();
54
+
55
+
$response->assertSuccessful();
56
+
$this->assertDatabaseQueriesCount(1);
57
+
}
58
+
}
50
59
```
51
60
52
-
## Usage
61
+
You can also use the method `whileCountingQueries` to avoid having to control when to start and stop counting queries, as below, where we refactor the example above:
53
62
54
63
```php
55
-
$DBQueriesCounter = new LucasDotDev\DBQueriesCounter();
0 commit comments