forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalPackageTest.php
50 lines (42 loc) · 1.4 KB
/
ExternalPackageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Tests;
use Illuminate\Http\Request;
use MongoDB\Laravel\Tests\Models\User;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\QueryBuilder;
/**
* Test integration with external packages.
*/
class ExternalPackageTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
User::truncate();
}
/**
* Integration test with spatie/laravel-query-builder.
*/
public function testSpacieQueryBuilder(): void
{
User::insert([
['name' => 'Jimmy Doe', 'birthday' => '2012-11-12', 'role' => 'user'],
['name' => 'John Doe', 'birthday' => '1980-07-08', 'role' => 'admin'],
['name' => 'Jane Doe', 'birthday' => '1983-09-10', 'role' => 'admin'],
['name' => 'Jess Doe', 'birthday' => '2014-05-06', 'role' => 'user'],
]);
$request = Request::create('/users', 'GET', ['filter' => ['role' => 'admin'], 'sort' => '-birthday']);
$result = QueryBuilder::for(User::class, $request)
->allowedFilters([
AllowedFilter::exact('role'),
])
->allowedSorts([
AllowedSort::field('birthday'),
])
->get();
$this->assertCount(2, $result);
$this->assertSame('Jane Doe', $result[0]->name);
}
}