Skip to content

Commit

Permalink
fix(CI): Move away from withConsecutive()
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Sep 22, 2023
1 parent 6db184a commit 379addd
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 93 deletions.
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
42 changes: 14 additions & 28 deletions tests/php/Chat/Parser/UserMentionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,10 @@ public function testGetRichMessageWithMentionsFullyIncludedInOtherMentions(strin

$this->userManager->expects($this->exactly(2))
->method('getDisplayName')
->withConsecutive(
[$longerId],
[$baseId]
)
->willReturnOnConsecutiveCalls(
$longerId . ' display name',
$baseId . ' display name'
);
->willReturnMap([
[$longerId, $longerId . ' display name'],
[$baseId, $baseId . ' display name']
]);

/** @var Room|MockObject $room */
$room = $this->createMock(Room::class);
Expand Down Expand Up @@ -275,29 +271,19 @@ public function testGetRichMessageWithSeveralMentions() {

$this->commentsManager->expects($this->exactly(3))
->method('resolveDisplayName')
->withConsecutive(
['user', 'testUser1'],
['user', 'testUser2'],
['user', 'testUser3']
)
->willReturn(
'testUser1 display name',
'testUser2 display name',
'testUser3 display name'
);
->willReturnMap([
['user', 'testUser1', 'testUser1 display name'],
['user', 'testUser2', 'testUser2 display name'],
['user', 'testUser3', 'testUser3 display name'],
]);

$this->userManager->expects($this->exactly(3))
->method('getDisplayName')
->withConsecutive(
['testUser1'],
['testUser2'],
['testUser3']
)
->willReturnOnConsecutiveCalls(
'testUser1 display name',
'testUser2 display name',
'testUser3 display name'
);
->willReturnMap([
['testUser1', 'testUser1 display name'],
['testUser2', 'testUser2 display name'],
['testUser3', 'testUser3 display name'],
]);

/** @var Room|MockObject $room */
$room = $this->createMock(Room::class);
Expand Down
57 changes: 43 additions & 14 deletions tests/php/Chat/SystemMessage/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
use OCA\Talk\Room;
use OCA\Talk\TalkSession;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand Down Expand Up @@ -158,7 +160,17 @@ public static function dataRoomTypes(): array {
'actorType' => 'users',
'actorId' => 'alice_actor',
'message' => ['message' => 'user_added', 'parameters' => ['user' => 'bob']],
]
],
[
'actorType' => 'users',
'actorId' => 'alice_actor',
'message' => ['message' => 'user_added', 'parameters' => ['user' => 'carmen']],
],
[
'actorType' => 'users',
'actorId' => 'alice_actor',
'message' => ['message' => 'user_added', 'parameters' => ['user' => 'delta']],
],
];

