1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Relay\Connection; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
6
|
|
|
use Overblog\GraphQLBundle\Relay\Connection\Paginator; |
7
|
|
|
|
8
|
|
|
class PaginatorTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
public function testForward() |
11
|
|
|
{ |
12
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
13
|
|
|
$this->assertSame(0, $offset); |
14
|
|
|
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available |
15
|
|
|
|
16
|
|
|
return array_fill(0, 6, 'item'); |
17
|
|
|
}); |
18
|
|
|
|
19
|
|
|
$this->assertCount(5, $paginator->forward(new Argument(['first' => 5]))->edges); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testForwardAfter() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
25
|
|
|
$this->assertSame(5, $offset); |
26
|
|
|
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available |
27
|
|
|
|
28
|
|
|
return array_fill(0, 6, 'item'); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
$this->assertCount(5, $paginator->forward(new Argument(['first' => 5, 'after' => base64_encode('arrayconnection:5') ]))->edges); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testBackward() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
37
|
|
|
$this->assertSame(5, $offset); |
38
|
|
|
$this->assertSame(5, $limit); |
39
|
|
|
|
40
|
|
|
return array_fill(0, 5, 'item'); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$this->assertCount(5, $paginator->backward(new Argument(['last' => 5]), 10)->edges); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testBackwardBefore() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
49
|
|
|
$this->assertSame(0, $offset); |
50
|
|
|
$this->assertSame(5, $limit); |
51
|
|
|
|
52
|
|
|
return array_fill(0, 5, 'item'); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->assertCount(5, $paginator->backward(new Argument(['last' => 5, 'before' => base64_encode('arrayconnection:5')]), 10)->edges); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testAuto() |
59
|
|
|
{ |
60
|
|
|
// Backward |
61
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
62
|
|
|
$this->assertSame(5, $offset); |
63
|
|
|
$this->assertSame(5, $limit); |
64
|
|
|
|
65
|
|
|
return array_fill(0, 5, 'item'); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->assertCount(5, $paginator->auto(new Argument(['last' => 5]), 10)->edges); |
69
|
|
|
|
70
|
|
|
// Forward |
71
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
72
|
|
|
$this->assertSame(0, $offset); |
73
|
|
|
$this->assertSame(6, $limit); // Includes the extra element to check if next page is available |
74
|
|
|
|
75
|
|
|
return array_fill(0, 5, 'item'); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$this->assertCount(5, $paginator->auto(new Argument(['first' => 5]), 10)->edges); |
79
|
|
|
|
80
|
|
|
// Backward + callable |
81
|
|
|
$paginator = new Paginator(function ($offset, $limit) { |
82
|
|
|
$this->assertSame(5, $offset); |
83
|
|
|
$this->assertSame(5, $limit); |
84
|
|
|
|
85
|
|
|
return array_fill(0, 5, 'item'); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
$countCalled = false; |
89
|
|
|
$result = $paginator->auto(new Argument(['last' => 5]), function () use (&$countCalled) { |
90
|
|
|
$countCalled = true; |
91
|
|
|
return 10; |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
$this->assertTrue($countCalled); |
95
|
|
|
$this->assertCount(5, $result->edges); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.