Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions tests/Core/Command/Log/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testChangeFile(): void {
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

public function changeRotateSizeProvider() {
public static function changeRotateSizeProvider(): array {
return [
['42', 42],
['0', 0],
Expand Down Expand Up @@ -96,13 +96,17 @@ public function testGetConfiguration(): void {
['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]);
}
Expand Down
15 changes: 8 additions & 7 deletions tests/lib/Collaboration/Resources/ProviderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ public function testGetResourceProvidersInvalidProvider(): void {
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');
Expand Down
39 changes: 25 additions & 14 deletions tests/lib/DB/MigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testExecuteStepWithUnknownClass(): void {
$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(
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testExecuteStepWithSchemaChange(): void {
->method('postSchemaChange');

$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['createInstance'])
->onlyMethods(['createInstance'])
->setConstructorArgs(['testing', $this->db])
->getMock();

Expand Down Expand Up @@ -164,7 +164,7 @@ public function testExecuteStepWithoutSchemaChange(): void {
->method('postSchemaChange');

$this->migrationService = $this->getMockBuilder(MigrationService::class)
->setMethods(['createInstance'])
->onlyMethods(['createInstance'])
->setConstructorArgs(['testing', $this->db])
->getMock();

Expand All @@ -191,7 +191,7 @@ public function dataGetMigration() {
*/
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(
Expand All @@ -211,22 +211,33 @@ public function testGetMigration($alias, $expected): void {

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();
}

Expand Down
68 changes: 30 additions & 38 deletions tests/lib/L10N/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ protected function getFactory(array $methods = [], $mockRequestGetHeaderMethod =
$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'],
Expand Down Expand Up @@ -124,24 +124,17 @@ public function testFindLanguageWithNotExistingRequestLanguageAndExistingStoredU
$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');
Expand Down Expand Up @@ -175,8 +168,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStor
['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');
Expand Down Expand Up @@ -210,8 +202,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStor
['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');
Expand Down Expand Up @@ -248,8 +239,7 @@ public function testFindLanguageWithNotExistingRequestLanguageAndNotExistingStor
['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');
Expand Down Expand Up @@ -302,7 +292,7 @@ public function testFindAvailableLanguages($app): void {
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],
Expand Down Expand Up @@ -351,7 +341,7 @@ public function testLanguageExists($app, $lang, array $availableLanguages, $expe
self::assertSame($expected, $factory->languageExists($app, $lang));
}

public function dataSetLanguageFromRequest(): array {
public static function dataSetLanguageFromRequest(): array {
return [
// Language is available
[null, 'de', ['de'], 'de'],
Expand Down Expand Up @@ -406,7 +396,7 @@ public function testGetLanguageFromRequest($app, $header, array $availableLangua
}
}

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']],
Expand Down Expand Up @@ -440,7 +430,7 @@ public function testGetL10nFilesForApp($app, $lang, $expected): void {
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/'],
Expand Down Expand Up @@ -473,7 +463,7 @@ public function testFindL10NDir($app, $expected): void {
self::assertSame($expected, $this->invokePrivate($factory, 'findL10nDir', [$app]));
}

public function dataFindLanguage(): array {
public static function dataFindLanguage(): array {
return [
// Not logged in
[false, [], 'en'],
Expand Down Expand Up @@ -511,8 +501,7 @@ public function testFindLanguage($loggedIn, $availableLang, $expected): void {
});

if ($loggedIn) {
$user = $this->getMockBuilder(IUser::class)
->getMock();
$user = $this->createMock(IUser::class);
$user->expects(self::any())
->method('getUID')
->willReturn('MyUserUid');
Expand Down Expand Up @@ -670,7 +659,7 @@ public function testFindGenericLanguageFallback(): void {
self::assertSame('en', $lang);
}

public function dataTestRespectDefaultLanguage(): array {
public static function dataTestRespectDefaultLanguage(): array {
return [
['de', 'de_DE', true, 'de_DE'],
['de', 'de', true, 'de'],
Expand Down Expand Up @@ -747,21 +736,22 @@ public function testReduceLanguagesByConfiguration(string $lang, array $availabl
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');

Expand All @@ -770,9 +760,11 @@ public function testGetLanguageIterator(bool $hasSession, ?IUser $iUserMock = nu
} else {
$this->expectException(\RuntimeException::class);
}
} else {
$user = $this->createMock(IUser::class);
}

$iterator = $factory->getLanguageIterator($iUserMock);
$iterator = $factory->getLanguageIterator($user);
self::assertInstanceOf(ILanguageIterator::class, $iterator);
}

Expand Down
18 changes: 12 additions & 6 deletions tests/lib/Log/LogFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void {
$this->factory = new LogFactory($this->c, $this->systemConfig);
}

public function fileTypeProvider(): array {
public static function fileTypeProvider(): array {
return [
[
'file'
Expand Down Expand Up @@ -67,14 +67,17 @@ public function testFile(string $type): void {

$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',
Expand All @@ -97,8 +100,11 @@ public function testFileCustomPath($path, $expected): void {

$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);
Expand Down
Loading
Loading