Skip to content

Commit e857f68

Browse files
Merge pull request #106 from openforests-git/feature/SW/database-query-count-assertions
Add docs for the database query assertions
2 parents b9294b2 + 6b2833e commit e857f68

File tree

2 files changed

+122
-0
lines changed
  • docs/components/optional-components
  • versioned_docs/version-12.x/components/optional-components

2 files changed

+122
-0
lines changed

docs/components/optional-components/tests.md

+61
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,67 @@ public function testCanListAdminUsers(): void
585585
}
586586
```
587587

588+
## Database Query Count and Query Assertions
589+
590+
Apiato includes functionality for testing database query efficiency and accuracy.
591+
592+
### Running the profiler
593+
594+
To enable the database query profiler, you can use the helper method directly before your act part. Stopping it is not
595+
necessary, however if your assert section contains database queries, not stopping it before the assert section will
596+
include those queries in the count.
597+
598+
```php
599+
$this->startDatabaseQueryLog();
600+
$action->run($data);
601+
$this->stopDatabaseQueryLog();
602+
```
603+
604+
### Assertions
605+
606+
Use `assertDatabaseQueryCount` to assert the number of queries executed during the test.
607+
608+
```php
609+
$this->assertDatabaseQueryCount(3);
610+
```
611+
612+
Use `assertDatabaseExecutedQuery` and `assertDatabaseExecutedQueries` to assert the queries executed during the test. You can also use partial queries.
613+
614+
```php
615+
$this->assertDatabaseExecutedQuery('select * from "users"');
616+
$this->assertDatabaseExecutedQueries([
617+
'select * from "users" where "id" = ? limit 1',
618+
'select * from "roles" where "id" = ? limit 1',
619+
'insert into "role_user" ("role_id", "user_id") values (?, ?)',
620+
]);
621+
```
622+
623+
### Using a wrapper
624+
625+
You can also use helper methods to wrap your test code and automatically start and stop the profiler.
626+
627+
```php
628+
$this->profileDatabaseQueries(fn() => $action->run($data));
629+
```
630+
631+
You can also use the helpers to profile and assert the queries in one go.
632+
633+
```php
634+
$this->profileDatbaseQueryCount(3, fn() => $action->run($data));
635+
```
636+
637+
```php
638+
$this->profileDatabaseExecutedQuery('select * from "users"', fn() => $action->run($data));
639+
```
640+
641+
```php
642+
$this->profileDatabaseExecutedQueries([
643+
'select * from "users" where "id" = ? limit 1',
644+
'select * from "roles" where "id" = ? limit 1',
645+
'insert into "role_user" ("role_id", "user_id") values (?, ?)',
646+
], fn() => $action->run($data));
647+
```
648+
588649
## Faker
589650

590651
An instance of [Faker](https://github.com/FakerPHP/Faker) is automatically provided in every test class, allowing you to generate fake data easily.

versioned_docs/version-12.x/components/optional-components/tests.md

+61
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,67 @@ public function testCanListAdminUsers(): void
585585
}
586586
```
587587

588+
## Database Query Count and Query Assertions
589+
590+
Apiato includes functionality for testing database query efficiency and accuracy.
591+
592+
### Running the profiler
593+
594+
To enable the database query profiler, you can use the helper method directly before your act part. Stopping it is not
595+
necessary, however if your assert section contains database queries, not stopping it before the assert section will
596+
include those queries in the count.
597+
598+
```php
599+
$this->startDatabaseQueryLog();
600+
$action->run($data);
601+
$this->stopDatabaseQueryLog();
602+
```
603+
604+
### Assertions
605+
606+
Use `assertDatabaseQueryCount` to assert the number of queries executed during the test.
607+
608+
```php
609+
$this->assertDatabaseQueryCount(3);
610+
```
611+
612+
Use `assertDatabaseExecutedQuery` and `assertDatabaseExecutedQueries` to assert the queries executed during the test. You can also use partial queries.
613+
614+
```php
615+
$this->assertDatabaseExecutedQuery('select * from "users"');
616+
$this->assertDatabaseExecutedQueries([
617+
'select * from "users" where "id" = ? limit 1',
618+
'select * from "roles" where "id" = ? limit 1',
619+
'insert into "role_user" ("role_id", "user_id") values (?, ?)',
620+
]);
621+
```
622+
623+
### Using a wrapper
624+
625+
You can also use helper methods to wrap your test code and automatically start and stop the profiler.
626+
627+
```php
628+
$this->profileDatabaseQueries(fn() => $action->run($data));
629+
```
630+
631+
You can also use the helpers to profile and assert the queries in one go.
632+
633+
```php
634+
$this->profileDatbaseQueryCount(3, fn() => $action->run($data));
635+
```
636+
637+
```php
638+
$this->profileDatabaseExecutedQuery('select * from "users"', fn() => $action->run($data));
639+
```
640+
641+
```php
642+
$this->profileDatabaseExecutedQueries([
643+
'select * from "users" where "id" = ? limit 1',
644+
'select * from "roles" where "id" = ? limit 1',
645+
'insert into "role_user" ("role_id", "user_id") values (?, ?)',
646+
], fn() => $action->run($data));
647+
```
648+
588649
## Faker
589650

590651
An instance of [Faker](https://github.com/FakerPHP/Faker) is automatically provided in every test class, allowing you to generate fake data easily.

0 commit comments

Comments
 (0)