|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -namespace Spatie\JsonApiPaginate\Test; |
4 |
| - |
5 |
| -use Illuminate\Database\Query\Builder; |
6 | 3 | use Illuminate\Support\Facades\DB;
|
7 |
| - |
8 |
| -class BuilderTest extends TestCase |
9 |
| -{ |
10 |
| - /** @test */ |
11 |
| - public function it_can_paginate_records() |
12 |
| - { |
13 |
| - $paginator = TestModel::jsonPaginate(); |
14 |
| - |
15 |
| - $this->assertEquals('http://localhost?page%5Bnumber%5D=2', $paginator->nextPageUrl()); |
16 |
| - } |
17 |
| - |
18 |
| - /** @test */ |
19 |
| - public function it_can_paginate_records_with_cursor() |
20 |
| - { |
21 |
| - config()->set('json-api-paginate.use_cursor_pagination', true); |
22 |
| - |
23 |
| - $paginator = TestModel::jsonPaginate(); |
24 |
| - |
25 |
| - $this->assertEquals('http://localhost?page%5Bcursor%5D=eyJ0ZXN0X21vZGVscy5pZCI6MzAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0', $paginator->nextPageUrl()); |
26 |
| - } |
27 |
| - |
28 |
| - /** @test */ |
29 |
| - public function it_returns_the_amount_of_records_specified_in_the_config_file() |
30 |
| - { |
31 |
| - config()->set('json-api-paginate.default_size', 10); |
32 |
| - |
33 |
| - $paginator = TestModel::jsonPaginate(); |
34 |
| - |
35 |
| - $this->assertCount(10, $paginator); |
36 |
| - } |
37 |
| - |
38 |
| - /** @test */ |
39 |
| - public function it_returns_the_amount_of_records_specified_in_the_config_file_using_cursor() |
40 |
| - { |
41 |
| - config()->set('json-api-paginate.use_cursor_pagination', true); |
42 |
| - config()->set('json-api-paginate.default_size', 10); |
43 |
| - |
44 |
| - $paginator = TestModel::jsonPaginate(); |
45 |
| - |
46 |
| - $this->assertCount(10, $paginator); |
47 |
| - } |
48 |
| - |
49 |
| - /** @test */ |
50 |
| - public function it_can_return_the_specified_amount_of_records() |
51 |
| - { |
52 |
| - $paginator = TestModel::jsonPaginate(15); |
53 |
| - |
54 |
| - $this->assertCount(15, $paginator); |
55 |
| - } |
56 |
| - |
57 |
| - /** @test */ |
58 |
| - public function it_can_return_the_specified_amount_of_records_with_cursor() |
59 |
| - { |
60 |
| - config()->set('json-api-paginate.use_cursor_pagination', true); |
61 |
| - |
62 |
| - $paginator = TestModel::jsonPaginate(15); |
63 |
| - |
64 |
| - $this->assertCount(15, $paginator); |
65 |
| - } |
66 |
| - |
67 |
| - /** @test */ |
68 |
| - public function it_will_not_return_more_records_that_the_configured_maximum() |
69 |
| - { |
70 |
| - $paginator = TestModel::jsonPaginate(15); |
71 |
| - |
72 |
| - $this->assertCount(15, $paginator); |
73 |
| - } |
74 |
| - |
75 |
| - /** @test */ |
76 |
| - public function it_can_set_a_custom_base_url_in_the_config_file() |
77 |
| - { |
78 |
| - config()->set('json-api-paginate.base_url', 'https://example.com'); |
79 |
| - |
80 |
| - $paginator = TestModel::jsonPaginate(); |
81 |
| - |
82 |
| - $this->assertEquals('https://example.com?page%5Bnumber%5D=2', $paginator->nextPageUrl()); |
83 |
| - } |
84 |
| - |
85 |
| - /** @test */ |
86 |
| - public function it_can_use_simple_pagination() |
87 |
| - { |
88 |
| - config()->set('json-api-paginate.use_simple_pagination', true); |
89 |
| - |
90 |
| - $paginator = TestModel::jsonPaginate(); |
91 |
| - |
92 |
| - $this->assertFalse(method_exists($paginator, 'total')); |
93 |
| - } |
94 |
| - |
95 |
| - /** @test */ |
96 |
| - public function it_can_use_cursor_pagination() |
97 |
| - { |
98 |
| - config()->set('json-api-paginate.use_cursor_pagination', true); |
99 |
| - |
100 |
| - $paginator = TestModel::jsonPaginate(); |
101 |
| - |
102 |
| - $this->assertFalse(method_exists($paginator, 'total')); |
103 |
| - } |
104 |
| - |
105 |
| - /** @test */ |
106 |
| - public function it_can_use_base_query_builder() |
107 |
| - { |
108 |
| - $paginator = DB::table('test_models')->jsonPaginate(); |
109 |
| - $this->assertEquals('http://localhost?page%5Bnumber%5D=2', $paginator->nextPageUrl()); |
110 |
| - } |
111 |
| - |
112 |
| - /** @test */ |
113 |
| - public function it_can_use_base_query_builder_with_cursor_pagination() |
114 |
| - { |
115 |
| - config()->set('json-api-paginate.use_cursor_pagination', true); |
116 |
| - |
117 |
| - $paginator = DB::table('test_models')->orderBy('id')->jsonPaginate(10); |
118 |
| - $this->assertEquals('http://localhost?page%5Bcursor%5D=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0', $paginator->nextPageUrl()); |
119 |
| - } |
120 |
| -} |
| 4 | +use Spatie\JsonApiPaginate\Test\TestModel; |
| 5 | + |
| 6 | +it('can paginate records') |
| 7 | + ->expect(fn () => TestModel::jsonPaginate())->nextPageUrl() |
| 8 | + ->toEqual('http://localhost?page%5Bnumber%5D=2'); |
| 9 | + |
| 10 | +it('can paginate records with cursor') |
| 11 | + ->tap(fn () => config()->set('json-api-paginate.use_cursor_pagination', true)) |
| 12 | + ->expect(fn () => TestModel::jsonPaginate()->nextPageUrl()) |
| 13 | + ->toEqual('http://localhost?page%5Bcursor%5D=eyJ0ZXN0X21vZGVscy5pZCI6MzAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0'); |
| 14 | + |
| 15 | +it('returns the amount of records specified in the config file') |
| 16 | + ->tap(fn () => config()->set('json-api-paginate.default_size', 10)) |
| 17 | + ->expect(fn () => TestModel::jsonPaginate()) |
| 18 | + ->toHaveCount(10); |
| 19 | + |
| 20 | +it('returns the amount of records specified in the config file using cursor') |
| 21 | + ->tap(fn () => config()->set('json-api-paginate.use_cursor_pagination', true)) |
| 22 | + ->tap(fn () => config()->set('json-api-paginate.default_size', 10)) |
| 23 | + ->expect(fn () => TestModel::jsonPaginate()) |
| 24 | + ->toHaveCount(10); |
| 25 | + |
| 26 | +it('can return the specified amount of records') |
| 27 | + ->expect(fn () => TestModel::jsonPaginate(15)) |
| 28 | + ->toHaveCount(15); |
| 29 | + |
| 30 | +it('can return the specified amount of records with cursor') |
| 31 | + ->tap(fn () => config()->set('json-api-paginate.use_cursor_pagination', true)) |
| 32 | + ->expect(fn () => TestModel::jsonPaginate(15)) |
| 33 | + ->toHaveCount(15); |
| 34 | + |
| 35 | +it('will not return more records that the configured maximum') |
| 36 | + ->expect(fn () => TestModel::jsonPaginate(15)) |
| 37 | + ->toHaveCount(15); |
| 38 | + |
| 39 | +it('can set a custom base url in the config file') |
| 40 | + ->tap(fn () => config()->set('json-api-paginate.base_url', 'https://example.com')) |
| 41 | + ->expect(fn () => TestModel::jsonPaginate()->nextPageUrl()) |
| 42 | + ->toEqual('https://example.com?page%5Bnumber%5D=2'); |
| 43 | + |
| 44 | +it('can use simple pagination') |
| 45 | + ->tap(fn () => config()->set('json-api-paginate.use_simple_pagination', true)) |
| 46 | + ->expect( |
| 47 | + fn () => method_exists(TestModel::jsonPaginate(), 'total') |
| 48 | + )->toBeFalse(); |
| 49 | + |
| 50 | +it('can use cursor pagination') |
| 51 | + ->tap(fn () => config()->set('json-api-paginate.use_cursor_pagination', true)) |
| 52 | + ->expect( |
| 53 | + fn () => method_exists(TestModel::jsonPaginate(), 'total') |
| 54 | + )->toBeFalse(); |
| 55 | + |
| 56 | +it('can use base query builder') |
| 57 | + ->expect(fn () => DB::table('test_models')->jsonPaginate()->nextPageUrl()) |
| 58 | + ->toEqual('http://localhost?page%5Bnumber%5D=2'); |
| 59 | + |
| 60 | +it('can use base query builder with custom pagination') |
| 61 | + ->tap(fn () => config()->set('json-api-paginate.use_cursor_pagination', true)) |
| 62 | + ->expect(fn () => DB::table('test_models')->orderBy('id')->jsonPaginate(10))->nextPageUrl() |
| 63 | + ->toEqual('http://localhost?page%5Bcursor%5D=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0'); |
0 commit comments