$allParticipants = [
Expand All @@ -172,12 +184,6 @@ public static function dataRoomTypes(): array {
'actorId' => 'alice_actor',
'participantType' => Participant::USER,
],
// alice_actor adding self-joined mode
[
'actorType' => 'users',
'actorId' => 'alice_actor',
'participantType' => Participant::USER_SELF_JOINED,
],
// alice_actor added bob
[
'actorType' => 'users',
Expand All @@ -187,7 +193,13 @@ public static function dataRoomTypes(): array {
// empty participant type
[
'actorType' => 'users',
'actorId' => 'alice_actor',
'actorId' => 'carmen',
],
// alice_actor adding self-joined mode
[
'actorType' => 'users',
'actorId' => 'delta',
'participantType' => Participant::USER_SELF_JOINED,
],
];

Expand Down Expand Up @@ -215,6 +227,7 @@ public function testAfterUsersAdd(int $roomType, string $objectType, array $part
// TODO: add all cases
$event = new AddParticipantsEvent($room, $participants);

$consecutive = [];
foreach ($expectedMessages as $expectedMessage) {
$consecutive[] = [
$room,
Expand All @@ -224,12 +237,20 @@ public function testAfterUsersAdd(int $roomType, string $objectType, array $part
$this->dummyTime,
false,
self::DUMMY_REFERENCE_ID,
null,
false,
];
}
if (isset($consecutive)) {
$this->chatManager
if (!empty($consecutive)) {
$i = 0;
$this->chatManager->expects($this->exactly(count($consecutive)))
->method('addSystemMessage')
->withConsecutive(...$consecutive);
->willReturnCallback(function () use ($consecutive, &$i) {
Assert::assertArrayHasKey($i, $consecutive);
Assert::assertSame($consecutive[$i], func_get_args());
$i++;
return $this->createMock(IComment::class);
});
}

$this->dispatch(Room::EVENT_AFTER_USERS_ADD, $event);
Expand Down Expand Up @@ -302,13 +323,21 @@ public function testAfterParticipantTypeSet(string $actorType, int $oldParticipa
json_encode($expectedMessage),
$this->dummyTime,
false,
self::DUMMY_REFERENCE_ID
self::DUMMY_REFERENCE_ID,
null,
false,
];
}
if (isset($consecutive)) {
$this->chatManager->expects($this->once())
$i = 0;
$this->chatManager->expects($this->exactly(count($consecutive)))
->method('addSystemMessage')
->withConsecutive(...$consecutive);
->willReturnCallback(function () use ($consecutive, &$i) {
Assert::assertArrayHasKey($i, $consecutive);
Assert::assertSame($consecutive[$i], func_get_args());
$i++;
return $this->createMock(IComment::class);
});
}

$this->dispatch(Room::EVENT_AFTER_PARTICIPANT_TYPE_SET, $event);
Expand Down
91 changes: 56 additions & 35 deletions tests/php/Controller/ChatControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use OCP\RichObjectStrings\IValidator;
use OCP\Security\ITrustedDomainHelper;
use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand Down Expand Up @@ -411,15 +412,23 @@ public function testSendReplyByUser() {

$this->messageParser->expects($this->exactly(2))
->method('createMessage')
->withConsecutive(
[$this->room, $participant, $parent, $this->l],
[$this->room, $participant, $comment, $this->l]
)
->willReturnOnConsecutiveCalls($parentMessage, $chatMessage);
->willReturnMap([
[$this->room, $participant, $parent, $this->l, $parentMessage],
[$this->room, $participant, $comment, $this->l, $chatMessage],
]);

$i = 0;
$expectedCalls = [
[$parentMessage],
[$chatMessage],
];
$this->messageParser->expects($this->exactly(2))
->method('parseMessage')
->withConsecutive([$parentMessage], [$chatMessage]);
->willReturnCallback(function () use ($expectedCalls, &$i) {
Assert::assertArrayHasKey($i, $expectedCalls);
Assert::assertSame($expectedCalls[$i], func_get_args());
$i++;
});

$this->controller->setRoom($this->room);
$this->controller->setParticipant($participant);
Expand Down Expand Up @@ -750,15 +759,18 @@ public function testReceiveHistoryByUser() {
$participant = $this->createMock(Participant::class);

$i = 4;
$expectedCalls = [
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l],
];
$this->messageParser->expects($this->exactly(4))
->method('createMessage')
->withConsecutive(
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l]
)
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use (&$i) {
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use ($expectedCalls, &$i) {
Assert::assertArrayHasKey(4 - $i, $expectedCalls);
Assert::assertSame($expectedCalls[4 - $i], func_get_args());

$chatMessage = $this->createMock(Message::class);
$chatMessage->expects($this->once())
->method('getVisibility')
Expand Down Expand Up @@ -816,15 +828,18 @@ public function testReceiveMessagesByUserNotJoinedButInRoom() {
]);

$i = 4;
$expectedCalls = [
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l],
];
$this->messageParser->expects($this->exactly(4))
->method('createMessage')
->withConsecutive(
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l]
)
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use (&$i) {
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use ($expectedCalls, &$i) {
Assert::assertArrayHasKey(4 - $i, $expectedCalls);
Assert::assertSame($expectedCalls[4 - $i], func_get_args());

$chatMessage = $this->createMock(Message::class);
$chatMessage->expects($this->once())
->method('getVisibility')
Expand Down Expand Up @@ -885,15 +900,18 @@ public function testReceiveMessagesByGuest() {
]);

$i = 4;
$expectedCalls = [
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l],
];
$this->messageParser->expects($this->exactly(4))
->method('createMessage')
->withConsecutive(
[$this->room, $participant, $comment4, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment1, $this->l]
)
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use (&$i) {
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use ($expectedCalls, &$i) {
Assert::assertArrayHasKey(4 - $i, $expectedCalls);
Assert::assertSame($expectedCalls[4 - $i], func_get_args());

$chatMessage = $this->createMock(Message::class);
$chatMessage->expects($this->once())
->method('getVisibility')
Expand Down Expand Up @@ -962,15 +980,18 @@ public function testWaitForNewMessagesByUser() {
->willReturn($testUser);

$i = 1;
$this->messageParser->expects($this->exactly(4))
$expectedCalls = [
[$this->room, $participant, $comment1, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment4, $this->l],
];
$this->messageParser->expects($this->exactly(count($expectedCalls)))
->method('createMessage')
->withConsecutive(
[$this->room, $participant, $comment1, $this->l],
[$this->room, $participant, $comment2, $this->l],
[$this->room, $participant, $comment3, $this->l],
[$this->room, $participant, $comment4, $this->l]
)
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use (&$i) {
->willReturnCallback(function ($room, $participant, IComment $comment, $l) use ($expectedCalls, &$i) {
Assert::assertArrayHasKey($i - 1, $expectedCalls);
Assert::assertSame($expectedCalls[$i - 1], func_get_args());

$chatMessage = $this->createMock(Message::class);
$chatMessage->expects($this->once())
->method('getVisibility')
Expand Down
10 changes: 8 additions & 2 deletions tests/php/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use OCP\Notification\INotification;
use OCP\RichObjectStrings\Definitions;
use OCP\Share\IManager as IShareManager;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand Down Expand Up @@ -946,10 +947,15 @@ public function testPrepareChatMessage(string $subject, int $roomType, array $su
$userManagerGet['with'][] = [$subjectParameters['userId']];
$userManagerGet['willReturn'][] = null;
}
$i = 0;
$this->userManager->expects($this->exactly(count($userManagerGet['with'])))
->method('getDisplayName')
->withConsecutive(...$userManagerGet['with'])
->willReturnOnConsecutiveCalls(...$userManagerGet['willReturn']);
->willReturnCallback(function () use ($userManagerGet, &$i) {
Assert::assertArrayHasKey($i, $userManagerGet['with']);
Assert::assertSame($userManagerGet['with'][$i], func_get_args());
$i++;
return $userManagerGet['willReturn'][$i - 1];
});

$comment = $this->createMock(IComment::class);
$comment->expects($this->any())
Expand Down
Loading

0 comments on commit 379addd

Please sign in to comment.