1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tests\codeception\unit; |
4
|
|
|
|
5
|
|
|
use yii\codeception\TestCase; |
6
|
|
|
use jones\wschat\components\ChatManager; |
7
|
|
|
|
8
|
|
|
class ChatTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** @var \UnitTester */ |
11
|
|
|
protected $tester; |
12
|
|
|
/** @var \jones\wschat\components\ChatManager */ |
13
|
|
|
protected $cm; |
14
|
|
|
protected $rid = 1; |
15
|
|
|
protected $userId = 1; |
16
|
|
|
protected $chatId = 1; |
17
|
|
|
|
18
|
|
|
protected function _before() |
19
|
|
|
{ |
20
|
|
|
$this->cm = new ChatManager(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function _after() |
24
|
|
|
{ |
25
|
|
|
unset($this->cm); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAddUser() |
29
|
|
|
{ |
30
|
|
|
$this->cm->addUser($this->rid, $this->userId); |
31
|
|
|
$user = $this->cm->getUserByRid($this->rid); |
32
|
|
|
$this->assertInstanceOf('jones\wschat\components\User', $user, |
33
|
|
|
'User should be instance of jones\wschat\components\User'); |
34
|
|
|
$this->assertEquals($this->userId, $user->getId(), 'User id should match'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @depends testAddUser |
39
|
|
|
*/ |
40
|
|
|
public function testFindChat() |
41
|
|
|
{ |
42
|
|
|
$this->cm->addUser($this->rid, $this->userId); |
43
|
|
|
$chat = $this->cm->findChat($this->chatId, $this->rid); |
44
|
|
|
$this->assertInstanceOf('jones\wschat\components\ChatRoom', $chat, |
45
|
|
|
'Chat should be instance of jones\wschat\ChatRoom'); |
46
|
|
|
$this->assertEquals($this->chatId, $chat->getUid(), 'Chat id should match'); |
47
|
|
|
$users = $chat->getUsers(); |
48
|
|
|
$this->assertEquals(1, sizeof($users), 'We added only one user'); |
49
|
|
|
$rid = rand(1, 100); |
50
|
|
|
$this->cm->addUser($rid, 2); |
51
|
|
|
$chat = $this->cm->findChat($this->chatId, $rid); |
52
|
|
|
$this->assertEquals(2, sizeof($chat->getUsers()), 'We added second user'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testUsersExistsInChat() |
56
|
|
|
{ |
57
|
|
|
$this->cm->addUser($this->rid, $this->userId); |
58
|
|
|
$this->assertFalse((boolean)$this->cm->isUserExistsInChat($this->userId, $this->chatId), |
59
|
|
|
'User should not be added to chat'); |
60
|
|
|
$this->cm->findChat($this->chatId, $this->rid); |
61
|
|
|
$rid = $this->cm->isUserExistsInChat($this->userId, $this->chatId); |
62
|
|
|
$this->assertTrue((boolean)$rid, 'User should be in chat'); |
63
|
|
|
$this->assertEquals($this->rid, $rid, 'Resource id should match'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testRemoveUserFromChat() |
67
|
|
|
{ |
68
|
|
|
$this->cm->addUser($this->rid, $this->userId); |
69
|
|
|
$chat = $this->cm->findChat($this->chatId, $this->rid); |
70
|
|
|
$this->cm->removeUserFromChat($this->rid); |
71
|
|
|
$this->assertEquals(0, sizeof($chat->getUsers()), 'Chat should not contains any users'); |
72
|
|
|
} |
73
|
|
|
} |