3 namespace Shaarli\Bookmark
;
5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Config\ConfigManager
;
10 * Class BookmarkInitializerTest
11 * @package Shaarli\Bookmark
13 class BookmarkInitializerTest
extends TestCase
15 /** @var string Path of test data store */
16 protected static $testDatastore = 'sandbox/datastore.php';
18 /** @var string Path of test config file */
19 protected static $testConf = 'sandbox/config';
22 * @var ConfigManager instance.
27 * @var History instance.
31 /** @var BookmarkServiceInterface instance */
32 protected $bookmarkService;
34 /** @var BookmarkInitializer instance */
35 protected $initializer;
38 * Initialize an empty BookmarkFileService
40 public function setUp()
42 if (file_exists(self
::$testDatastore)) {
43 unlink(self
::$testDatastore);
46 copy('tests/utils/config/configJson.json.php', self
::$testConf .'.json.php');
47 $this->conf
= new ConfigManager(self
::$testConf);
48 $this->conf
->set('resource.datastore', self
::$testDatastore);
49 $this->history
= new History('sandbox/history.php');
50 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
52 $this->initializer
= new BookmarkInitializer($this->bookmarkService
);
56 * Test initialize() with a data store containing bookmarks.
58 public function testInitializeNotEmptyDataStore(): void
60 $refDB = new \
ReferenceLinkDB();
61 $refDB->write(self
::$testDatastore);
62 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
63 $this->initializer
= new BookmarkInitializer($this->bookmarkService
);
65 $this->initializer
->initialize();
67 $this->assertEquals($refDB->countLinks() +
2, $this->bookmarkService
->count());
68 $bookmark = $this->bookmarkService
->get(43);
69 $this->assertEquals(43, $bookmark->getId());
70 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
71 $this->assertTrue($bookmark->isPrivate());
73 $bookmark = $this->bookmarkService
->get(44);
74 $this->assertEquals(44, $bookmark->getId());
76 'The personal, minimalist, super-fast, database free, bookmarking service',
79 $this->assertFalse($bookmark->isPrivate());
81 $this->bookmarkService
->save();
84 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
85 $this->assertEquals($refDB->countLinks() +
2, $this->bookmarkService
->count());
86 $bookmark = $this->bookmarkService
->get(43);
87 $this->assertEquals(43, $bookmark->getId());
88 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
89 $this->assertTrue($bookmark->isPrivate());
91 $bookmark = $this->bookmarkService
->get(44);
92 $this->assertEquals(44, $bookmark->getId());
94 'The personal, minimalist, super-fast, database free, bookmarking service',
97 $this->assertFalse($bookmark->isPrivate());
101 * Test initialize() with an a non existent datastore file .
103 public function testInitializeNonExistentDataStore(): void
105 $this->conf
->set('resource.datastore', static::$testDatastore . '_empty');
106 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
108 $this->initializer
->initialize();
110 $this->assertEquals(2, $this->bookmarkService
->count());
111 $bookmark = $this->bookmarkService
->get(0);
112 $this->assertEquals(0, $bookmark->getId());
113 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
114 $this->assertTrue($bookmark->isPrivate());
116 $bookmark = $this->bookmarkService
->get(1);
117 $this->assertEquals(1, $bookmark->getId());
119 'The personal, minimalist, super-fast, database free, bookmarking service',
120 $bookmark->getTitle()
122 $this->assertFalse($bookmark->isPrivate());