Merge pull request #52552 from nextcloud/techdebt/noid/more-phpunit-10-preparations

tests: Prepare more tests for PHPUnit 10
pull/52558/head
Joas Schilling 2025-04-30 09:42:57 +07:00 committed by GitHub
commit d20e4d2fe9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 135 additions and 135 deletions

@ -62,7 +62,7 @@ class FileTest extends TestCase {
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
public function changeRotateSizeProvider() {
public static function changeRotateSizeProvider(): array {
return [
['42', 42],
['0', 0],
@ -96,13 +96,17 @@ class FileTest extends TestCase {
['log_rotate_size', 100 * 1024 * 1024, 5 * 1024 * 1024],
]);
$calls = [
['Log backend file: disabled'],
['Log file: /var/log/nextcloud.log'],
['Rotate at: 5 MB'],
];
$this->consoleOutput->expects($this->exactly(3))
->method('writeln')
->withConsecutive(
['Log backend file: disabled'],
['Log file: /var/log/nextcloud.log'],
['Rotate at: 5 MB'],
);
->willReturnCallback(function (string $message) use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected[0], $message);
});
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

@ -77,13 +77,14 @@ class ProviderManagerTest extends TestCase {
public function testGetResourceProvidersValidAndInvalidProvider(): void {
$this->serverContainer->expects($this->exactly(2))
->method('query')
->withConsecutive(
[$this->equalTo('InvalidResourceProvider')],
[$this->equalTo(ResourceProvider::class)],
)->willReturnOnConsecutiveCalls(
$this->throwException(new QueryException('A meaningful error message')),
$this->createMock(ResourceProvider::class),
);
->willReturnCallback(function (string $service) {
if ($service === 'InvalidResourceProvider') {
throw new QueryException('A meaningful error message');
}
if ($service === ResourceProvider::class) {
return $this->createMock(ResourceProvider::class);
}
});
$this->logger->expects($this->once())
->method('error');

@ -93,7 +93,7 @@ class MigrationsTest extends \Test\TestCase {
$this->expectExceptionMessage('Migration step \'X\' is unknown');
$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['findMigrations'])
->onlyMethods(['findMigrations'])
->setConstructorArgs(['testing', $this->db])
->getMock();
$this->migrationService->expects($this->any())->method('findMigrations')->willReturn(
@ -134,7 +134,7 @@ class MigrationsTest extends \Test\TestCase {
->method('postSchemaChange');
$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['createInstance'])
->onlyMethods(['createInstance'])
->setConstructorArgs(['testing', $this->db])
->getMock();
@ -164,7 +164,7 @@ class MigrationsTest extends \Test\TestCase {
->method('postSchemaChange');
$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['createInstance'])
->onlyMethods(['createInstance'])
->setConstructorArgs(['testing', $this->db])
->getMock();
@ -191,7 +191,7 @@ class MigrationsTest extends \Test\TestCase {
*/
public function testGetMigration($alias, $expected): void {
$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['getMigratedVersions', 'findMigrations'])
->onlyMethods(['getMigratedVersions', 'findMigrations'])
->setConstructorArgs(['testing', $this->db])
->getMock();
$this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
@ -211,22 +211,33 @@ class MigrationsTest extends \Test\TestCase {
public function testMigrate(): void {
$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['getMigratedVersions', 'findMigrations', 'executeStep'])
->onlyMethods(['getMigratedVersions', 'findMigrations', 'executeStep'])
->setConstructorArgs(['testing', $this->db])
->getMock();
$this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
['20170130180000', '20170130180001']
);
$this->migrationService->expects($this->any())->method('findMigrations')->willReturn(
['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
);
$this->migrationService->method('getMigratedVersions')
->willReturn(
['20170130180000', '20170130180001']
);
$this->migrationService->method('findMigrations')
->willReturn(
['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
);
$this->assertEquals(
['20170130180000', '20170130180001', '20170130180002', '20170130180003'],
$this->migrationService->getAvailableVersions());
$this->migrationService->getAvailableVersions()
);
$this->migrationService->expects($this->exactly(2))->method('executeStep')
->withConsecutive(['20170130180002'], ['20170130180003']);
$calls = [
['20170130180002', false],
['20170130180003', false],
];
$this->migrationService->expects($this->exactly(2))
->method('executeStep')
->willReturnCallback(function () use (&$calls) {
$expected = array_shift($calls);
$this->assertEquals($expected, func_get_args());
});
$this->migrationService->migrate();
}

@ -83,14 +83,14 @@ class FactoryTest extends TestCase {
$this->serverRoot,
$this->appManager,
])
->setMethods($methods)
->onlyMethods($methods)
->getMock();
}
return new Factory($this->config, $this->request, $this->userSession, $this->cacheFactory, $this->serverRoot, $this->appManager);
}
public function dataFindAvailableLanguages(): array {
public static function dataFindAvailableLanguages(): array {
return [
[null],
['files'],
@ -124,24 +124,17 @@ class FactoryTest extends TestCase {
$this->invokePrivate($factory, 'requestLanguage', ['de']);
$factory->expects($this->exactly(2))
->method('languageExists')
->withConsecutive(
['MyApp', 'de'],
['MyApp', 'jp'],
)
->willReturnOnConsecutiveCalls(
false,
true,
);
->willReturnMap([
['MyApp', 'de', false],
['MyApp', 'jp', true],
]);
$this->config
->expects($this->exactly(1))
->method('getSystemValue')
->withConsecutive(
['force_language', false],
)->willReturnOnConsecutiveCalls(
false,
);
$user = $this->getMockBuilder(IUser::class)
->getMock();
->willReturnMap([
['force_language', false, false],
]);
$user = $this->createMock(IUser::class);
$user->expects(self::once())
->method('getUID')
->willReturn('MyUserUid');
@ -175,8 +168,7 @@ class FactoryTest extends TestCase {
['force_language', false, false],
['default_language', false, 'es']
]);
$user = $this->getMockBuilder(IUser::class)
->getMock();
$user = $this->createMock(IUser::class);
$user->expects(self::once())
->method('getUID')
->willReturn('MyUserUid');
@ -210,8 +202,7 @@ class FactoryTest extends TestCase {
['force_language', false, false],
['default_language', false, 'es']
]);
$user = $this->getMockBuilder(IUser::class)
->getMock();
$user = $this->createMock(IUser::class);
$user->expects(self::once())
->method('getUID')
->willReturn('MyUserUid');
@ -248,8 +239,7 @@ class FactoryTest extends TestCase {
['force_language', false, false],
['default_language', false, 'es']
]);
$user = $this->getMockBuilder(IUser::class)
->getMock();
$user = $this->createMock(IUser::class);
$user->expects(self::once())
->method('getUID')
->willReturn('MyUserUid');
@ -302,7 +292,7 @@ class FactoryTest extends TestCase {
self::assertEqualsCanonicalizing(['cs', 'de', 'en', 'ru'], $factory->findAvailableLanguages($app));
}
public function dataLanguageExists(): array {
public static function dataLanguageExists(): array {
return [
[null, 'en', [], true],
[null, 'de', [], false],
@ -351,7 +341,7 @@ class FactoryTest extends TestCase {
self::assertSame($expected, $factory->languageExists($app, $lang));
}
public function dataSetLanguageFromRequest(): array {
public static function dataSetLanguageFromRequest(): array {
return [
// Language is available
[null, 'de', ['de'], 'de'],
@ -406,7 +396,7 @@ class FactoryTest extends TestCase {
}
}
public function dataGetL10nFilesForApp(): array {
public static function dataGetL10nFilesForApp(): array {
return [
['', 'de', [\OC::$SERVERROOT . '/core/l10n/de.json']],
['core', 'ru', [\OC::$SERVERROOT . '/core/l10n/ru.json']],
@ -440,7 +430,7 @@ class FactoryTest extends TestCase {
self::assertSame($expected, $this->invokePrivate($factory, 'getL10nFilesForApp', [$app, $lang]));
}
public function dataFindL10NDir(): array {
public static function dataFindL10NDir(): array {
return [
['', \OC::$SERVERROOT . '/core/l10n/'],
['core', \OC::$SERVERROOT . '/core/l10n/'],
@ -473,7 +463,7 @@ class FactoryTest extends TestCase {
self::assertSame($expected, $this->invokePrivate($factory, 'findL10nDir', [$app]));
}
public function dataFindLanguage(): array {
public static function dataFindLanguage(): array {
return [
// Not logged in
[false, [], 'en'],
@ -511,8 +501,7 @@ class FactoryTest extends TestCase {
});
if ($loggedIn) {
$user = $this->getMockBuilder(IUser::class)
->getMock();
$user = $this->createMock(IUser::class);
$user->expects(self::any())
->method('getUID')
->willReturn('MyUserUid');
@ -670,7 +659,7 @@ class FactoryTest extends TestCase {
self::assertSame('en', $lang);
}
public function dataTestRespectDefaultLanguage(): array {
public static function dataTestRespectDefaultLanguage(): array {
return [
['de', 'de_DE', true, 'de_DE'],
['de', 'de', true, 'de'],
@ -747,21 +736,22 @@ class FactoryTest extends TestCase {
self::assertEqualsCanonicalizing($expected, $commonLanguagesCodes);
}
public function languageIteratorRequestProvider():array {
public static function languageIteratorRequestProvider(): array {
return [
[ true, $this->createMock(IUser::class)],
[ false, $this->createMock(IUser::class)],
[ false, null]
[ true, true],
[ false, true],
[ false, false],
];
}
/**
* @dataProvider languageIteratorRequestProvider
*/
public function testGetLanguageIterator(bool $hasSession, ?IUser $iUserMock = null): void {
public function testGetLanguageIterator(bool $hasSession, bool $mockUser): void {
$factory = $this->getFactory();
$user = null;
if ($iUserMock === null) {
if (!$mockUser) {
$matcher = $this->userSession->expects(self::once())
->method('getUser');
@ -770,9 +760,11 @@ class FactoryTest extends TestCase {
} else {
$this->expectException(\RuntimeException::class);
}
} else {
$user = $this->createMock(IUser::class);
}
$iterator = $factory->getLanguageIterator($iUserMock);
$iterator = $factory->getLanguageIterator($user);
self::assertInstanceOf(ILanguageIterator::class, $iterator);
}

@ -39,7 +39,7 @@ class LogFactoryTest extends TestCase {
$this->factory = new LogFactory($this->c, $this->systemConfig);
}
public function fileTypeProvider(): array {
public static function fileTypeProvider(): array {
return [
[
'file'
@ -67,14 +67,17 @@ class LogFactoryTest extends TestCase {
$this->systemConfig->expects($this->exactly(3))
->method('getValue')
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
->willReturnOnConsecutiveCalls($datadir, $defaultLog, 0640);
->willReturnMap([
['datadirectory', $datadir, $datadir],
['logfile', $defaultLog, $defaultLog],
['logfilemode', 0640, 0640],
]);
$log = $this->factory->get($type);
$this->assertInstanceOf(File::class, $log);
}
public function logFilePathProvider():array {
public static function logFilePathProvider():array {
return [
[
'/dev/null',
@ -97,8 +100,11 @@ class LogFactoryTest extends TestCase {
$this->systemConfig->expects($this->exactly(3))
->method('getValue')
->withConsecutive(['datadirectory', $datadir], ['logfile', $defaultLog], ['logfilemode', 0640])
->willReturnOnConsecutiveCalls($datadir, $path, 0640);
->willReturnMap([
['datadirectory', $datadir, $datadir],
['logfile', $defaultLog, $path],
['logfilemode', 0640, 0640],
]);
$log = $this->factory->get('file');
$this->assertInstanceOf(File::class, $log);

@ -29,13 +29,12 @@ class ProviderTest extends \Test\TestCase {
$this->appManager
->expects($this->exactly(4))
->method('isEnabledForUser')
->withConsecutive(
['files_sharing'],
['federation'],
['activity'],
['provisioning_api']
)
->willReturn(false);
->willReturnMap([
['files_sharing', null, false],
['federation', null, false],
['activity', null, false],
['provisioning_api', null, false],
]);
$expected = new \OCP\AppFramework\Http\JSONResponse(
[
@ -60,18 +59,12 @@ class ProviderTest extends \Test\TestCase {
$this->appManager
->expects($this->exactly(4))
->method('isEnabledForUser')
->withConsecutive(
['files_sharing'],
['federation'],
['activity'],
['provisioning_api']
)
->willReturnOnConsecutiveCalls(
true,
false,
false,
false
);
->willReturnMap([
['files_sharing', null, true],
['federation', null, false],
['activity', null, false],
['provisioning_api', null, false],
]);
$expected = new \OCP\AppFramework\Http\JSONResponse(
[
@ -109,18 +102,12 @@ class ProviderTest extends \Test\TestCase {
$this->appManager
->expects($this->exactly(4))
->method('isEnabledForUser')
->withConsecutive(
['files_sharing'],
['federation'],
['activity'],
['provisioning_api']
)
->willReturnOnConsecutiveCalls(
false,
true,
false,
false
);
->willReturnMap([
['files_sharing', null, false],
['federation', null, true],
['activity', null, false],
['provisioning_api', null, false],
]);
$expected = new \OCP\AppFramework\Http\JSONResponse(
[

@ -75,7 +75,9 @@ class ManagerTest extends TestCase {
$this->crypto,
$this->config,
$this->logger
])->setMethods($setMethods)->getMock();
])
->onlyMethods($setMethods)
->getMock();
}
}
@ -104,14 +106,10 @@ class ManagerTest extends TestCase {
$folder
->expects($this->exactly(2))
->method('getFile')
->withConsecutive(
['private'],
['public']
)
->willReturnOnConsecutiveCalls(
$privateFile,
$publicFile
);
->willReturnMap([
['private', $privateFile],
['public', $publicFile],
]);
$this->appData
->expects($this->once())
->method('getFolder')
@ -155,14 +153,10 @@ class ManagerTest extends TestCase {
$folder
->expects($this->exactly(2))
->method('newFile')
->withConsecutive(
['private'],
['public']
)
->willReturnOnConsecutiveCalls(
$privateFile,
$publicFile
);
->willReturnMap([
['private', null, $privateFile],
['public', null, $publicFile],
]);
$this->appData
->expects($this->exactly(2))
->method('getFolder')

@ -178,10 +178,6 @@ class ManagerTest extends TestCase {
$this->container->expects($this->exactly(2))
->method('get')
->withConsecutive(
['section1'],
['section2']
)
->willReturnMap([
['section1', $section],
['section2', $section2],

@ -42,7 +42,7 @@ class ChangesCheckTest extends TestCase {
$this->checker = new ChangesCheck($this->clientService, $this->mapper, $this->logger);
}
public function statusCodeProvider():array {
public static function statusCodeProvider(): array {
return [
[200, ChangesCheck::RESPONSE_HAS_CONTENT],
[304, ChangesCheck::RESPONSE_USE_CACHE],
@ -74,8 +74,10 @@ class ChangesCheckTest extends TestCase {
$entry = $this->createMock(Changes::class);
$entry->expects($this->exactly(2))
->method('__call')
->withConsecutive(['getVersion'], ['setVersion', [$version]])
->willReturnOnConsecutiveCalls('', null);
->willReturnMap([
['getVersion', [], ''],
['setVersion', [$version], null],
]);
$this->mapper->expects($this->once())
->method('insert');
@ -100,7 +102,7 @@ class ChangesCheckTest extends TestCase {
$this->invokePrivate($this->checker, 'cacheResult', [$entry, $version]);
}
public function changesXMLProvider(): array {
public static function changesXMLProvider(): array {
return [
[ # 0 - full example
'<?xml version="1.0" encoding="utf-8" ?>
@ -277,7 +279,7 @@ class ChangesCheckTest extends TestCase {
$this->assertSame($expected, $actual);
}
public function etagProvider() {
public static function etagProvider() {
return [
[''],
['a27aab83d8205d73978435076e53d143']
@ -310,7 +312,7 @@ class ChangesCheckTest extends TestCase {
$this->assertInstanceOf(IResponse::class, $response);
}
public function versionProvider(): array {
public static function versionProvider(): array {
return [
['13.0.7', '13.0.7'],
['13.0.7.3', '13.0.7'],
@ -329,12 +331,12 @@ class ChangesCheckTest extends TestCase {
$this->assertSame($expected, $normalized);
}
public function changeDataProvider():array {
$testDataFound = $testDataNotFound = $this->versionProvider();
array_walk($testDataFound, function (&$params) {
public static function changeDataProvider():array {
$testDataFound = $testDataNotFound = self::versionProvider();
array_walk($testDataFound, static function (&$params) {
$params[] = true;
});
array_walk($testDataNotFound, function (&$params) {
array_walk($testDataNotFound, static function (&$params) {
$params[] = false;
});
return array_merge($testDataFound, $testDataNotFound);

@ -88,10 +88,17 @@ class AvailabilityCoordinatorTest extends TestCase {
->method('getAbsence')
->with($user->getUID())
->willReturn($absence);
$calls = [
[$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
[$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300],
];
$this->cache->expects(self::exactly(2))
->method('set')
->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
[$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300]);
->willReturnCallback(static function () use (&$calls): void {
$expected = array_shift($calls);
self::assertEquals($expected, func_get_args());
});
$expected = new OutOfOfficeData(
'420',