public function testUsersExistsInChat() { $this->cm->addUser($this->rid, $this->userId); $this->assertFalse((bool) $this->cm->isUserExistsInChat($this->userId, $this->chatId), 'User should not be added to chat'); $this->cm->findChat($this->chatId, $this->rid); $rid = $this->cm->isUserExistsInChat($this->userId, $this->chatId); $this->assertTrue((bool) $rid, 'User should be in chat'); $this->assertEquals($this->rid, $rid, 'Resource id should match'); }
/** * Process auth request. Find user chat(if not exists - create it) * and send message to all other clients * * @access private * @param $rid * @param $data * @return void */ private function authRequest($rid, array $data) { $chatId = $data['cid']; Yii::info('Auth request from rid: ' . $rid . ' and chat: ' . $chatId, 'chat'); $userId = !empty($data['user']['id']) ? $data['user']['id'] : ''; //the same user already connected to current chat, need to close old connect if ($oldRid = $this->cm->isUserExistsInChat($userId, $chatId)) { $this->closeRequest($oldRid); } $this->cm->addUser($rid, $userId, $data['user']); $chat = $this->cm->findChat($chatId, $rid); $users = $chat->getUsers(); $joinedUser = $this->cm->getUserByRid($rid); $response = ['user' => $joinedUser, 'join' => true]; foreach ($users as $user) { //send message for other users of this chat if ($userId != $user->getId()) { $conn = $this->clients[$user->getRid()]; $conn->send(Json::encode(['type' => 'auth', 'data' => $response])); } } //send auth response for joined user $response = ['user' => $joinedUser, 'users' => $users, 'history' => $this->cm->getHistory($chat->getUid())]; $conn = $this->clients[$rid]; $conn->send(Json::encode(['type' => 'auth', 'data' => $response])); }