Skip to content

Commit

Permalink
Merge pull request #10571 from nextcloud/techdebt/noid/prepare-phpuni…
Browse files Browse the repository at this point in the history
…t-10

techdebt(CI): Prepare unit tests for PHPUnit 10
  • Loading branch information
nickvergessen authored Sep 25, 2023
2 parents 6c5dccb + 379addd commit c581c49
Show file tree
Hide file tree
Showing 32 changed files with 272 additions and 229 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
],
"test:unit": "vendor/bin/phpunit -c tests/php/phpunit.xml --color --fail-on-warning --fail-on-risky"
"test:unit": "vendor/bin/phpunit -c tests/php/phpunit.xml --colors=always --fail-on-warning --fail-on-risky"
},
"require-dev": {
"nextcloud/ocp": "dev-master",
Expand Down
24 changes: 11 additions & 13 deletions tests/php/Activity/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,21 @@ protected function getProvider(array $methods = []) {
}


public function dataPreParse(): array {
$user = $this->createMock(IUser::class);
public static function dataPreParse(): array {
return [
['other', null, true, true],
['spreed', null, true, true],
['spreed', $user, true, true],
['spreed', $user, false, false],
['other', false, true, true],
['spreed', false, true, true],
['spreed', true, true, true],
['spreed', true, false, false],
];
}

/**
* @dataProvider dataPreParse
*
* @param bool $validUser
* @param bool $disabledForUser
*/
public function testPreParse(string $appId, ?IUser $user, bool $disabledForUser, bool $willThrowException): void {
public function testPreParse(string $appId, bool $hasUser, bool $disabledForUser, bool $willThrowException): void {
$user = $hasUser ? $this->createMock(IUser::class) : null;

/** @var IEvent|MockObject $event */
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
Expand Down Expand Up @@ -148,7 +146,7 @@ public function testPreParseThrows() {
static::invokePrivate($provider, 'preParse', [$event]);
}

public function dataSetSubject() {
public static function dataSetSubject() {
return [
['No placeholder', [], 'No placeholder'],
['This has one {placeholder}', ['placeholder' => ['name' => 'foobar']], 'This has one foobar'],
Expand Down Expand Up @@ -179,7 +177,7 @@ public function testSetSubject($subject, array $parameters, $parsedSubject) {
self::invokePrivate($provider, 'setSubjects', [$event, $subject, $parameters]);
}

public function dataGetRoom() {
public static function dataGetRoom() {
return [
[Room::TYPE_ONE_TO_ONE, 23, 'private-call', 'private-call', 'one2one'],
[Room::TYPE_GROUP, 42, 'group-call', 'group-call', 'group'],
Expand Down Expand Up @@ -232,7 +230,7 @@ public function testGetRoom($type, $id, $name, $expectedName, $expectedType) {
], self::invokePrivate($provider, 'getRoom', [$room, 'user']));
}

public function dataGetUser(): array {
public static function dataGetUser(): array {
return [
['test', true, 'Test'],
['foo', false, 'foo'],
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Activity/Provider/InvitationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function testParseThrowsWrongSubject() {
$provider->parse('en', $event);
}

public function dataParse() {
public static function dataParse() {
return [
['en', true, ['room' => 23, 'user' => 'test1'], ['actor' => ['actor-data'], 'call' => ['call-data']]],
['de', false, ['room' => 42, 'user' => 'test2'], ['actor' => ['actor-data'], 'call' => ['call-unknown']]],
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Activity/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Test\TestCase;

class SettingTest extends TestCase {
public function dataSettings() {
public static function dataSettings() {
return [
[Setting::class],
];
Expand Down
27 changes: 17 additions & 10 deletions tests/php/BackgroundJob/CheckHostedSignalingServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
Expand Down Expand Up @@ -106,21 +107,27 @@ public function testRunWithPendingToActiveChange() {

$this->config
->method('getAppValue')
->will($this->returnValueMap([
->willReturnMap([
['spreed', 'hosted-signaling-server-account-id', '', 'my-account-id'],
['spreed', 'hosted-signaling-server-account', '{}', '{"status": "pending"}']
]));
]);
$this->config->expects($this->once())
->method('deleteAppValue')
->withConsecutive(
['spreed', 'signaling_mode'],
);
$this->config->expects($this->exactly(2))
->with('spreed', 'signaling_mode');

$expectedCalls = [
['spreed', 'signaling_servers', '{"servers":[{"server":"signaling-url","verify":true}],"secret":"signaling-secret"}'],
['spreed', 'hosted-signaling-server-account', json_encode($newStatus)],
];

$i = 0;
$this->config->expects($this->exactly(count($expectedCalls)))
->method('setAppValue')
->withConsecutive(
['spreed', 'signaling_servers', '{"servers":[{"server":"signaling-url","verify":true}],"secret":"signaling-secret"}'],
['spreed', 'hosted-signaling-server-account', json_encode($newStatus)]
);
->willReturnCallback(function () use ($expectedCalls, &$i) {
Assert::assertArrayHasKey($i, $expectedCalls);
Assert::assertSame($expectedCalls[$i], func_get_args());
$i++;
});

$group = $this->createMock(IGroup::class);
$this->groupManager->expects($this->once())
Expand Down
6 changes: 3 additions & 3 deletions tests/php/BackgroundJob/RemoveEmptyRoomsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testDeleteIfFileIsRemoved(string $objectType, array $fileList, i
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}

public function dataDeleteIfFileIsRemoved(): array {
public static function dataDeleteIfFileIsRemoved(): array {
return [
['', [], 0],
['email', [], 0],
Expand Down Expand Up @@ -145,7 +145,7 @@ public function testDeleteIfIsEmpty(string $objectType, int $actorsCount, int $n
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}

public function dataDeleteIfIsEmpty(): array {
public static function dataDeleteIfIsEmpty(): array {
return [
['', 1, 0],
['file', 1, 0],
Expand All @@ -169,7 +169,7 @@ public function testCallback(int $roomType, string $objectType, int $numDeletedR
$this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
}

public function dataCallback(): array {
public static function dataCallback(): array {
return [
[Room::TYPE_CHANGELOG, '', 0],
[Room::TYPE_GROUP, 'file', 1],
Expand Down
4 changes: 2 additions & 2 deletions tests/php/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function testGetCapabilitiesGuest(): void {
], $capabilities->getCapabilities());
}

public function dataGetCapabilitiesUserAllowed(): array {
public static function dataGetCapabilitiesUserAllowed(): array {
return [
[true, false, 'none', true, Participant::PRIVACY_PRIVATE],
[false, true, '1 MB', true, Participant::PRIVACY_PUBLIC],
Expand Down Expand Up @@ -428,7 +428,7 @@ public function testConfigRecording(bool $enabled): void {
$this->assertEquals($data['spreed']['config']['call']['recording'], $enabled);
}

public function dataTestConfigRecording(): array {
public static function dataTestConfigRecording(): array {
return [
[true],
[false],
Expand Down
12 changes: 6 additions & 6 deletions tests/php/Chat/AutoComplete/SearchPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function getPlugin(array $methods = []) {
$this->userId,
$this->l,
])
->setMethods($methods)
->onlyMethods($methods)
->getMock();
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public function testSearch() {
$plugin->search('fo', 10, 0, $result);
}

public function dataSearchUsers() {
public static function dataSearchUsers() {
return [
['test', [], [], [], []],
['test', ['current', 'foo', 'test', 'test1'], [
Expand Down Expand Up @@ -216,7 +216,7 @@ public function testSearchUsers($search, array $userIds, array $userNames, array
self::invokePrivate($plugin, 'searchUsers', [$search, $userIds, $result]);
}

public function dataSearchGuests() {
public static function dataSearchGuests() {
return [
['test', [], [], []],
['', ['abcdef' => ''], [['abcdef' => 'Guest']], []],
Expand Down Expand Up @@ -269,7 +269,7 @@ protected function createUserMock(array $userData) {
return $user;
}

public function dataCreateResult() {
public static function dataCreateResult() {
return [
['user', 'foo', 'bar', '', ['label' => 'bar', 'value' => ['shareType' => 'user', 'shareWith' => 'foo']]],
['user', 'test', 'Test', '', ['label' => 'Test', 'value' => ['shareType' => 'user', 'shareWith' => 'test']]],
Expand Down Expand Up @@ -304,7 +304,7 @@ public function testCreateResult($type, $uid, $name, $managerName, array $expect
}


public function dataCreateGuestResult(): array {
public static function dataCreateGuestResult(): array {
return [
['1234', 'foo', ['label' => 'foo', 'value' => ['shareType' => 'guest', 'shareWith' => 'guest/1234']]],
['abcd', 'bar', ['label' => 'bar', 'value' => ['shareType' => 'guest', 'shareWith' => 'guest/abcd']]],
Expand Down Expand Up @@ -359,7 +359,7 @@ public function testSearchGroups($search, $groupIds, $isGroup, $displayName, $to
$this->assertCount($totalExactMatches, $actual['exact']['groups']);
}

public function dataSearchGroups(): array {
public static function dataSearchGroups(): array {
return [
// $search, $groupIds, $isGroup, $displayName, $totalMatches, $totalExactMatches
['', ['groupid'], true, 'group', 1, 0],
Expand Down
24 changes: 12 additions & 12 deletions tests/php/Chat/AutoComplete/SorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
*/

namespace OCA\Talk\Tests\php\Chat;
namespace OCA\Talk\Tests\php\Chat\AutoComplete;

use OCA\Talk\Chat\AutoComplete\Sorter;
use OCA\Talk\Chat\CommentsManager;
Expand All @@ -36,23 +36,23 @@ class SorterTest extends TestCase {

protected ?Sorter $sorter = null;

protected array $user1 = [
protected static array $user1 = [
'label' => 'Seattle',
'value' => [
'shareType' => 'user',
'shareWith' => 'seattle',
],
];

protected array $user2 = [
protected static array $user2 = [
'label' => 'New York',
'value' => [
'shareType' => 'user',
'shareWith' => 'new_york',
],
];

protected array $user3 = [
protected static array $user3 = [
'label' => 'ttle Sea',
'value' => [
'shareType' => 'user',
Expand All @@ -71,15 +71,15 @@ public function testGetId() {
$this->assertSame('talk_chat_participants', $this->sorter->getId());
}

public function dataSort(): array {
public static function dataSort(): array {
return [
'no user posted' => ['', ['users' => [$this->user1, $this->user2]], [], ['users' => [$this->user1, $this->user2]]],
'second user posted' => ['', ['users' => [$this->user1, $this->user2]], ['new_york' => new \DateTime('2000-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted later' => ['', ['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2017-01-01'), 'new_york' => new \DateTime('2018-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted earlier' => ['', ['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2018-01-01'), 'new_york' => new \DateTime('2017-01-01')], ['users' => [$this->user1, $this->user2]]],
'starting match first1' => ['Sea', ['users' => [$this->user1, $this->user3]], [], ['users' => [$this->user1, $this->user3]]],
'starting match first2' => ['Sea', ['users' => [$this->user3, $this->user1]], [], ['users' => [$this->user1, $this->user3]]],
'no users' => ['', ['groups' => [$this->user1, $this->user2]], [], ['groups' => [$this->user1, $this->user2]]],
'no user posted' => ['', ['users' => [self::$user1, self::$user2]], [], ['users' => [self::$user1, self::$user2]]],
'second user posted' => ['', ['users' => [self::$user1, self::$user2]], ['new_york' => new \DateTime('2000-01-01')], ['users' => [self::$user2, self::$user1]]],
'second user posted later' => ['', ['users' => [self::$user1, self::$user2]], ['seattle' => new \DateTime('2017-01-01'), 'new_york' => new \DateTime('2018-01-01')], ['users' => [self::$user2, self::$user1]]],
'second user posted earlier' => ['', ['users' => [self::$user1, self::$user2]], ['seattle' => new \DateTime('2018-01-01'), 'new_york' => new \DateTime('2017-01-01')], ['users' => [self::$user1, self::$user2]]],
'starting match first1' => ['Sea', ['users' => [self::$user1, self::$user3]], [], ['users' => [self::$user1, self::$user3]]],
'starting match first2' => ['Sea', ['users' => [self::$user3, self::$user1]], [], ['users' => [self::$user1, self::$user3]]],
'no users' => ['', ['groups' => [self::$user1, self::$user2]], [], ['groups' => [self::$user1, self::$user2]]],
];
}

Expand Down
10 changes: 5 additions & 5 deletions tests/php/Chat/ChatManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected function assertCommentEquals(array $data, IComment $comment): void {
]);
}

public function dataSendMessage(): array {
public static function dataSendMessage(): array {
return [
'simple message' => ['testUser1', 'testMessage1', '', '0'],
'reference id' => ['testUser2', 'testMessage2', 'referenceId2', '0'],
Expand Down Expand Up @@ -654,7 +654,7 @@ public function testClearHistory(): void {
$manager->clearHistory($chat, 'users', 'admin');
}

public function dataSearchIsPartOfConversationNameOrAtAll(): array {
public static function dataSearchIsPartOfConversationNameOrAtAll(): array {
return [
'found a in all' => [
'a', 'room', true
Expand Down Expand Up @@ -697,7 +697,7 @@ public function testSearchIsPartOfConversationNameOrAtAll(string $search, string
$this->assertEquals($expected, $actual);
}

public function dataAddConversationNotify(): array {
public static function dataAddConversationNotify(): array {
return [
[
'',
Expand Down Expand Up @@ -765,7 +765,7 @@ public function testIsSharedFile(string $message, bool $expected): void {
$this->assertEquals($expected, $actual);
}

public function dataIsSharedFile(): array {
public static function dataIsSharedFile(): array {
return [
['', false],
[json_encode([]), false],
Expand Down Expand Up @@ -802,7 +802,7 @@ public function testFilterCommentsWithNonExistingFiles(array $list, int $expecte
$this->assertCount($expectedCount, $result);
}

public function dataFilterCommentsWithNonExistingFiles(): array {
public static function dataFilterCommentsWithNonExistingFiles(): array {
return [
[[], 0],
[[json_encode(['parameters' => ['not a shared file']])], 1],
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Chat/Command/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setUp(): void {
);
}

public function dataExecApp(): array {
public static function dataExecApp(): array {
return [
['arguments1', ''],
['arguments2', "output from\nevent"],
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testExecApp(string $arguments, string $expected): void {
$this->assertSame($expected, self::invokePrivate($executor, 'execApp', [$room, $message, $command, $arguments]));
}

public function dataExecShell(): array {
public static function dataExecShell(): array {
return [
['admin', 'token', '', '', ''],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} {ARGUMENTS}', 'foo bar "hello bear"', 'output1'],
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Chat/Command/ShellExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Test\TestCase;

class ShellExecutorTest extends TestCase {
public function dataExecShellRun(): array {
public static function dataExecShellRun(): array {
return [
['admin', 'token', 'echo {ARGUMENTS}', '$PATH', '$PATH'],
['admin', 'token', 'echo {ARGUMENTS}', '$(pwd)', '$(pwd)'],
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testExecShellRun(?string $actorId, string $roomToken, string $cm
$this->assertSame($output, $executor->execShell($cmd, $arguments, $roomToken, $actorId));
}

public function dataExecShell(): array {
public static function dataExecShell(): array {
return [
['admin', 'token', '', '', '', ''],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} {ARGUMENTS}', 'foo bar "hello bear"', "/var/www/nextcloud/script.sh 'admin' 'token' 'foo bar \"hello bear\"'", 'output1'],
Expand Down
Loading

0 comments on commit c581c49

Please sign in to comment.