aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2021-01-20 14:45:59 +0100
committerArthurHoaro <arthur@hoa.ro>2021-01-20 15:01:29 +0100
commit9b8c0a4560fa1d87cab1529099b1b4677e92e265 (patch)
tree330a9b1a42ff7b7f24a76612b57fae63417ef483 /tests/bookmark
parent055d97f9a9e67d8ee8ae81bbf59a4b846a145d9f (diff)
downloadShaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.tar.gz
Shaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.tar.zst
Shaarli-9b8c0a4560fa1d87cab1529099b1b4677e92e265.zip
Handle pagination through BookmarkService
Handle all search results through SearchResult object. This is a required step toward implementing a BookmarkService based on SQL database. Related to #953
Diffstat (limited to 'tests/bookmark')
-rw-r--r--tests/bookmark/BookmarkFileServiceTest.php8
-rw-r--r--tests/bookmark/SearchResultTest.php125
2 files changed, 129 insertions, 4 deletions
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
index f619aff3..d1af3fb0 100644
--- a/tests/bookmark/BookmarkFileServiceTest.php
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -807,7 +807,7 @@ class BookmarkFileServiceTest extends TestCase
807 $request = ['searchtags' => $tags]; 807 $request = ['searchtags' => $tags];
808 $this->assertEquals( 808 $this->assertEquals(
809 2, 809 2,
810 count($this->privateLinkDB->search($request, null, true)) 810 count($this->privateLinkDB->search($request, null, true)->getBookmarks())
811 ); 811 );
812 } 812 }
813 813
@@ -820,7 +820,7 @@ class BookmarkFileServiceTest extends TestCase
820 $request = ['searchtags' => $tags]; 820 $request = ['searchtags' => $tags];
821 $this->assertEquals( 821 $this->assertEquals(
822 2, 822 2,
823 count($this->privateLinkDB->search($request, null, true)) 823 count($this->privateLinkDB->search($request, null, true)->getBookmarks())
824 ); 824 );
825 } 825 }
826 826
@@ -834,12 +834,12 @@ class BookmarkFileServiceTest extends TestCase
834 $request = ['searchtags' => $tags]; 834 $request = ['searchtags' => $tags];
835 $this->assertEquals( 835 $this->assertEquals(
836 1, 836 1,
837 count($this->privateLinkDB->search($request, 'all', true)) 837 count($this->privateLinkDB->search($request, 'all', true)->getBookmarks())
838 ); 838 );
839 839
840 $this->assertEquals( 840 $this->assertEquals(
841 0, 841 0,
842 count($this->publicLinkDB->search($request, 'public', true)) 842 count($this->publicLinkDB->search($request, 'public', true)->getBookmarks())
843 ); 843 );
844 } 844 }
845 845
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
3declare(strict_types=1);
4
5namespace Shaarli\Bookmark;
6
7use Shaarli\TestCase;
8
9/**
10 * Test SearchResult class.
11 */
12class 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}