namespace Shaarli\Bookmark;
use DateTime;
-use PHPUnit\Framework\TestCase;
+use malkusch\lock\mutex\NoMutex;
use ReferenceLinkDB;
use ReflectionClass;
use Shaarli;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\BookmarkMarkdownFormatter;
use Shaarli\History;
+use Shaarli\TestCase;
/**
* Unitary tests for LegacyLinkDBTest
*/
protected $privateLinkDB = null;
+ /** @var NoMutex */
+ protected $mutex;
+
/**
* Instantiates public and private LinkDBs with test data
*
*
* Resets test data for each test
*/
- protected function setUp()
+ protected function setUp(): void
{
+ $this->mutex = new NoMutex();
+
if (file_exists(self::$testDatastore)) {
unlink(self::$testDatastore);
}
$this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore);
$this->history = new History('sandbox/history.php');
- $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false);
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
}
/**
$db = self::getMethod('migrate');
$db->invokeArgs($this->privateLinkDB, []);
- $db = new \FakeBookmarkService($this->conf, $this->history, true);
+ $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, true);
$this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
$this->assertEquals($this->refDB->countLinks(), $db->count());
}
/**
* Test get() method for an undefined bookmark
- *
- * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testGetUndefined()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$this->privateLinkDB->get(666);
}
$this->assertEquals($updated, $bookmark->getUpdated());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId());
$this->assertNull($bookmark->getUpdated());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId());
/**
* Test add() method for a bookmark without any field set and without writing the data store
- *
- * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testAddMinimalNoWrite()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$bookmark = new Bookmark();
- $this->privateLinkDB->add($bookmark);
+ $this->privateLinkDB->add($bookmark, false);
$bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$this->privateLinkDB->get(43);
}
/**
* Test add() method while logged out
- *
- * @expectedException \Exception
- * @expectedExceptionMessage You're not authorized to alter the datastore
*/
public function testAddLoggedOut()
{
- $this->publicLinkDB->add(new Bookmark());
- }
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
- /**
- * Test add() method with an entry which is not a bookmark instance
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Provided data is invalid
- */
- public function testAddNotABookmark()
- {
- $this->privateLinkDB->add(['title' => 'hi!']);
+ $this->publicLinkDB->add(new Bookmark());
}
/**
* Test add() method with a Bookmark already containing an ID
- *
- * @expectedException \Exception
- * @expectedExceptionMessage This bookmarks already exists
*/
public function testAddWithId()
{
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('This bookmarks already exists');
+
$bookmark = new Bookmark();
$bookmark->setId(43);
$this->privateLinkDB->add($bookmark);
$this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(42);
$this->assertEquals(42, $bookmark->getId());
$this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(42);
$this->assertEquals(42, $bookmark->getId());
$this->assertEquals($title, $bookmark->getTitle());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(42);
$this->assertEquals(42, $bookmark->getId());
/**
* Test set() method while logged out
- *
- * @expectedException \Exception
- * @expectedExceptionMessage You're not authorized to alter the datastore
*/
public function testSetLoggedOut()
{
- $this->publicLinkDB->set(new Bookmark());
- }
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
- /**
- * Test set() method with an entry which is not a bookmark instance
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Provided data is invalid
- */
- public function testSetNotABookmark()
- {
- $this->privateLinkDB->set(['title' => 'hi!']);
+ $this->publicLinkDB->set(new Bookmark());
}
/**
* Test set() method with a Bookmark without an ID defined.
- *
- * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testSetWithoutId()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$bookmark = new Bookmark();
$this->privateLinkDB->set($bookmark);
}
/**
* Test set() method with a Bookmark with an unknow ID
- *
- * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testSetWithUnknownId()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$bookmark = new Bookmark();
$bookmark->setId(666);
$this->privateLinkDB->set($bookmark);
$this->assertEquals(43, $bookmark->getId());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(43);
$this->assertEquals(43, $bookmark->getId());
$this->assertEquals($title, $bookmark->getTitle());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(42);
$this->assertEquals(42, $bookmark->getId());
/**
* Test addOrSet() method while logged out
- *
- * @expectedException \Exception
- * @expectedExceptionMessage You're not authorized to alter the datastore
*/
public function testAddOrSetLoggedOut()
{
- $this->publicLinkDB->addOrSet(new Bookmark());
- }
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
- /**
- * Test addOrSet() method with an entry which is not a bookmark instance
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Provided data is invalid
- */
- public function testAddOrSetNotABookmark()
- {
- $this->privateLinkDB->addOrSet(['title' => 'hi!']);
+ $this->publicLinkDB->addOrSet(new Bookmark());
}
/**
$this->assertEquals($title, $bookmark->getTitle());
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$bookmark = $this->privateLinkDB->get(42);
$this->assertEquals(42, $bookmark->getId());
/**
* Test remove() method with an existing Bookmark
- *
- * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testRemoveExisting()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$bookmark = $this->privateLinkDB->get(42);
$this->privateLinkDB->remove($bookmark);
$this->assertInstanceOf(BookmarkNotFoundException::class, $exception);
// reload from file
- $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
+ $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$this->privateLinkDB->get(42);
}
/**
* Test remove() method while logged out
- *
- * @expectedException \Exception
- * @expectedExceptionMessage You're not authorized to alter the datastore
*/
public function testRemoveLoggedOut()
{
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
+
$bookmark = $this->privateLinkDB->get(42);
$this->publicLinkDB->remove($bookmark);
}
- /**
- * Test remove() method with an entry which is not a bookmark instance
- *
- * @expectedException \Exception
- * @expectedExceptionMessage Provided data is invalid
- */
- public function testRemoveNotABookmark()
- {
- $this->privateLinkDB->remove(['title' => 'hi!']);
- }
-
/**
* Test remove() method with a Bookmark with an unknown ID
- *
- * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
*/
public function testRemoveWithUnknownId()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
+
$bookmark = new Bookmark();
$bookmark->setId(666);
$this->privateLinkDB->remove($bookmark);
{
$dbSize = $this->privateLinkDB->count();
$this->privateLinkDB->initialize();
- $this->assertEquals($dbSize + 2, $this->privateLinkDB->count());
- $this->assertEquals(
- 'My secret stuff... - Pastebin.com',
- $this->privateLinkDB->get(43)->getTitle()
+ $this->assertEquals($dbSize + 3, $this->privateLinkDB->count());
+ $this->assertStringStartsWith(
+ 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
+ $this->privateLinkDB->get(43)->getDescription()
);
- $this->assertEquals(
- 'The personal, minimalist, super-fast, database free, bookmarking service',
- $this->privateLinkDB->get(44)->getTitle()
+ $this->assertStringStartsWith(
+ 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
+ $this->privateLinkDB->get(44)->getDescription()
+ );
+ $this->assertStringStartsWith(
+ 'Welcome to Shaarli!',
+ $this->privateLinkDB->get(45)->getDescription()
);
}
* to make sure that nothing have been broken in the migration process.
* They mostly cover search/filters. Some of them might be redundant with the previous ones.
*/
-
/**
* Attempt to instantiate a LinkDB whereas the datastore is not writable
- *
- * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException
- * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*#
*/
public function testConstructDatastoreNotWriteable()
{
+ $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException::class);
+ $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
+
$conf = new ConfigManager('tests/utils/config/configJson');
$conf->set('resource.datastore', 'null/store.db');
- new BookmarkFileService($conf, $this->history, true);
+ new BookmarkFileService($conf, $this->history, $this->mutex, true);
}
/**
{
unlink(self::$testDatastore);
$this->assertFileNotExists(self::$testDatastore);
- new BookmarkFileService($this->conf, $this->history, true);
+ new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$this->assertFileExists(self::$testDatastore);
// ensure the correct data has been written
{
unlink(self::$testDatastore);
$this->assertFileNotExists(self::$testDatastore);
- $db = new \FakeBookmarkService($this->conf, $this->history, false);
+ $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, false);
$this->assertFileNotExists(self::$testDatastore);
$this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
$this->assertCount(0, $db->getBookmarks());
*/
public function testSave()
{
- $testDB = new BookmarkFileService($this->conf, $this->history, true);
+ $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$dbSize = $testDB->count();
$bookmark = new Bookmark();
$testDB->add($bookmark);
- $testDB = new BookmarkFileService($this->conf, $this->history, true);
+ $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
$this->assertEquals($dbSize + 1, $testDB->count());
}
public function testCountHiddenPublic()
{
$this->conf->set('privacy.hide_public_links', true);
- $linkDB = new BookmarkFileService($this->conf, $this->history, false);
+ $linkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
$this->assertEquals(0, $linkDB->count());
}
*/
public function testDays()
{
- $this->assertEquals(
+ $this->assertSame(
['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
$this->publicLinkDB->days()
);
- $this->assertEquals(
+ $this->assertSame(
['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
$this->privateLinkDB->days()
);
$link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
$this->assertNotEquals(false, $link);
- $this->assertContains(
+ $this->assertContainsPolyfill(
'A free software media publishing platform',
$link->getDescription()
);