aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkInitializerTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-17 21:34:12 +0100
committerArthurHoaro <arthur@hoa.ro>2020-01-18 09:56:32 +0100
commite26e2060f5470ce8bf4c5973284bae07b8af170a (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/bookmark/BookmarkInitializerTest.php
parentcf92b4dd1521241eefc58eaf6dcd202cd83969d8 (diff)
downloadShaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.gz
Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.zst
Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.zip
Add and update unit test for the new system (Bookmark + Service)
See #1307
Diffstat (limited to 'tests/bookmark/BookmarkInitializerTest.php')
-rw-r--r--tests/bookmark/BookmarkInitializerTest.php120
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/bookmark/BookmarkInitializerTest.php b/tests/bookmark/BookmarkInitializerTest.php
new file mode 100644
index 00000000..d23eb069
--- /dev/null
+++ b/tests/bookmark/BookmarkInitializerTest.php
@@ -0,0 +1,120 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use PHPUnit\Framework\TestCase;
6use ReferenceLinkDB;
7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
9
10/**
11 * Class BookmarkInitializerTest
12 * @package Shaarli\Bookmark
13 */
14class BookmarkInitializerTest extends TestCase
15{
16 /** @var string Path of test data store */
17 protected static $testDatastore = 'sandbox/datastore.php';
18
19 /** @var string Path of test config file */
20 protected static $testConf = 'sandbox/config';
21
22 /**
23 * @var ConfigManager instance.
24 */
25 protected $conf;
26
27 /**
28 * @var History instance.
29 */
30 protected $history;
31
32 /** @var BookmarkServiceInterface instance */
33 protected $bookmarkService;
34
35 /** @var BookmarkInitializer instance */
36 protected $initializer;
37
38 /**
39 * Initialize an empty BookmarkFileService
40 */
41 public function setUp()
42 {
43 if (file_exists(self::$testDatastore)) {
44 unlink(self::$testDatastore);
45 }
46
47 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
48 $this->conf = new ConfigManager(self::$testConf);
49 $this->conf->set('resource.datastore', self::$testDatastore);
50 $this->history = new History('sandbox/history.php');
51 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
52
53 $this->initializer = new BookmarkInitializer($this->bookmarkService);
54 }
55
56 /**
57 * Test initialize() with an empty data store.
58 */
59 public function testInitializeEmptyDataStore()
60 {
61 $refDB = new \ReferenceLinkDB();
62 $refDB->write(self::$testDatastore);
63 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
64 $this->initializer = new BookmarkInitializer($this->bookmarkService);
65
66 $this->initializer->initialize();
67
68 $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
69 $bookmark = $this->bookmarkService->get(43);
70 $this->assertEquals(43, $bookmark->getId());
71 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
72 $this->assertTrue($bookmark->isPrivate());
73
74 $bookmark = $this->bookmarkService->get(44);
75 $this->assertEquals(44, $bookmark->getId());
76 $this->assertEquals(
77 'The personal, minimalist, super-fast, database free, bookmarking service',
78 $bookmark->getTitle()
79 );
80 $this->assertFalse($bookmark->isPrivate());
81
82 // Reload from file
83 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
84 $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
85 $bookmark = $this->bookmarkService->get(43);
86 $this->assertEquals(43, $bookmark->getId());
87 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
88 $this->assertTrue($bookmark->isPrivate());
89
90 $bookmark = $this->bookmarkService->get(44);
91 $this->assertEquals(44, $bookmark->getId());
92 $this->assertEquals(
93 'The personal, minimalist, super-fast, database free, bookmarking service',
94 $bookmark->getTitle()
95 );
96 $this->assertFalse($bookmark->isPrivate());
97 }
98
99 /**
100 * Test initialize() with a data store containing bookmarks.
101 */
102 public function testInitializeNotEmptyDataStore()
103 {
104 $this->initializer->initialize();
105
106 $this->assertEquals(2, $this->bookmarkService->count());
107 $bookmark = $this->bookmarkService->get(0);
108 $this->assertEquals(0, $bookmark->getId());
109 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
110 $this->assertTrue($bookmark->isPrivate());
111
112 $bookmark = $this->bookmarkService->get(1);
113 $this->assertEquals(1, $bookmark->getId());
114 $this->assertEquals(
115 'The personal, minimalist, super-fast, database free, bookmarking service',
116 $bookmark->getTitle()
117 );
118 $this->assertFalse($bookmark->isPrivate());
119 }
120}