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 7e84ac5 commit dcacf51
Showing 1 changed file with 158 additions and 131 deletions.
289 changes: 158 additions & 131 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,133 +176,136 @@ public function testCreateUpdateGet() {
$this->assertEquals("nowhere", $retrievedAlbum->getLocation());
}

public function testEmptyFiles() {
$album1 = $this->mapper->create("user1", "album1");

$this->assertEquals([new AlbumWithFiles($album1, [])], $this->mapper->getForUserWithFiles("user1"));
}

public function testAddFiles() {
$album1 = $this->mapper->create("user1", "album1");
$album2 = $this->mapper->create("user1", "album2");

$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);

$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
});
$this->assertCount(2, $albumsWithFiles);

$this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
$this->assertEquals($fileId2, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
$files = $albumsWithFiles[0]->getFiles();
usort($files, function (AlbumFile $a, AlbumFile $b) {
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($album2->getId(), $albumsWithFiles[1]->getAlbum()->getId());
$this->assertEquals($fileId1, $albumsWithFiles[1]->getAlbum()->getLastAddedPhoto());

$files = $albumsWithFiles[1]->getFiles();
usort($files, function (AlbumFile $a, AlbumFile $b) {
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]);
}

public function testAddRemoveFiles() {
$album1 = $this->mapper->create("user1", "album1");

$fileId1 = $this->createFile("file1", "text/plain");
$fileId2 = $this->createFile("file2", "image/png");
$fileId3 = $this->createFile("file3", "image/png");

$this->time = 110;
$this->mapper->addFile($album1->getId(), $fileId1);
$this->time = 120;
$this->mapper->addFile($album1->getId(), $fileId2);
$this->time = 130;
$this->mapper->addFile($album1->getId(), $fileId3);

$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
});
$this->assertCount(1, $albumsWithFiles);

$this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
$this->assertEquals($fileId3, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
$files = $albumsWithFiles[0]->getFiles();
usort($files, function (AlbumFile $a, AlbumFile $b) {
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->mapper->removeFile($album1->getId(), $fileId2);

$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
});
$this->assertCount(1, $albumsWithFiles);

$this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
$this->assertEquals($fileId3, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
$files = $albumsWithFiles[0]->getFiles();
usort($files, function (AlbumFile $a, AlbumFile $b) {
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->mapper->removeFile($album1->getId(), $fileId3);

$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
});
$this->assertCount(1, $albumsWithFiles);

$this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
$this->assertEquals($fileId1, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
$files = $albumsWithFiles[0]->getFiles();
usort($files, function (AlbumFile $a, AlbumFile $b) {
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->mapper->removeFile($album1->getId(), $fileId1);

$albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
});
$this->assertCount(1, $albumsWithFiles);

$this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
$this->assertEquals(-1, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
$files = $albumsWithFiles[0]->getFiles();
$this->assertCount(0, $files);
}
/**
* Disabled as the function does no longer exist
* public function testEmptyFiles() {
* $album1 = $this->mapper->create("user1", "album1");
*
* $this->assertEquals([new AlbumWithFiles($album1, [])], $this->mapper->getForUserWithFiles("user1"));
* }
*
* public function testAddFiles() {
* $album1 = $this->mapper->create("user1", "album1");
* $album2 = $this->mapper->create("user1", "album2");
*
* $fileId1 = $this->createFile("file1", "text/plain");
* $fileId2 = $this->createFile("file2", "image/png");
*
* $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) {
* return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
* });
* $this->assertCount(2, $albumsWithFiles);
*
* $this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
* $this->assertEquals($fileId2, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
* $files = $albumsWithFiles[0]->getFiles();
* usort($files, function (AlbumFile $a, AlbumFile $b) {
* return $a->getFileId() <=> $b->getFileId();
* });
* $this->assertCount(2, $files);
* $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());
*
* $files = $albumsWithFiles[1]->getFiles();
* usort($files, function (AlbumFile $a, AlbumFile $b) {
* return $a->getFileId() <=> $b->getFileId();
* });
* $this->assertCount(1, $files);
* $this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 100, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
* }
*
* public function testAddRemoveFiles() {
* $album1 = $this->mapper->create("user1", "album1");
*
* $fileId1 = $this->createFile("file1", "text/plain");
* $fileId2 = $this->createFile("file2", "image/png");
* $fileId3 = $this->createFile("file3", "image/png");
*
* $this->time = 110;
* $this->mapper->addFile($album1->getId(), $fileId1, 'user1');
* $this->time = 120;
* $this->mapper->addFile($album1->getId(), $fileId2, 'user1');
* $this->time = 130;
* $this->mapper->addFile($album1->getId(), $fileId3, 'user1');
*
* $albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
* usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
* return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
* });
* $this->assertCount(1, $albumsWithFiles);
*
* $this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
* $this->assertEquals($fileId3, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
* $files = $albumsWithFiles[0]->getFiles();
* usort($files, function (AlbumFile $a, AlbumFile $b) {
* return $a->getFileId() <=> $b->getFileId();
* });
* $this->assertCount(3, $files);
* $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]);
*
*
*
* $this->mapper->removeFile($album1->getId(), $fileId2);
*
* $albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
* usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
* return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
* });
* $this->assertCount(1, $albumsWithFiles);
*
* $this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
* $this->assertEquals($fileId3, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
* $files = $albumsWithFiles[0]->getFiles();
* usort($files, function (AlbumFile $a, AlbumFile $b) {
* return $a->getFileId() <=> $b->getFileId();
* });
* $this->assertCount(2, $files);
* $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]);
*
*
*
* $this->mapper->removeFile($album1->getId(), $fileId3);
*
* $albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
* usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
* return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
* });
* $this->assertCount(1, $albumsWithFiles);
*
* $this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
* $this->assertEquals($fileId1, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
* $files = $albumsWithFiles[0]->getFiles();
* usort($files, function (AlbumFile $a, AlbumFile $b) {
* return $a->getFileId() <=> $b->getFileId();
* });
* $this->assertCount(1, $files);
* $this->assertEquals(new AlbumFile($fileId1, "file1", "text/plain", 10, 10000, "dummy", 110, 'user1'), $albumsWithFiles[0]->getFiles()[0]);
*
*
*
* $this->mapper->removeFile($album1->getId(), $fileId1);
*
* $albumsWithFiles = $this->mapper->getForUserWithFiles("user1");
* usort($albumsWithFiles, function (AlbumWithFiles $a, AlbumWithFiles $b) {
* return $a->getAlbum()->getId() <=> $b->getAlbum()->getId();
* });
* $this->assertCount(1, $albumsWithFiles);
*
* $this->assertEquals($album1->getId(), $albumsWithFiles[0]->getAlbum()->getId());
* $this->assertEquals(-1, $albumsWithFiles[0]->getAlbum()->getLastAddedPhoto());
* $files = $albumsWithFiles[0]->getFiles();
* $this->assertCount(0, $files);
* }
*/
}

0 comments on commit dcacf51

Please sign in to comment.