From c5cd7ef9b9d1a5e439144b845164c0594ce0bef5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 13 May 2025 08:48:58 +0200 Subject: [PATCH] test: Fix tests/lib/[H-N]* Signed-off-by: Joas Schilling --- tests/lib/HelperStorageTest.php | 4 +- .../ExcludeFileByNameFilterIteratorTest.php | 8 +- tests/lib/Mail/MailerTest.php | 6 +- tests/lib/Mail/MessageTest.php | 5 +- tests/lib/MemoryInfoTest.php | 17 +--- tests/lib/Migration/BackgroundRepairTest.php | 13 +-- tests/lib/Notification/ActionTest.php | 14 +-- tests/lib/Notification/ManagerTest.php | 10 +-- tests/lib/Notification/NotificationTest.php | 86 +++++++++---------- 9 files changed, 72 insertions(+), 91 deletions(-) diff --git a/tests/lib/HelperStorageTest.php b/tests/lib/HelperStorageTest.php index 455bd288351..e4e877ab2a8 100644 --- a/tests/lib/HelperStorageTest.php +++ b/tests/lib/HelperStorageTest.php @@ -71,13 +71,13 @@ class HelperStorageTest extends \Test\TestCase { */ private function getStorageMock($freeSpace = 12) { $this->storageMock = $this->getMockBuilder(Temporary::class) - ->setMethods(['free_space']) + ->onlyMethods(['free_space']) ->setConstructorArgs([[]]) ->getMock(); $this->storageMock->expects($this->once()) ->method('free_space') - ->willReturn(12); + ->willReturn($freeSpace); return $this->storageMock; } diff --git a/tests/lib/IntegrityCheck/Iterator/ExcludeFileByNameFilterIteratorTest.php b/tests/lib/IntegrityCheck/Iterator/ExcludeFileByNameFilterIteratorTest.php index 14cc8851178..1d0b3e4f24d 100644 --- a/tests/lib/IntegrityCheck/Iterator/ExcludeFileByNameFilterIteratorTest.php +++ b/tests/lib/IntegrityCheck/Iterator/ExcludeFileByNameFilterIteratorTest.php @@ -17,11 +17,11 @@ class ExcludeFileByNameFilterIteratorTest extends TestCase { parent::setUp(); $this->filter = $this->getMockBuilder(ExcludeFileByNameFilterIterator::class) ->disableOriginalConstructor() - ->setMethods(['current']) + ->onlyMethods(['current']) ->getMock(); } - public function fileNameProvider(): array { + public static function fileNameProvider(): array { return [ ['a file', true], ['Thumbs.db', false], @@ -42,7 +42,7 @@ class ExcludeFileByNameFilterIteratorTest extends TestCase { public function testAcceptForFiles($fileName, $expectedResult): void { $iteratorMock = $this->getMockBuilder(\RecursiveDirectoryIterator::class) ->disableOriginalConstructor() - ->setMethods(['getFilename', 'isDir']) + ->onlyMethods(['getFilename', 'isDir']) ->getMock(); $iteratorMock->method('getFilename') @@ -64,7 +64,7 @@ class ExcludeFileByNameFilterIteratorTest extends TestCase { public function testAcceptForDirs($fileName, $expectedResult): void { $iteratorMock = $this->getMockBuilder(\RecursiveDirectoryIterator::class) ->disableOriginalConstructor() - ->setMethods(['getFilename', 'isDir']) + ->onlyMethods(['getFilename', 'isDir']) ->getMock(); $iteratorMock->method('getFilename') diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index 02a2605fc77..76a06e58c65 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -66,7 +66,7 @@ class MailerTest extends TestCase { /** * @return array */ - public function sendmailModeProvider(): array { + public static function sendmailModeProvider(): array { return [ 'smtp' => ['smtp', ' -bs'], 'pipe' => ['pipe', ' -t -i'], @@ -170,7 +170,7 @@ class MailerTest extends TestCase { ['mail_smtpport', 25, 25], ]); $this->mailer = $this->getMockBuilder(Mailer::class) - ->setMethods(['getInstance']) + ->onlyMethods(['getInstance']) ->setConstructorArgs( [ $this->config, @@ -227,7 +227,7 @@ class MailerTest extends TestCase { /** * @return array */ - public function mailAddressProvider() { + public static function mailAddressProvider(): array { return [ ['lukas@owncloud.com', true, false], ['lukas@localhost', true, false], diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index 260d3aaff76..79a12f3c355 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -27,7 +27,7 @@ class MessageTest extends TestCase { /** * @return array */ - public function mailAddressProvider() { + public static function mailAddressProvider(): array { return [ [ ['lukas@owncloud.com' => 'Lukas Reschke'], @@ -65,8 +65,7 @@ class MessageTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->symfonyEmail = $this->getMockBuilder(Email::class) - ->disableOriginalConstructor()->getMock(); + $this->symfonyEmail = $this->createMock(Email::class); $this->message = new Message($this->symfonyEmail, false); } diff --git a/tests/lib/MemoryInfoTest.php b/tests/lib/MemoryInfoTest.php index 2477de2d799..a81a5ebcaeb 100644 --- a/tests/lib/MemoryInfoTest.php +++ b/tests/lib/MemoryInfoTest.php @@ -37,12 +37,7 @@ class MemoryInfoTest extends TestCase { ini_set('memory_limit', $this->iniSettingBeforeTest); } - /** - * Provides test data. - * - * @return array - */ - public function getMemoryLimitTestData(): array { + public static function getMemoryLimitTestData(): array { return [ 'unlimited' => ['-1', -1,], '524288000 bytes' => ['524288000', 524288000,], @@ -65,12 +60,7 @@ class MemoryInfoTest extends TestCase { self::assertEquals($expected, $memoryInfo->getMemoryLimit()); } - /** - * Provides sufficient memory limit test data. - * - * @return array - */ - public function getSufficientMemoryTestData(): array { + public static function getSufficientMemoryTestData(): array { return [ 'unlimited' => [-1, true,], '512M' => [512 * 1024 * 1024, true,], @@ -85,12 +75,11 @@ class MemoryInfoTest extends TestCase { * @param int $memoryLimit The memory limit * @param bool $expected If the memory limit is sufficient. * @dataProvider getSufficientMemoryTestData - * @return void */ public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { /* @var MemoryInfo|MockObject $memoryInfo */ $memoryInfo = $this->getMockBuilder(MemoryInfo::class) - ->setMethods(['getMemoryLimit',]) + ->onlyMethods(['getMemoryLimit',]) ->getMock(); $memoryInfo diff --git a/tests/lib/Migration/BackgroundRepairTest.php b/tests/lib/Migration/BackgroundRepairTest.php index 25ea6088072..b85bd4262e0 100644 --- a/tests/lib/Migration/BackgroundRepairTest.php +++ b/tests/lib/Migration/BackgroundRepairTest.php @@ -54,22 +54,15 @@ class BackgroundRepairTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->jobList = $this->getMockBuilder(JobList::class) - ->disableOriginalConstructor() - ->getMock(); - $this->logger = $this->getMockBuilder(LoggerInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $this->jobList = $this->createMock(JobList::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->time = $this->createMock(ITimeFactory::class); $this->time->method('getTime') ->willReturn(999999); $this->appManager = $this->createMock(IAppManager::class); $this->repair = new Repair($this->dispatcher, $this->logger); - $this->job = $this->getMockBuilder(BackgroundRepair::class) - ->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList, $this->appManager]) - ->setMethods(['loadApp']) - ->getMock(); + $this->job = new BackgroundRepair($this->repair, $this->time, $this->logger, $this->jobList, $this->appManager); } public function testNoArguments(): void { diff --git a/tests/lib/Notification/ActionTest.php b/tests/lib/Notification/ActionTest.php index 3ae4b1db7e7..241e08048a0 100644 --- a/tests/lib/Notification/ActionTest.php +++ b/tests/lib/Notification/ActionTest.php @@ -20,7 +20,7 @@ class ActionTest extends TestCase { $this->action = new Action(); } - public function dataSetLabel() { + public static function dataSetLabel(): array { return [ ['test1'], [str_repeat('a', 1)], @@ -38,7 +38,7 @@ class ActionTest extends TestCase { $this->assertSame($label, $this->action->getLabel()); } - public function dataSetLabelInvalid() { + public static function dataSetLabelInvalid(): array { return [ [''], [str_repeat('a', 33)], @@ -56,7 +56,7 @@ class ActionTest extends TestCase { $this->action->setLabel($label); } - public function dataSetParsedLabel() { + public static function dataSetParsedLabel(): array { return [ ['test1'], [str_repeat('a', 1)], @@ -74,7 +74,7 @@ class ActionTest extends TestCase { $this->assertSame($label, $this->action->getParsedLabel()); } - public function dataSetParsedLabelInvalid() { + public static function dataSetParsedLabelInvalid(): array { return [ [''], ]; @@ -91,7 +91,7 @@ class ActionTest extends TestCase { $this->action->setParsedLabel($label); } - public function dataSetLink() { + public static function dataSetLink(): array { return [ ['test1', 'GET'], ['test2', 'POST'], @@ -112,7 +112,7 @@ class ActionTest extends TestCase { $this->assertSame($type, $this->action->getRequestType()); } - public function dataSetLinkInvalid() { + public static function dataSetLinkInvalid(): array { return [ // Invalid link ['', 'GET'], @@ -135,7 +135,7 @@ class ActionTest extends TestCase { $this->action->setLink($link, $type); } - public function dataSetPrimary() { + public static function dataSetPrimary(): array { return [ [true], [false], diff --git a/tests/lib/Notification/ManagerTest.php b/tests/lib/Notification/ManagerTest.php index 2a85316d7c4..418d642317e 100644 --- a/tests/lib/Notification/ManagerTest.php +++ b/tests/lib/Notification/ManagerTest.php @@ -153,7 +153,7 @@ class ManagerTest extends TestCase { $this->coordinator, $this->richTextFormatter, ]) - ->setMethods(['getApps']) + ->onlyMethods(['getApps']) ->getMock(); $manager->expects($this->once()) @@ -185,7 +185,7 @@ class ManagerTest extends TestCase { $this->coordinator, $this->richTextFormatter, ]) - ->setMethods(['getApps']) + ->onlyMethods(['getApps']) ->getMock(); $manager->expects($this->never()) @@ -210,7 +210,7 @@ class ManagerTest extends TestCase { $this->coordinator, $this->richTextFormatter, ]) - ->setMethods(['getApps']) + ->onlyMethods(['getApps']) ->getMock(); $manager->expects($this->once()) @@ -236,7 +236,7 @@ class ManagerTest extends TestCase { $this->coordinator, $this->richTextFormatter, ]) - ->setMethods(['getApps']) + ->onlyMethods(['getApps']) ->getMock(); $manager->expects($this->once()) @@ -246,7 +246,7 @@ class ManagerTest extends TestCase { $manager->getCount($notification); } - public function dataIsFairUseOfFreePushService(): array { + public static function dataIsFairUseOfFreePushService(): array { return [ [true, 999, true], [true, 1000, true], diff --git a/tests/lib/Notification/NotificationTest.php b/tests/lib/Notification/NotificationTest.php index 957d74233be..93b1746be3c 100644 --- a/tests/lib/Notification/NotificationTest.php +++ b/tests/lib/Notification/NotificationTest.php @@ -30,7 +30,7 @@ class NotificationTest extends TestCase { $this->notification = new Notification($this->validator, $this->richTextFormatter); } - protected function dataValidString($maxLength) { + protected static function dataValidString($maxLength): array { $dataSets = [ ['test1'], ['1564'], @@ -42,7 +42,7 @@ class NotificationTest extends TestCase { return $dataSets; } - protected function dataInvalidString($maxLength) { + protected static function dataInvalidString($maxLength): array { $dataSets = [ [''] ]; @@ -52,8 +52,8 @@ class NotificationTest extends TestCase { return $dataSets; } - public function dataSetApp() { - return $this->dataValidString(32); + public static function dataSetApp(): array { + return self::dataValidString(32); } /** @@ -66,8 +66,8 @@ class NotificationTest extends TestCase { $this->assertSame($app, $this->notification->getApp()); } - public function dataSetAppInvalid() { - return $this->dataInvalidString(32); + public static function dataSetAppInvalid(): array { + return self::dataInvalidString(32); } /** @@ -82,8 +82,8 @@ class NotificationTest extends TestCase { } - public function dataSetUser() { - return $this->dataValidString(64); + public static function dataSetUser(): array { + return self::dataValidString(64); } /** @@ -96,8 +96,8 @@ class NotificationTest extends TestCase { $this->assertSame($user, $this->notification->getUser()); } - public function dataSetUserInvalid() { - return $this->dataInvalidString(64); + public static function dataSetUserInvalid(): array { + return self::dataInvalidString(64); } /** @@ -111,7 +111,7 @@ class NotificationTest extends TestCase { $this->notification->setUser($user); } - public function dataSetDateTime() { + public static function dataSetDateTime(): array { $past = new \DateTime(); $past->sub(new \DateInterval('P1Y')); $current = new \DateTime(); @@ -135,7 +135,7 @@ class NotificationTest extends TestCase { $this->assertSame($dateTime, $this->notification->getDateTime()); } - public function dataSetDateTimeZero() { + public static function dataSetDateTimeZero(): array { $nineTeenSeventy = new \DateTime(); $nineTeenSeventy->setTimestamp(0); return [ @@ -155,7 +155,7 @@ class NotificationTest extends TestCase { $this->notification->setDateTime($dateTime); } - public function dataSetObject() { + public static function dataSetObject(): array { return [ ['a', '21'], [str_repeat('a', 64), '42'], @@ -175,11 +175,11 @@ class NotificationTest extends TestCase { $this->assertSame($id, $this->notification->getObjectId()); } - public function dataSetObjectTypeInvalid() { - return $this->dataInvalidString(64); + public static function dataSetObjectTypeInvalid(): array { + return self::dataInvalidString(64); } - public function dataSetObjectIdInvalid() { + public static function dataSetObjectIdInvalid(): array { return [ [''], [str_repeat('a', 64 + 1)], @@ -198,7 +198,7 @@ class NotificationTest extends TestCase { $this->notification->setObject('object', $id); } - public function dataSetSubject() { + public static function dataSetSubject(): array { return [ ['a', []], [str_repeat('a', 64), [str_repeat('a', 160)]], @@ -219,8 +219,8 @@ class NotificationTest extends TestCase { $this->assertSame($parameters, $this->notification->getSubjectParameters()); } - public function dataSetSubjectInvalidSubject() { - return $this->dataInvalidString(64); + public static function dataSetSubjectInvalidSubject(): array { + return self::dataInvalidString(64); } /** @@ -234,8 +234,8 @@ class NotificationTest extends TestCase { $this->notification->setSubject($subject, []); } - public function dataSetParsedSubject() { - return $this->dataValidString(false); + public static function dataSetParsedSubject(): array { + return self::dataValidString(false); } /** @@ -248,8 +248,8 @@ class NotificationTest extends TestCase { $this->assertSame($subject, $this->notification->getParsedSubject()); } - public function dataSetParsedSubjectInvalid() { - return $this->dataInvalidString(false); + public static function dataSetParsedSubjectInvalid(): array { + return self::dataInvalidString(false); } /** @@ -263,7 +263,7 @@ class NotificationTest extends TestCase { $this->notification->setParsedSubject($subject); } - public function dataSetMessage() { + public static function dataSetMessage(): array { return [ ['a', []], [str_repeat('a', 64), [str_repeat('a', 160)]], @@ -284,8 +284,8 @@ class NotificationTest extends TestCase { $this->assertSame($parameters, $this->notification->getMessageParameters()); } - public function dataSetMessageInvalidMessage() { - return $this->dataInvalidString(64); + public static function dataSetMessageInvalidMessage(): array { + return self::dataInvalidString(64); } /** @@ -299,8 +299,8 @@ class NotificationTest extends TestCase { $this->notification->setMessage($message, []); } - public function dataSetParsedMessage() { - return $this->dataValidString(false); + public static function dataSetParsedMessage(): array { + return self::dataValidString(false); } /** @@ -313,8 +313,8 @@ class NotificationTest extends TestCase { $this->assertSame($message, $this->notification->getParsedMessage()); } - public function dataSetParsedMessageInvalid() { - return $this->dataInvalidString(false); + public static function dataSetParsedMessageInvalid(): array { + return self::dataInvalidString(false); } /** @@ -328,8 +328,8 @@ class NotificationTest extends TestCase { $this->notification->setParsedMessage($message); } - public function dataSetLink() { - return $this->dataValidString(4000); + public static function dataSetLink(): array { + return self::dataValidString(4000); } /** @@ -342,8 +342,8 @@ class NotificationTest extends TestCase { $this->assertSame($link, $this->notification->getLink()); } - public function dataSetLinkInvalid() { - return $this->dataInvalidString(4000); + public static function dataSetLinkInvalid(): array { + return self::dataInvalidString(4000); } /** @@ -357,8 +357,8 @@ class NotificationTest extends TestCase { $this->notification->setLink($link); } - public function dataSetIcon() { - return $this->dataValidString(4000); + public static function dataSetIcon(): array { + return self::dataValidString(4000); } /** @@ -371,8 +371,8 @@ class NotificationTest extends TestCase { $this->assertSame($icon, $this->notification->getIcon()); } - public function dataSetIconInvalid() { - return $this->dataInvalidString(4000); + public static function dataSetIconInvalid(): array { + return self::dataInvalidString(4000); } /** @@ -508,7 +508,7 @@ class NotificationTest extends TestCase { $this->assertEquals([$action2, $action1, $action1], $this->notification->getParsedActions()); } - public function dataIsValid() { + public static function dataIsValid(): array { return [ [false, '', false], [true, '', false], @@ -527,7 +527,7 @@ class NotificationTest extends TestCase { public function testIsValid($isValidCommon, $subject, $expected): void { /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ $notification = $this->getMockBuilder(Notification::class) - ->setMethods([ + ->onlyMethods([ 'isValidCommon', 'getSubject', 'getParsedSubject', @@ -560,7 +560,7 @@ class NotificationTest extends TestCase { public function testIsParsedValid($isValidCommon, $subject, $expected): void { /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ $notification = $this->getMockBuilder(Notification::class) - ->setMethods([ + ->onlyMethods([ 'isValidCommon', 'getParsedSubject', 'getSubject', @@ -583,7 +583,7 @@ class NotificationTest extends TestCase { $this->assertEquals($expected, $notification->isValidParsed()); } - public function dataIsValidCommon() { + public static function dataIsValidCommon(): array { return [ ['', '', 0, '', '', false], ['app', '', 0, '', '', false], @@ -607,7 +607,7 @@ class NotificationTest extends TestCase { public function testIsValidCommon($app, $user, $timestamp, $objectType, $objectId, $expected): void { /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ $notification = $this->getMockBuilder(Notification::class) - ->setMethods([ + ->onlyMethods([ 'getApp', 'getUser', 'getDateTime',