https://github.com/lucasdotvin/laravel-database-queries-counter
A simple way to check how many queries a test suite has performed.
https://github.com/lucasdotvin/laravel-database-queries-counter
database-perfomance laravel php testing
Last synced: 5 months ago
JSON representation
A simple way to check how many queries a test suite has performed.
- Host: GitHub
- URL: https://github.com/lucasdotvin/laravel-database-queries-counter
- Owner: lucasdotvin
- License: mit
- Created: 2022-02-05T16:11:24.000Z (over 4 years ago)
- Default Branch: develop
- Last Pushed: 2023-10-09T16:05:40.000Z (over 2 years ago)
- Last Synced: 2025-02-21T17:16:27.544Z (over 1 year ago)
- Topics: database-perfomance, laravel, php, testing
- Language: PHP
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Laravel Database Queries Counter
[](https://packagist.org/packages/lucasdotvin/laravel-database-queries-counter)
[](https://github.com/lucasdotvin/laravel-database-queries-counter/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/lucasdotvin/laravel-database-queries-counter/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/lucasdotvin/laravel-database-queries-counter)
This package provides a simple way to check how many queries a test suite has performed.
> It is important to keep in mind that merely controlling how many queries you perform on the database is not enough to ensure your application has a good performance. This package does not intend to do it, instead, it is thought to help you prevent common mistakes (like N+1 queries).
## Installation
You can install the package via composer:
```bash
composer require lucasdotvin/laravel-database-queries-counter --dev
```
You can publish the traits if you want to extend them:
```bash
php artisan vendor:publish --tag="laravel-database-queries-counter-traits"
```
## Usage
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:
```php
times(10)->create();
$user = User::factory()->create();
$this->actingAs($user);
$this->startCountingQueries();
$response = $this->get(route('posts.index'));
$this->stopCountingQueries();
$response->assertSuccessful();
$this->assertDatabaseQueriesCount(1);
}
}
```
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:
```php
public function testIndexPageDoesNotPerfomNPlusOneQueries()
{
Post::factory()->times(10)->create();
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->whileCountingQueries(fn () => $this->get(route('posts.index')));
$response->assertSuccessful();
$this->assertDatabaseQueriesCount(1);
}
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Lucas Vinicius](https://github.com/lucasdotvin)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.