diff options
Diffstat (limited to 'tests/bookmark/SearchResultTest.php')
-rw-r--r-- | tests/bookmark/SearchResultTest.php | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/bookmark/SearchResultTest.php b/tests/bookmark/SearchResultTest.php new file mode 100644 index 00000000..12854c1f --- /dev/null +++ b/tests/bookmark/SearchResultTest.php | |||
@@ -0,0 +1,125 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Bookmark; | ||
6 | |||
7 | use Shaarli\TestCase; | ||
8 | |||
9 | /** | ||
10 | * Test SearchResult class. | ||
11 | */ | ||
12 | class SearchResultTest extends TestCase | ||
13 | { | ||
14 | /** Create a SearchResult without any pagination parameter. */ | ||
15 | public function testResultNoParameters(): void | ||
16 | { | ||
17 | $searchResult = SearchResult::getSearchResult($data = ['a', 'b', 'c', 'd', 'e', 'f']); | ||
18 | |||
19 | static::assertSame($data, $searchResult->getBookmarks()); | ||
20 | static::assertSame(6, $searchResult->getResultCount()); | ||
21 | static::assertSame(6, $searchResult->getTotalCount()); | ||
22 | static::assertSame(null, $searchResult->getLimit()); | ||
23 | static::assertSame(0, $searchResult->getOffset()); | ||
24 | static::assertSame(1, $searchResult->getPage()); | ||
25 | static::assertSame(1, $searchResult->getLastPage()); | ||
26 | static::assertTrue($searchResult->isFirstPage()); | ||
27 | static::assertTrue($searchResult->isLastPage()); | ||
28 | } | ||
29 | |||
30 | /** Create a SearchResult with only an offset parameter */ | ||
31 | public function testResultWithOffset(): void | ||
32 | { | ||
33 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 2); | ||
34 | |||
35 | static::assertSame([2 => 'c', 3 => 'd', 4 => 'e', 5 => 'f'], $searchResult->getBookmarks()); | ||
36 | static::assertSame(4, $searchResult->getResultCount()); | ||
37 | static::assertSame(6, $searchResult->getTotalCount()); | ||
38 | static::assertSame(null, $searchResult->getLimit()); | ||
39 | static::assertSame(2, $searchResult->getOffset()); | ||
40 | static::assertSame(2, $searchResult->getPage()); | ||
41 | static::assertSame(2, $searchResult->getLastPage()); | ||
42 | static::assertFalse($searchResult->isFirstPage()); | ||
43 | static::assertTrue($searchResult->isLastPage()); | ||
44 | } | ||
45 | |||
46 | /** Create a SearchResult with only a limit parameter */ | ||
47 | public function testResultWithLimit(): void | ||
48 | { | ||
49 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 0, 2); | ||
50 | |||
51 | static::assertSame([0 => 'a', 1 => 'b'], $searchResult->getBookmarks()); | ||
52 | static::assertSame(2, $searchResult->getResultCount()); | ||
53 | static::assertSame(6, $searchResult->getTotalCount()); | ||
54 | static::assertSame(2, $searchResult->getLimit()); | ||
55 | static::assertSame(0, $searchResult->getOffset()); | ||
56 | static::assertSame(1, $searchResult->getPage()); | ||
57 | static::assertSame(3, $searchResult->getLastPage()); | ||
58 | static::assertTrue($searchResult->isFirstPage()); | ||
59 | static::assertFalse($searchResult->isLastPage()); | ||
60 | } | ||
61 | |||
62 | /** Create a SearchResult with offset and limit parameters */ | ||
63 | public function testResultWithLimitAndOffset(): void | ||
64 | { | ||
65 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 2, 2); | ||
66 | |||
67 | static::assertSame([2 => 'c', 3 => 'd'], $searchResult->getBookmarks()); | ||
68 | static::assertSame(2, $searchResult->getResultCount()); | ||
69 | static::assertSame(6, $searchResult->getTotalCount()); | ||
70 | static::assertSame(2, $searchResult->getLimit()); | ||
71 | static::assertSame(2, $searchResult->getOffset()); | ||
72 | static::assertSame(2, $searchResult->getPage()); | ||
73 | static::assertSame(3, $searchResult->getLastPage()); | ||
74 | static::assertFalse($searchResult->isFirstPage()); | ||
75 | static::assertFalse($searchResult->isLastPage()); | ||
76 | } | ||
77 | |||
78 | /** Create a SearchResult with offset and limit parameters displaying the last page */ | ||
79 | public function testResultWithLimitAndOffsetLastPage(): void | ||
80 | { | ||
81 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 4, 2); | ||
82 | |||
83 | static::assertSame([4 => 'e', 5 => 'f'], $searchResult->getBookmarks()); | ||
84 | static::assertSame(2, $searchResult->getResultCount()); | ||
85 | static::assertSame(6, $searchResult->getTotalCount()); | ||
86 | static::assertSame(2, $searchResult->getLimit()); | ||
87 | static::assertSame(4, $searchResult->getOffset()); | ||
88 | static::assertSame(3, $searchResult->getPage()); | ||
89 | static::assertSame(3, $searchResult->getLastPage()); | ||
90 | static::assertFalse($searchResult->isFirstPage()); | ||
91 | static::assertTrue($searchResult->isLastPage()); | ||
92 | } | ||
93 | |||
94 | /** Create a SearchResult with offset and limit parameters out of bound (display the last page) */ | ||
95 | public function testResultWithLimitAndOffsetOutOfBounds(): void | ||
96 | { | ||
97 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 12, 2); | ||
98 | |||
99 | static::assertSame([4 => 'e', 5 => 'f'], $searchResult->getBookmarks()); | ||
100 | static::assertSame(2, $searchResult->getResultCount()); | ||
101 | static::assertSame(6, $searchResult->getTotalCount()); | ||
102 | static::assertSame(2, $searchResult->getLimit()); | ||
103 | static::assertSame(-2, $searchResult->getOffset()); | ||
104 | static::assertSame(3, $searchResult->getPage()); | ||
105 | static::assertSame(3, $searchResult->getLastPage()); | ||
106 | static::assertFalse($searchResult->isFirstPage()); | ||
107 | static::assertTrue($searchResult->isLastPage()); | ||
108 | } | ||
109 | |||
110 | /** Create a SearchResult with offset and limit parameters out of bound (no result) */ | ||
111 | public function testResultWithLimitAndOffsetOutOfBoundsNoResult(): void | ||
112 | { | ||
113 | $searchResult = SearchResult::getSearchResult(['a', 'b', 'c', 'd', 'e', 'f'], 12, 2, true); | ||
114 | |||
115 | static::assertSame([], $searchResult->getBookmarks()); | ||
116 | static::assertSame(0, $searchResult->getResultCount()); | ||
117 | static::assertSame(6, $searchResult->getTotalCount()); | ||
118 | static::assertSame(2, $searchResult->getLimit()); | ||
119 | static::assertSame(12, $searchResult->getOffset()); | ||
120 | static::assertSame(7, $searchResult->getPage()); | ||
121 | static::assertSame(3, $searchResult->getLastPage()); | ||
122 | static::assertFalse($searchResult->isFirstPage()); | ||
123 | static::assertFalse($searchResult->isLastPage()); | ||
124 | } | ||
125 | } | ||