From dcacf51b695168fad22b2fece32f9f703e47b507 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 13 Aug 2024 09:53:59 +0200 Subject: [PATCH] fix(test): Fix existing test as good as possible Signed-off-by: Joas Schilling --- tests/Album/AlbumMapperTest.php | 289 +++++++++++++++++--------------- 1 file changed, 158 insertions(+), 131 deletions(-) diff --git a/tests/Album/AlbumMapperTest.php b/tests/Album/AlbumMapperTest.php index 05dc5515d..99682e356 100644 --- a/tests/Album/AlbumMapperTest.php +++ b/tests/Album/AlbumMapperTest.php @@ -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; /** @@ -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 { @@ -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 { @@ -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); + * } + */ }