ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ConversationRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  public function testConversationsCanBeRetrieved(): void
26  {
27  $conversations_fixture = [
28  [
29  'conversation' => [
30  'id' => '1',
31  'is_group' => '1',
32  'participants' => json_encode([['id' => 1], ['id' => 2], ['id' => 3]], JSON_THROW_ON_ERROR)
33  ],
34  'messages' => [
35  [
36  'id' => '1',
37  'message' => 'Hello World',
38  'user_id' => '1',
39  'timestamp' => (string) time()
40  ],
41  ]
42  ],
43  [
44  'conversation' => [
45  'id' => '2',
46  'is_group' => '0',
47  'participants' => json_encode([['id' => 3], ['id' => 4]], JSON_THROW_ON_ERROR)
48  ],
49  'messages' => [
50  [
51  'id' => '2',
52  'message' => 'Hello World',
53  'user_id' => '1',
54  'timestamp' => (string) time()
55  ],
56  ]
57  ],
58  [
59  'conversation' => [
60  'id' => '3',
61  'is_group' => '0',
62  'participants' => json_encode([['id' => 5], ['id' => 1]], JSON_THROW_ON_ERROR)
63  ],
64  'messages' => [
65  [
66  'id' => '3',
67  'message' => 'Hello World',
68  'user_id' => '1',
69  'timestamp' => (string) time()
70  ],
71  ]
72  ],
73  [
74  'conversation' => [
75  'id' => '4',
76  'is_group' => '0',
77  'participants' => json_encode([['id' => 6], ['id' => 1]], JSON_THROW_ON_ERROR)
78  ],
79  'messages' => []
80  ],
81  ];
82 
83  $user = $this->getMockBuilder(ilObjUser::class)->onlyMethods(['getId'])->disableOriginalConstructor()->getMock();
84  $user->method('getId')->willReturn(1);
85  $db = $this->createMock(ilDBInterface::class);
86  $resultMock = $this->createMock(ilDBStatement::class);
87 
88  $db->expects($this->once())
89  ->method('query')
90  ->with($this->stringContains('FROM osc_conversation'))
91  ->willReturn($resultMock);
92 
93  $db->expects($this->exactly(count($conversations_fixture) - 1))
94  ->method('queryF')
95  ->with($this->stringContains('FROM osc_messages'))
96  ->willReturn($resultMock);
97 
98  $db->expects($this->exactly((count($conversations_fixture) * 2) - 1 + 1))->method('fetchAssoc')->with($resultMock)->willReturnOnConsecutiveCalls(
99  $conversations_fixture[0]['conversation'],
100  $conversations_fixture[0]['messages'][0],
101  $conversations_fixture[1]['conversation'],
102  $conversations_fixture[2]['conversation'],
103  $conversations_fixture[2]['messages'][0],
104  $conversations_fixture[3]['conversation'],
105  null
106  );
107 
108  $repository = new Conversation(
109  $db,
110  $user
111  );
112 
113  $conversations = $repository->findByIds(array_map(static function (array $conversation): string {
114  return $conversation['conversation']['id'];
115  }, $conversations_fixture));
116 
117  self::assertCount(count($conversations_fixture) - 1, $conversations);
118 
119  self::assertSame($conversations_fixture[0]['conversation']['id'], $conversations[0]->getId());
120  self::assertTrue($conversations[0]->isGroup());
121  self::assertSame($conversations_fixture[0]['messages'][0]['id'], $conversations[0]->getLastMessage()->getId());
122 
123  self::assertFalse($conversations[1]->isGroup());
124  self::assertSame($conversations_fixture[2]['conversation']['id'], $conversations[1]->getId());
125  self::assertSame($conversations_fixture[2]['messages'][0]['id'], $conversations[1]->getLastMessage()->getId());
126 
127  self::assertFalse($conversations[2]->isGroup());
128  self::assertSame($conversations_fixture[3]['conversation']['id'], $conversations[2]->getId());
129  self::assertSame('', $conversations[2]->getLastMessage()->getId());
130  }
131 }
Class ilOnScreenChatBaseTest.