aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-10-28 12:44:44 +0200
committerGitHub <noreply@github.com>2017-10-28 12:44:44 +0200
commit0926d263902c184bd4f4c2036cb8ee90f81c5060 (patch)
treef326e180aee99881725e5e2abf5a4c13e323e964 /tests
parent88d38cb290aad669ad1406e2362d85c81e46d4f6 (diff)
parent9ec0a61156192484ca90a8dc88b7c23b26129755 (diff)
downloadShaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.tar.gz
Shaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.tar.zst
Shaarli-0926d263902c184bd4f4c2036cb8ee90f81c5060.zip
Merge pull request #962 from ArthurHoaro/feature/perfs2
Performances: reorder links when they're written instead of read
Diffstat (limited to 'tests')
-rw-r--r--tests/LinkFilterTest.php13
-rw-r--r--tests/utils/ReferenceLinkDB.php23
2 files changed, 35 insertions, 1 deletions
diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php
index d796d3a3..9cd6dbd4 100644
--- a/tests/LinkFilterTest.php
+++ b/tests/LinkFilterTest.php
@@ -8,6 +8,10 @@ require_once 'application/LinkFilter.php';
8class LinkFilterTest extends PHPUnit_Framework_TestCase 8class LinkFilterTest extends PHPUnit_Framework_TestCase
9{ 9{
10 /** 10 /**
11 * @var string Test datastore path.
12 */
13 protected static $testDatastore = 'sandbox/datastore.php';
14 /**
11 * @var LinkFilter instance. 15 * @var LinkFilter instance.
12 */ 16 */
13 protected static $linkFilter; 17 protected static $linkFilter;
@@ -18,12 +22,19 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
18 protected static $refDB; 22 protected static $refDB;
19 23
20 /** 24 /**
25 * @var LinkDB instance
26 */
27 protected static $linkDB;
28
29 /**
21 * Instanciate linkFilter with ReferenceLinkDB data. 30 * Instanciate linkFilter with ReferenceLinkDB data.
22 */ 31 */
23 public static function setUpBeforeClass() 32 public static function setUpBeforeClass()
24 { 33 {
25 self::$refDB = new ReferenceLinkDB(); 34 self::$refDB = new ReferenceLinkDB();
26 self::$linkFilter = new LinkFilter(self::$refDB->getLinks()); 35 self::$refDB->write(self::$testDatastore);
36 self::$linkDB = new LinkDB(self::$testDatastore, true, false);
37 self::$linkFilter = new LinkFilter(self::$linkDB);
27 } 38 }
28 39
29 /** 40 /**
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index f09eebc1..e887aa78 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -141,6 +141,7 @@ class ReferenceLinkDB
141 */ 141 */
142 public function write($filename) 142 public function write($filename)
143 { 143 {
144 $this->reorder();
144 file_put_contents( 145 file_put_contents(
145 $filename, 146 $filename,
146 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' 147 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
@@ -148,6 +149,27 @@ class ReferenceLinkDB
148 } 149 }
149 150
150 /** 151 /**
152 * Reorder links by creation date (newest first).
153 *
154 * Also update the urls and ids mapping arrays.
155 *
156 * @param string $order ASC|DESC
157 */
158 public function reorder($order = 'DESC')
159 {
160 // backward compatibility: ignore reorder if the the `created` field doesn't exist
161 if (! isset(array_values($this->_links)[0]['created'])) {
162 return;
163 }
164
165 $order = $order === 'ASC' ? -1 : 1;
166 // Reorder array by dates.
167 usort($this->_links, function($a, $b) use ($order) {
168 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
169 });
170 }
171
172 /**
151 * Returns the number of links in the reference data 173 * Returns the number of links in the reference data
152 */ 174 */
153 public function countLinks() 175 public function countLinks()
@@ -187,6 +209,7 @@ class ReferenceLinkDB
187 209
188 public function getLinks() 210 public function getLinks()
189 { 211 {
212 $this->reorder();
190 return $this->_links; 213 return $this->_links;
191 } 214 }
192 215