generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCursorPaginatorTest.php
103 lines (84 loc) · 3.03 KB
/
CursorPaginatorTest.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Vanthao03596\LaravelCursorPaginate\Tests;
use PHPUnit\Framework\TestCase;
use Vanthao03596\LaravelCursorPaginate\Cursor;
use Vanthao03596\LaravelCursorPaginate\CursorPaginator;
class CursorPaginatorTest extends TestCase
{
public function testReturnsRelevantContextInformation()
{
$p = new CursorPaginator($array = [['id' => 1], ['id' => 2], ['id' => 3]], 2, null, [
'parameters' => ['id'],
]);
$this->assertTrue($p->hasPages());
$this->assertTrue($p->hasMorePages());
$this->assertEquals([['id' => 1], ['id' => 2]], $p->items());
$pageInfo = [
'data' => [['id' => 1], ['id' => 2]],
'path' => '/',
'per_page' => 2,
'next_page_url' => '/?cursor='.$this->getCursor(['id' => 2]),
'prev_page_url' => null,
];
$this->assertEquals($pageInfo, $p->toArray());
}
public function testPaginatorRemovesTrailingSlashes()
{
$p = new CursorPaginator(
$array = [['id' => 4], ['id' => 5], ['id' => 6]],
2,
null,
['path' => 'http://website.com/test/', 'parameters' => ['id']]
);
$this->assertSame('http://website.com/test?cursor='.$this->getCursor(['id' => 5]), $p->nextPageUrl());
}
public function testPaginatorGeneratesUrlsWithoutTrailingSlash()
{
$p = new CursorPaginator(
$array = [['id' => 4], ['id' => 5], ['id' => 6]],
2,
null,
['path' => 'http://website.com/test', 'parameters' => ['id']]
);
$this->assertSame('http://website.com/test?cursor='.$this->getCursor(['id' => 5]), $p->nextPageUrl());
}
public function testItRetrievesThePaginatorOptions()
{
$p = new CursorPaginator(
$array = [['id' => 4], ['id' => 5], ['id' => 6]],
2,
null,
$options = ['path' => 'http://website.com/test', 'parameters' => ['id']]
);
$this->assertSame($p->getOptions(), $options);
}
public function testPaginatorReturnsPath()
{
$p = new CursorPaginator(
$array = [['id' => 4], ['id' => 5], ['id' => 6]],
2,
null,
$options = ['path' => 'http://website.com/test', 'parameters' => ['id']]
);
$this->assertSame($p->path(), 'http://website.com/test');
}
public function testCanTransformPaginatorItems()
{
$p = new CursorPaginator(
$array = [['id' => 4], ['id' => 5], ['id' => 6]],
2,
null,
$options = ['path' => 'http://website.com/test', 'parameters' => ['id']]
);
$p->through(function ($item) {
$item['id'] = $item['id'] + 2;
return $item;
});
$this->assertInstanceOf(CursorPaginator::class, $p);
$this->assertSame([['id' => 6], ['id' => 7]], $p->items());
}
protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
}
}