|
1 | 1 | <?php |
2 | 2 |
|
3 | | -uses(\Spatie\QueryBuilder\Tests\TestCase::class)->in('tests', 'Concerns', 'TestClasses'); |
| 3 | +use Illuminate\Support\Facades\DB; |
| 4 | +use Spatie\QueryBuilder\QueryBuilder; |
| 5 | +use Spatie\QueryBuilder\Tests\TestCase; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Spatie\QueryBuilder\Tests\TestClasses\Models\TestModel; |
4 | 8 |
|
5 | | -/* |
6 | | -|-------------------------------------------------------------------------- |
7 | | -| Test Case |
8 | | -|-------------------------------------------------------------------------- |
9 | | -| |
10 | | -| The closure you provide to your test functions is always bound to a specific PHPUnit test |
11 | | -| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may |
12 | | -| need to change it using the "uses()" function to bind a different classes or traits. |
13 | | -| |
14 | | -*/ |
| 9 | +uses(TestCase::class)->in(__DIR__); |
15 | 10 |
|
16 | | -/** @link https://pestphp.com/docs/underlying-test-case */ |
| 11 | +function createQueryFromFilterRequest(array $filters, string $model = null): QueryBuilder |
| 12 | +{ |
| 13 | + $model ??= TestModel::class; |
17 | 14 |
|
18 | | -/* |
19 | | -|-------------------------------------------------------------------------- |
20 | | -| Expectations |
21 | | -|-------------------------------------------------------------------------- |
22 | | -| |
23 | | -| When you're writing tests, you often need to check that values meet certain conditions. The |
24 | | -| "expect()" function gives you access to a set of "expectations" methods that you can use |
25 | | -| to assert different things. Of course, you may extend the Expectation API at any time. |
26 | | -| |
27 | | -*/ |
| 15 | + $request = new Request([ |
| 16 | + 'filter' => $filters, |
| 17 | + ]); |
28 | 18 |
|
29 | | -/** @link https://pestphp.com/docs/expectations#custom-expectations */ |
| 19 | + return QueryBuilder::for($model, $request); |
| 20 | +} |
30 | 21 |
|
31 | | -/* |
32 | | -|-------------------------------------------------------------------------- |
33 | | -| Functions |
34 | | -|-------------------------------------------------------------------------- |
35 | | -| |
36 | | -| While Pest is very powerful out-of-the-box, you may have some testing code specific to your |
37 | | -| project that you don't want to repeat in every file. Here you can also expose helpers as |
38 | | -| global functions to help you to reduce the number of lines of code in your test files. |
39 | | -| |
40 | | -*/ |
| 22 | +function assertQueryExecuted(string $query) |
| 23 | +{ |
| 24 | + $queries = array_map(function ($queryLogItem) { |
| 25 | + return $queryLogItem['query']; |
| 26 | + }, DB::getQueryLog()); |
41 | 27 |
|
42 | | -/** @link https://pestphp.com/docs/helpers */ |
| 28 | + expect($queries)->toContain($query); |
| 29 | +} |
0 commit comments