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: docs/components/optional-components/tests.md
+61
Original file line number
Diff line number
Diff line change
@@ -585,6 +585,67 @@ public function testCanListAdminUsers(): void
585
585
}
586
586
```
587
587
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.
Copy file name to clipboardexpand all lines: versioned_docs/version-12.x/components/optional-components/tests.md
+61
Original file line number
Diff line number
Diff line change
@@ -585,6 +585,67 @@ public function testCanListAdminUsers(): void
585
585
}
586
586
```
587
587
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.
0 commit comments