Skip to content

Commit 0958770

Browse files
authored
Merge pull request #68 from alexmanase/refactor-tests-to-pest
Refactor tests to Pest
2 parents ddb9486 + 0de4326 commit 0958770

File tree

7 files changed

+125
-218
lines changed

7 files changed

+125
-218
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ jobs:
4747
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
4848
4949
- name: Execute tests
50-
run: vendor/bin/phpunit
50+
run: vendor/bin/pest

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"\/Users\/alexandrumanase\/Documents\/repos\/laravel-json-api-paginate\/tests\/BuilderTest.php::it":4},"times":{"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_paginate_records":0.073,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_paginate_records_with_cursor":0.071,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_returns_the_amount_of_records_specified_in_the_config_file":0.005,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_returns_the_amount_of_records_specified_in_the_config_file_using_cursor":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_return_the_specified_amount_of_records":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_return_the_specified_amount_of_records_with_cursor":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_will_not_return_more_records_that_the_configured_maximum":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_set_a_custom_base_url_in_the_config_file":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_use_simple_pagination":0.004,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_use_cursor_pagination":0.005,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_use_base_query_builder":0.005,"Spatie\\JsonApiPaginate\\Test\\BuilderTest::it_can_use_base_query_builder_with_cursor_pagination":0.004,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-json-api-paginate\/tests\/BuilderTest.php::it":0.004,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-json-api-paginate\/tests\/RequestTest.php::it":0.006}}

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0",
24+
"pestphp/pest": "^1.22",
2425
"phpunit/phpunit": "^8.0|^9.0"
2526
},
2627
"autoload": {
@@ -34,10 +35,13 @@
3435
}
3536
},
3637
"scripts": {
37-
"test": "vendor/bin/phpunit"
38+
"test": "vendor/bin/pest"
3839
},
3940
"config": {
40-
"sort-packages": true
41+
"sort-packages": true,
42+
"allow-plugins": {
43+
"pestphp/pest-plugin": true
44+
}
4145
},
4246
"extra": {
4347
"laravel": {
@@ -48,4 +52,4 @@
4852
},
4953
"minimum-stability": "dev",
5054
"prefer-stable": true
51-
}
55+
}

tests/BuilderTest.php

Lines changed: 60 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,63 @@
11
<?php
22

3-
namespace Spatie\JsonApiPaginate\Test;
4-
5-
use Illuminate\Database\Query\Builder;
63
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');

tests/Pest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
*/
8+
9+
uses(Spatie\JsonApiPaginate\Test\TestCase::class)->in('.');

tests/RequestTest.php

Lines changed: 46 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,65 @@
11
<?php
22

3-
namespace Spatie\JsonApiPaginate\Test;
3+
it('will discover the page size parameter')
4+
->get('/?page[size]=2')
5+
->assertJsonFragment(['per_page' => 2]);
46

