Skip to content

Commit a8d757b

Browse files
committed
Update README.md file
1 parent 9229320 commit a8d757b

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

README.md

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff 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
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/lucasdotdev/laravel-database-queries-counter.svg?style=flat-square)](https://packagist.org/packages/lucasdotdev/laravel-database-queries-counter)
44
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/lucasdotdev/laravel-database-queries-counter/run-tests?label=tests)](https://github.com/lucasdotdev/laravel-database-queries-counter/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/lucasdotdev/laravel-database-queries-counter/Check%20&%20fix%20styling?label=code%20style)](https://github.com/lucasdotdev/laravel-database-queries-counter/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/lucasdotdev/laravel-database-queries-counter.svg?style=flat-square)](https://packagist.org/packages/lucasdotdev/laravel-database-queries-counter)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
9-
10-
## Support us
11-
12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-database-queries-counter.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-database-queries-counter)
13-
14-
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.
179

1810
## Installation
1911

@@ -23,37 +15,64 @@ You can install the package via composer:
2315
composer require lucasdotdev/laravel-database-queries-counter
2416
```
2517

26-
You can publish and run the migrations with:
18+
You can publish the traits if you want to extend them:
2719

2820
```bash
29-
php artisan vendor:publish --tag="laravel-database-queries-counter-migrations"
30-
php artisan migrate
21+
php artisan vendor:publish --tag="laravel-database-queries-counter-traits"
3122
```
3223

33-
You can publish the config file with:
34-
35-
```bash
36-
php artisan vendor:publish --tag="laravel-database-queries-counter-config"
37-
```
24+
## Usage
3825

39-
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:
4027

4128
```php
42-
return [
43-
];
44-
```
29+
<?php
4530

46-
Optionally, you can publish the views using
31+
namespace Tests\Feature;
4732

48-
```bash
49-
php artisan vendor:publish --tag="laravel-database-queries-counter-views"
33+
use App\Models\Post;
34+
use App\Models\User;
35+
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+
}
5059
```
5160

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:
5362

5463
```php
55-
$DBQueriesCounter = new LucasDotDev\DBQueriesCounter();
56-
echo $DBQueriesCounter->echoPhrase('Hello, LucasDotDev!');
64+
public function testIndexPageDoesNotPerfomNPlusOneQueries()
65+
{
66+
Post::factory()->times(10)->create();
67+
68+
$user = User::factory()->create();
69+
$this->actingAs($user);
70+
71+
$response = $this->whileCountingQueries(fn () => $this->get(route('posts.index')));
72+
73+
$response->assertSuccessful();
74+
$this->assertDatabaseQueriesCount(1);
75+
}
5776
```
5877

5978
## Testing

0 commit comments

Comments
 (0)