Skip to content

Commit

Permalink
fix(test): Fix existing test as good as possible
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Aug 13, 2024
1 parent b2d16da commit 2488577
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions tests/Album/AlbumMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use Test\TestCase;

/**
Expand All @@ -30,8 +34,16 @@ class AlbumMapperTest extends TestCase {
private $mimeLoader;
/** @var AlbumMapper */
private $mapper;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var ITimeFactory|MockObject */
private $timeFactory;
/** @var IUserManager|MockObject */
private $userManager;
/** @var IGroupManager|MockObject */
private $groupManager;
/** @var IL10N|MockObject */
private $l10n;
/** @var ISecureRandom|MockObject */
private $secureRandom;
private int $time = 100;

protected function setUp(): void {
Expand All @@ -41,11 +53,23 @@ protected function setUp(): void {
$this->connection = \OC::$server->get(IDBConnection::class);
$this->mimeLoader = \OC::$server->get(IMimeTypeLoader::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->l10n = $this->createMock(IL10N::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->timeFactory->method('getTime')->willReturnCallback(function () {
return $this->time;
});

$this->mapper = new AlbumMapper($this->connection, $this->mimeLoader, $this->timeFactory);
$this->mapper = new AlbumMapper(
$this->connection,
$this->mimeLoader,
$this->timeFactory,
$this->userManager,
$this->groupManager,
$this->l10n,
$this->secureRandom,
);
}

protected function tearDown():void {
Expand Down Expand Up @@ -152,6 +176,8 @@ public function testCreateUpdateGet() {
$this->assertEquals("nowhere", $retrievedAlbum->getLocation());
}

/**
* Disabled as the function does no longer exist
public function testEmptyFiles() {
$album1 = $this->mapper->create("user1", "album1");
Expand All @@ -165,9 +191,9 @@ public function testAddFiles() {
$fileId1 = $this->createFile("file1", "text/plain");
$fileId2 = $this->createFile("file2", "image/png");
$this->mapper->addFile($album1->getId(), $fileId1);
$this->mapper->addFile($album1->getId(), $fileId2);
$this->mapper->addFile($album2->getId(), $fileId1);
$this->mapper->addFile($album1->getId(), $fileId1, 'user1');
$this->mapper->addFile($album1->getId(), $fileId2, 'user1');
$this->mapper->addFile($album2->getId(), $fileId1, 'user1');
$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
Expand All @@ -182,8 +208,8 @@ public function testAddFiles() {
return $a->getFileId() <=> $b->getFileId();
});
$this->assertCount(2, $files);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 100), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId2, "file2", "image/png", 10, 10000, "dummy", 100), $albumsWithFiles[0]->getFiles()[1]);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 100, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId2, "file2", "image/png", 10, 10000, "dummy", 100, 'user1'), $albumsWithFiles[0]->getFiles()[1]);
$this->assertEquals($album2->getId(), $albumsWithFiles[1]->getAlbum()->getId());
$this->assertEquals($fileId1, $albumsWithFiles[1]->getAlbum()->getLastAddedPhoto());
Expand All @@ -193,7 +219,7 @@ public function testAddFiles() {
return $a->getFileId() <=> $b->getFileId();
});
$this->assertCount(1, $files);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 100), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 100, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
}
public function testAddRemoveFiles() {
Expand All @@ -204,11 +230,11 @@ public function testAddRemoveFiles() {
$fileId3 = $this->createFile("file3", "image/png");
$this->time = 110;
$this->mapper->addFile($album1->getId(), $fileId1);
$this->mapper->addFile($album1->getId(), $fileId1, 'user1');
$this->time = 120;
$this->mapper->addFile($album1->getId(), $fileId2);
$this->mapper->addFile($album1->getId(), $fileId2, 'user1');
$this->time = 130;
$this->mapper->addFile($album1->getId(), $fileId3);
$this->mapper->addFile($album1->getId(), $fileId3, 'user1');
$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
Expand All @@ -223,9 +249,9 @@ public function testAddRemoveFiles() {
return $a->getFileId() <=> $b->getFileId();
});
$this->assertCount(3, $files);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId2, "file2", "image/png", 10, 10000, "dummy", 120), $albumsWithFiles[0]->getFiles()[1]);
$this->assertEquals(new AlbumFile($fileId3, "file3", "image/png", 10, 10000, "dummy", 130), $albumsWithFiles[0]->getFiles()[2]);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId2, "file2", "image/png", 10, 10000, "dummy", 120, 'user1'), $albumsWithFiles[0]->getFiles()[1]);
$this->assertEquals(new AlbumFile($fileId3, "file3", "image/png", 10, 10000, "dummy", 130, 'user1'), $albumsWithFiles[0]->getFiles()[2]);
Expand All @@ -244,8 +270,8 @@ public function testAddRemoveFiles() {
return $a->getFileId() <=> $b->getFileId();
});
$this->assertCount(2, $files);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId3, "file3", "image/png", 10, 10000, "dummy", 130), $albumsWithFiles[0]->getFiles()[1]);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId3, "file3", "image/png", 10, 10000, "dummy", 130, 'user1'), $albumsWithFiles[0]->getFiles()[1]);
Expand All @@ -264,7 +290,7 @@ public function testAddRemoveFiles() {
return $a->getFileId() <=> $b->getFileId();
});
$this->assertCount(1, $files);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110), $albumsWithFiles[0]->getFiles()[0]);
$this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
Expand All @@ -281,4 +307,5 @@ public function testAddRemoveFiles() {
$files = $albumsWithFiles[0]->getFiles();
$this->assertCount(0, $files);
}
*/
}

0 comments on commit 2488577

Please sign in to comment.