5-
class RequestTest extends TestCase
6-
{
7-
/** @test */
8-
public function it_will_discover_the_page_size_parameter()
9-
{
10-
$response = $this->get('/?page[size]=2');
7+
it('will discover the page number parameter')
8+
->get('/?page[number]=2')
9+
->assertJsonFragment(['current_page' => 2]);
1110

12-
$response->assertJsonFragment(['per_page' => 2]);
13-
}
11+
it('will discover the cursor parameter')
12+
->get('cursor/?page[cursor]=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0')
13+
->assertJsonFragment(['prev_cursor' => 'eyJpZCI6MTEsIl9wb2ludHNUb05leHRJdGVtcyI6ZmFsc2V9']);
1414

15-
/** @test */
16-
public function it_will_discover_the_page_number_parameter()
17-
{
18-
$response = $this->get('/?page[number]=2');
15+
it('will use the default page size')
16+
->get('/')
17+
->assertJsonFragment(['per_page' => 30]);
1918

20-
$response->assertJsonFragment(['current_page' => 2]);
21-
}
19+
it('will use the configured page size parameter')
20+
->tap(fn () => config(['json-api-paginate.size_parameter' => 'modified_size']))
21+
->get('/?page[modified_size]=2')
22+
->assertJsonFragment(['per_page' => 2]);
2223

23-
/** @test */
24-
public function it_will_discover_the_cursor_parameter()
25-
{
26-
$response = $this->get('cursor/?page[cursor]=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0');
24+
it('will use the configured page size parameter for cursor')
25+
->tap(fn () => config(['json-api-paginate.size_parameter' => 'modified_size']))
26+
->get('cursor/?page[modified_size]=2')
27+
->assertJsonFragment(['per_page' => 2]);
2728

28-
$response->assertJsonFragment(['prev_cursor' => 'eyJpZCI6MTEsIl9wb2ludHNUb05leHRJdGVtcyI6ZmFsc2V9']);
29-
}
29+
it('will use the configured page number parameter')
30+
->tap(fn () => config(['json-api-paginate.number_parameter' => 'modified_number']))
31+
->get('/?page[modified_number]=2')
32+
->assertJsonFragment(['current_page' => 2]);
3033

31-
/** @test */
32-
public function it_will_use_the_default_page_size()
33-
{
34-
$response = $this->get('/');
34+
it('will use the configured cursor parameter')
35+
->tap(fn () => config(['json-api-paginate.cursor_parameter' => 'modified_cursor']))
36+
->get('cursor/?page[size]=10&page[modified_cursor]=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0')
37+
->assertJsonFragment(['next_cursor' => 'eyJpZCI6MjAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0']);
3538

36-
$response->assertJsonFragment(['per_page' => 30]);
37-
}
39+
it('will use the configured size parameter for cursor')
40+
->get('cursor/?page[size]=10')
41+
->assertJsonFragment(['next_cursor' => 'eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0']);
3842

39-
/** @test */
40-
public function it_will_use_the_configured_page_size_parameter()
41-
{
42-
config(['json-api-paginate.size_parameter' => 'modified_size']);
43+
it('will use default size when page size is 0', function () {
44+
$default_size = config('json-api-paginate.default_size');
4345

44-
$response = $this->get('/?page[modified_size]=2');
46+
$response = $this->get('/?page[size]=0');
4547

46-
$response->assertJsonFragment(['per_page' => 2]);
47-
}
48+
$response->assertJsonFragment(['per_page' => $default_size]);
49+
});
4850

49-
/** @test */
50-
public function it_will_use_the_configured_page_size_parameter_for_cursor()
51-
{
52-
config(['json-api-paginate.size_parameter' => 'modified_size']);
51+
it('will use default size when page size is negative', function () {
52+
$default_size = config('json-api-paginate.default_size');
5353

54-
$response = $this->get('cursor/?page[modified_size]=2');
54+
$response = $this->get('/?page[size]=-1');
5555

56-
$response->assertJsonFragment(['per_page' => 2]);
57-
}
56+
$response->assertJsonFragment(['per_page' => $default_size]);
57+
});
5858

59-
/** @test */
60-
public function it_will_use_the_configured_page_number_parameter()
61-
{
62-
config(['json-api-paginate.number_parameter' => 'modified_number']);
59+
it('will use default size when page size is illegal', function () {
60+
$default_size = config('json-api-paginate.default_size');
6361

64-
$response = $this->get('/?page[modified_number]=2');
62+
$response = $this->get('/?page[size]=Rpfwj5N1b7');
6563

66-
$response->assertJsonFragment(['current_page' => 2]);
67-
}
68-
69-
/** @test */
70-
public function it_will_use_the_configured_cursor_parameter()
71-
{
72-
config(['json-api-paginate.cursor_parameter' => 'modified_cursor']);
73-
74-
75-
$response = $this->get('cursor/?page[size]=10&page[modified_cursor]=eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0');
76-
$response->assertJsonFragment(['next_cursor' => 'eyJpZCI6MjAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0']);
77-
}
78-
79-
/** @test */
80-
public function it_will_use_the_configured_size_parameter_for_cursor()
81-
{
82-
$response = $this->get('cursor/?page[size]=10');
83-
84-
$response->assertJsonFragment(['next_cursor' => 'eyJpZCI6MTAsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0']);
85-
}
86-
87-
public function it_will_use_default_size_when_page_size_is_zero()
88-
{
89-
$default_size = config('json-api-paginate.default_size');
90-
91-
$response = $this->get('/?page[size]=0');
92-
93-
$response->assertJsonFragment(['per_page' => $default_size]);
94-
}
95-
96-
/** @test */
97-
public function it_will_use_default_size_when_page_size_is_negative()
98-
{
99-
$default_size = config('json-api-paginate.default_size');
100-
101-
$response = $this->get('/?page[size]=-1');
102-
103-
$response->assertJsonFragment(['per_page' => $default_size]);
104-
}
105-
106-
/** @test */
107-
public function it_will_use_default_size_when_page_size_is_illegal()
108-
{
109-
$default_size = config('json-api-paginate.default_size');
110-
111-
$response = $this->get('/?page[size]=Rpfwj5N1b7');
112-
113-
$response->assertJsonFragment(['per_page' => $default_size]);
114-
}
115-
}
64+
$response->assertJsonFragment(['per_page' => $default_size]);
65+
});

0 commit comments

Comments
 (0)