aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/ApiUtilsTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-18 10:01:06 +0100
committerGitHub <noreply@github.com>2020-01-18 10:01:06 +0100
commit3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/api/ApiUtilsTest.php
parent796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff)
parente26e2060f5470ce8bf4c5973284bae07b8af170a (diff)
downloadShaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip
Store bookmarks as PHP objects and add a service layer to retri… (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'tests/api/ApiUtilsTest.php')
-rw-r--r--tests/api/ApiUtilsTest.php67
1 files changed, 35 insertions, 32 deletions
diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php
index 7499dd71..7efec9bb 100644
--- a/tests/api/ApiUtilsTest.php
+++ b/tests/api/ApiUtilsTest.php
@@ -2,6 +2,7 @@
2 2
3namespace Shaarli\Api; 3namespace Shaarli\Api;
4 4
5use Shaarli\Bookmark\Bookmark;
5use Shaarli\Http\Base64Url; 6use Shaarli\Http\Base64Url;
6 7
7/** 8/**
@@ -212,7 +213,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
212 public function testFormatLinkComplete() 213 public function testFormatLinkComplete()
213 { 214 {
214 $indexUrl = 'https://domain.tld/sub/'; 215 $indexUrl = 'https://domain.tld/sub/';
215 $link = [ 216 $data = [
216 'id' => 12, 217 'id' => 12,
217 'url' => 'http://lol.lol', 218 'url' => 'http://lol.lol',
218 'shorturl' => 'abc', 219 'shorturl' => 'abc',
@@ -223,6 +224,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
223 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 224 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
224 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'), 225 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'),
225 ]; 226 ];
227 $bookmark = new Bookmark();
228 $bookmark->fromArray($data);
226 229
227 $expected = [ 230 $expected = [
228 'id' => 12, 231 'id' => 12,
@@ -236,7 +239,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
236 'updated' => '2017-01-07T16:06:12+00:00', 239 'updated' => '2017-01-07T16:06:12+00:00',
237 ]; 240 ];
238 241
239 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 242 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
240 } 243 }
241 244
242 /** 245 /**
@@ -245,7 +248,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
245 public function testFormatLinkMinimalNote() 248 public function testFormatLinkMinimalNote()
246 { 249 {
247 $indexUrl = 'https://domain.tld/sub/'; 250 $indexUrl = 'https://domain.tld/sub/';
248 $link = [ 251 $data = [
249 'id' => 12, 252 'id' => 12,
250 'url' => '?abc', 253 'url' => '?abc',
251 'shorturl' => 'abc', 254 'shorturl' => 'abc',
@@ -255,6 +258,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
255 'private' => '', 258 'private' => '',
256 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 259 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
257 ]; 260 ];
261 $bookmark = new Bookmark();
262 $bookmark->fromArray($data);
258 263
259 $expected = [ 264 $expected = [
260 'id' => 12, 265 'id' => 12,
@@ -268,7 +273,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
268 'updated' => '', 273 'updated' => '',
269 ]; 274 ];
270 275
271 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 276 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
272 } 277 }
273 278
274 /** 279 /**
@@ -277,7 +282,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
277 public function testUpdateLink() 282 public function testUpdateLink()
278 { 283 {
279 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 284 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
280 $old = [ 285 $data = [
281 'id' => 12, 286 'id' => 12,
282 'url' => '?abc', 287 'url' => '?abc',
283 'shorturl' => 'abc', 288 'shorturl' => 'abc',
@@ -287,8 +292,10 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
287 'private' => '', 292 'private' => '',
288 'created' => $created, 293 'created' => $created,
289 ]; 294 ];
295 $old = new Bookmark();
296 $old->fromArray($data);
290 297
291 $new = [ 298 $data = [
292 'id' => 13, 299 'id' => 13,
293 'shorturl' => 'nope', 300 'shorturl' => 'nope',
294 'url' => 'http://somewhere.else', 301 'url' => 'http://somewhere.else',
@@ -299,17 +306,18 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
299 'created' => 'creation', 306 'created' => 'creation',
300 'updated' => 'updation', 307 'updated' => 'updation',
301 ]; 308 ];
309 $new = new Bookmark();
310 $new->fromArray($data);
302 311
303 $result = ApiUtils::updateLink($old, $new); 312 $result = ApiUtils::updateLink($old, $new);
304 $this->assertEquals(12, $result['id']); 313 $this->assertEquals(12, $result->getId());
305 $this->assertEquals('http://somewhere.else', $result['url']); 314 $this->assertEquals('http://somewhere.else', $result->getUrl());
306 $this->assertEquals('abc', $result['shorturl']); 315 $this->assertEquals('abc', $result->getShortUrl());
307 $this->assertEquals('Le Cid', $result['title']); 316 $this->assertEquals('Le Cid', $result->getTitle());
308 $this->assertEquals('Percé jusques au fond du cœur [...]', $result['description']); 317 $this->assertEquals('Percé jusques au fond du cœur [...]', $result->getDescription());
309 $this->assertEquals('corneille rodrigue', $result['tags']); 318 $this->assertEquals('corneille rodrigue', $result->getTagsString());
310 $this->assertEquals(true, $result['private']); 319 $this->assertEquals(true, $result->isPrivate());
311 $this->assertEquals($created, $result['created']); 320 $this->assertEquals($created, $result->getCreated());
312 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
313 } 321 }
314 322
315 /** 323 /**
@@ -318,7 +326,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
318 public function testUpdateLinkMinimal() 326 public function testUpdateLinkMinimal()
319 { 327 {
320 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 328 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
321 $old = [ 329 $data = [
322 'id' => 12, 330 'id' => 12,
323 'url' => '?abc', 331 'url' => '?abc',
324 'shorturl' => 'abc', 332 'shorturl' => 'abc',
@@ -328,24 +336,19 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
328 'private' => true, 336 'private' => true,
329 'created' => $created, 337 'created' => $created,
330 ]; 338 ];
339 $old = new Bookmark();
340 $old->fromArray($data);
331 341
332 $new = [ 342 $new = new Bookmark();
333 'url' => '',
334 'title' => '',
335 'description' => '',
336 'tags' => '',
337 'private' => false,
338 ];
339 343
340 $result = ApiUtils::updateLink($old, $new); 344 $result = ApiUtils::updateLink($old, $new);
341 $this->assertEquals(12, $result['id']); 345 $this->assertEquals(12, $result->getId());
342 $this->assertEquals('?abc', $result['url']); 346 $this->assertEquals('', $result->getUrl());
343 $this->assertEquals('abc', $result['shorturl']); 347 $this->assertEquals('abc', $result->getShortUrl());
344 $this->assertEquals('?abc', $result['title']); 348 $this->assertEquals('', $result->getTitle());
345 $this->assertEquals('', $result['description']); 349 $this->assertEquals('', $result->getDescription());
346 $this->assertEquals('', $result['tags']); 350 $this->assertEquals('', $result->getTagsString());
347 $this->assertEquals(false, $result['private']); 351 $this->assertEquals(false, $result->isPrivate());
348 $this->assertEquals($created, $result['created']); 352 $this->assertEquals($created, $result->getCreated());
349 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
350 } 353 }
351} 354}