/
test_chat.php
130 lines (104 loc) · 2.59 KB
/
test_chat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
require_once('RR_PHPUnit_TestCase.class.php');
require_once('chat.php');
class MyChatRequest extends FunctionalChatRequest
{
/**
* @return Either([String]) Validated and possibly modified request data
*/
static function validate(array $params)
{
return self::validateFromList(array('chatter', 'room', 'message'), $params);
}
static function fromValidParams(array $params)
{
return new MyChatRequest(
$params['chatter'],
$params['room'],
$params['message']
);
}
}
class PhpFunctionalChatTest extends RR_PHPUnit_TestCase
{
function setUp()
{
$this->postModule = FunctionalChatPostModule::standard();
$this->settings = new FunctionalChatSettings(
$this->postModule,
__DIR__.'/test_chat.json',
__DIR__.'/test_chat.lock',
10
);
$this->params1 = array(
'room' => 'myplace',
'chatter' => 'Joe',
'message' => 'Test message'
);
$this->now = $_SERVER['REQUEST_TIME'];
$this->post1 = new FunctionalChatPost(
'Ramona',
'myroom',
'Hello',
$this->now
);
$this->chat = new PhpFunctionalChat();
$request1_e = MyChatRequest::fromParams($this->params1);
if ($request1_e->isRight()) {
$this->request1 = $request1_e->fromRight();
}
}
function testValidateRequest()
{
$x = MyChatRequest::validate($this->params1);
$this->instanceOf('Either', $x);
}
function testPostFromRequest()
{
$fillPost = $this->postModule->fromRequest;
$x = $fillPost($this->request1, time());
$this->instanceOf('Either', $x);
$this->ok($x->isRight());
}
function testMain()
{
$x = $this->chat->receivePostIO($this->request1, $this->settings);
$this->instanceOf('Either', $x);
$this->ok($x->isRight());
}
function testRetainMessage()
{
$this->post1->time = $this->now - 30;
$this->not($this->chat->retainMessage($this->post1));
$this->post1->time = $this->now;
$this->ok($this->chat->retainMessage($this->post1));
}
function testPostToKeep()
{
$posts = $this->getPosts(10);
$filtered = $this->chat->postsToKeep($posts, 8);
$this->equals(5, count($filtered));
}
function testPostToWrite()
{
$posts = $this->getPosts(10);
$outgoing = $this->chat->postsToWrite($posts, $this->post1, 8);
$this->equals(6, count($outgoing));
}
private function getPosts($n)
{
$alphabet = range('a', 'z');
$ps = array();
for ($i = 0; $i < $n; $i++) {
$p = new stdClass();
$ps []= new FunctionalChatPost(
array_rand($alphabet, 6),
'myroom',
array_rand($alphabet, 12),
$this->now - $i,
$n - $i
);
}
return $ps;
}
}