Completed
Push — master ( a51d1e...202aeb )
by Pedro
02:02
created

BotParserTest::testPlayers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ApplicationTest\Service;
4
5
use Zend\Stdlib\ArrayUtils;
6
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
7
use Application\Service\BotParser;
8
9
class BotParserTest extends AbstractHttpControllerTestCase
10
{
11
    public function setUp()
12
    {
13
        // The module configuration should still be applicable for tests.
14
        // You can override configuration here with test case specific values,
15
        // such as sample view templates, path stacks, module_listener_options,
16
        // etc.
17
        $configOverrides = [];
18
19
        $this->setApplicationConfig(ArrayUtils::merge(
20
            include __DIR__ . '/../../../../config/application.config.php',
21
            $configOverrides
22
        ));
23
24
          parent::setUp();
25
    }
26
27
    public function testPositionQuest()
28
    {
29
        $testcase = 'questinfo-position.txt';
30
        $config = [
31
            'bot_quest' => __DIR__ . '/../testcases/' . $testcase,
32
        ];
33
34
        $parser = new BotParser($config);
35
        $db = $parser->getQuestData();
36
37
        // Title
38
        $this->assertEquals(
39
            'lay waste to the Towers of Ankh-Allor, wherein lies the terrible sorceror Croocq',
40
            $db['title']
41
        );
42
43
        // Type
44
        $this->assertEquals(2, $db['type']);
45
46
        // Current stage
47
        $this->assertEquals(1, $db['objective']);
48
49
        // Number of stages
50
        $this->assertEquals(2, count($db['stages']));
51
52
        // Stage 1
53
        $this->assertSame([
54
            'x_pos' => 225,
55
            'y_pos' => 315,
56
            'color' => '#d30000',
57
        ], $db['stages'][0]);
58
59
        // Stage 2
60
        $this->assertSame([
61
            'x_pos' => 280,
62
            'y_pos' => 360,
63
            'color' => '#d30000',
64
        ], $db['stages'][1]);
65
66
        // Number of players
67
        $this->assertEquals(4, count($db['players']));
68
69
        // Player 1
70
        $this->assertSame([
71
            'nick'  => 'Orange',
72
            'x_pos' => 195,
73
            'y_pos' => 315,
74
            'color' => '#0080e1',
75
        ], $db['players'][0]);
76
77
        // Player 2
78
        $this->assertSame([
79
            'nick'  => 'pi',
80
            'x_pos' => 225,
81
            'y_pos' => 270,
82
            'color' => '#0080e1',
83
        ], $db['players'][1]);
84
85
        // Player 3
86
        $this->assertSame([
87
            'nick'  => 'BernardoRafael',
88
            'x_pos' => 225,
89
            'y_pos' => 315,
90
            'color' => '#0080e1',
91
        ], $db['players'][2]);
92
93
        // Player 4
94
        $this->assertSame([
95
            'nick'  => 'pirilampo',
96
            'x_pos' => 225,
97
            'y_pos' => 315,
98
            'color' => '#0080e1',
99
        ], $db['players'][3]);
100
    }
101
102
    public function testTimeQuest()
103
    {
104
        $testcase = 'questinfo-time.txt';
105
        $config = [
106
            'bot_quest' => __DIR__ . '/../testcases/' . $testcase,
107
        ];
108
109
        $parser = new BotParser($config);
110
        $db = $parser->getQuestData();
111
112
        // Title
113
        $this->assertEquals(
114
            'locate the herbs and brew the elixir to rid the realm of the Normonic Plague',
115
            $db['title']
116
        );
117
118
        // Type
119
        $this->assertEquals(1, $db['type']);
120
121
        // Current stage
122
        $this->assertEquals(1531325222, $db['objective_num']);
123
124
        // Number of players
125
        $this->assertEquals(4, count($db['players']));
126
127
        $this->assertEquals('RichardStallman', $db['players'][0]['nick']);
128
        $this->assertEquals('xhip', $db['players'][1]['nick']);
129
        $this->assertEquals('Infernvs', $db['players'][2]['nick']);
130
        $this->assertEquals('.hack', $db['players'][3]['nick']);
131
    }
132
133
    public function testScoreboard()
134
    {
135
        $testcase = 'irpg-scoreboard.db';
136
        $config = [
137
            'bot_db' => __DIR__ . '/../testcases/' . $testcase,
138
        ];
139
140
        $parser = new BotParser($config);
141
        $scoreboard = $parser->getScoreboard();
142
143
        // Test the sorting method
144
        // flcl and fALSO have the same level, but flcl should be first
145
        // because he has a lower ttl
146
        $this->assertEquals('flcl', $scoreboard[0]['nick']);
147
        $this->assertEquals('fALSO', $scoreboard[1]['nick']);
148
149
        // Number of players
150
        $this->assertEquals(12, count($scoreboard));
151
152
        // Full data of the first player
153
        $this->assertSame([
154
            'nick'    => 'flcl',
155
            'level'   => 43,
156
            'class'   => 'gordo',
157
            'ttl'     => '3 days 1 hour',
158
            'ttl_num' => 264103,
159
            'status'  => true
160
        ], $scoreboard[0]);
161
162
        // Full data of the last player
163
        $this->assertSame([
164
            'nick'    => 'MalMen',
165
            'level'   => 26,
166
            'class'   => 'amen',
167
            'ttl'     => '1 hour 40 minutes',
168
            'ttl_num' => 6031,
169
            'status'  => true
170
        ], $scoreboard[11]);
171
    }
172
173
    public function testDatabase()
174
    {
175
        $testcase = 'irpg-scoreboard.db';
176
        $config = [
177
            'bot_db' => __DIR__ . '/../testcases/' . $testcase,
178
        ];
179
180
        $parser = new BotParser($config);
181
182
        $db = $parser->getDatabase();
183
184
        // Number of players
185
        $this->assertEquals(12, count($db));
186
187
        // Test for invalid player
188
        $this->assertEquals(0, $parser->getDatabase('ZBR123'));
189
190
        $expectedUser = [
191
            'nick' => 'fALSO',
192
            'level' => 43,
193
            'admin' => 'No',
194
            'class' => 'GLORIOSO',
195
            'ttl' => [
196
                'display' => '3 days 12 hours',
197
                'numeric' => 304376,
198
            ],
199
            'nick_host' => 'secret!secret@secret',
200
            'online' => 'Yes',
201
            'idled' => [
202
                'display' => '2 weeks 5 days',
203
                'numeric' => 1675484,
204
            ],
205
            'x_pos' => 259,
206
            'y_pos' => 390,
207
            'msg_pen' => [
208
                'display' => '10 minutes 3 seconds',
209
                'numeric' => 603,
210
            ],
211
            'nick_pen' => [
212
                'display' => 'None',
213
                'numeric' => 0,
214
            ],
215
            'part_pen' => [
216
                'display' => 'None',
217
                'numeric' => 0,
218
            ],
219
            'kick_pen' => [
220
                'display' => 'None',
221
                'numeric' => 0,
222
            ],
223
            'quit_pen' => [
224
                'display' => 'None',
225
                'numeric' => 0,
226
            ],
227
            'quest_pen' => [
228
                'display' => 'None',
229
                'numeric' => 0,
230
            ],
231
            'logout_pen' => [
232
                'display' => 'None',
233
                'numeric' => 0,
234
            ],
235
            'total_pen' => [
236
                'display' => '10 minutes 3 seconds',
237
                'numeric' => 603,
238
            ],
239
            'created' => [
240
                'display' => '2018-06-20 22:58:00',
241
                'numeric' => 1529535480,
242
            ],
243
            'last_login' => [
244
                'display' => '2018-06-28 13:35:35',
245
                'numeric' => 1530192935,
246
            ],
247
            'amulet' => [
248
                'display' => '43',
249
                'numeric' => 43,
250
                'unique' => null,
251
            ],
252
            'charm' => [
253
                'display' => '40',
254
                'numeric' => 40,
255
                'unique' => null,
256
            ],
257
            'helm' => [
258
                'display' => '23',
259
                'numeric' => 23,
260
                'unique' => null,
261
            ],
262
            'boots' => [
263
                'display' => '27',
264
                'numeric' => 27,
265
                'unique' => null,
266
            ],
267
            'gloves' => [
268
                'display' => '30',
269
                'numeric' => 30,
270
                'unique' => null,
271
            ],
272
            'ring' => [
273
                'display' => '52h',
274
                'numeric' => 52,
275
                'unique' => 'Juliet\'s Glorious Ring of Sparkliness',
276
            ],
277
            'leggings' => [
278
                'display' => '21',
279
                'numeric' => 21,
280
                'unique' => null,
281
            ],
282
            'shield' => [
283
                'display' => '43',
284
                'numeric' => 43,
285
                'unique' => null,
286
            ],
287
            'tunic' => [
288
                'display' => '55',
289
                'numeric' => 55,
290
                'unique' => null,
291
            ],
292
            'weapon' => [
293
                'display' => '46',
294
                'numeric' => 46,
295
                'unique' => null,
296
            ],
297
            'sum' => 380,
298
            'alignment' => 'Neutral',
299
        ];
300
301
        $this->assertSame($expectedUser, $parser->getDatabase('fALSO'));
302
    }
303
304
    public function testPlayers()
305
    {
306
        $testcase = 'irpg-scoreboard.db';
307
        $config = [
308
            'bot_db' => __DIR__ . '/../testcases/' . $testcase,
309
        ];
310
311
        $parser = new BotParser($config);
312
313
        $expectedPlayers = [
314
            'Abram',
315
            'Bergalho',
316
            'Sir_MaD',
317
            'Infernvs',
318
            'BernardoRafael',
319
            'pirilampo',
320
            'fALSO',
321
            'Orange',
322
            'flcl',
323
            'Weasel',
324
            'pi',
325
            'MalMen',
326
        ];
327
328
        $players = $parser->getPlayers();
329
330
        // Number of players
331
        $this->assertEquals(12, count($players));
332
333
        // Test full output
334
        $this->assertSame($expectedPlayers, $players);
335
    }
336
337
    public function testItems()
338
    {
339
        $testcase = 'mapitems.db';
340
        $config = [
341
            'bot_item' => __DIR__ . '/../testcases/' . $testcase,
342
        ];
343
344
        $parser = new BotParser($config);
345
346
        $expectedItems = [
347
            [
348
                'x_pos' => 407,
349
                'y_pos' => 380,
350
                'type' => 'charm',
351
                'level' => '20',
352
                'age' => 792,
353
                'color' => '#ff8000',
354
            ],
355
            [
356
                'x_pos' => 274,
357
                'y_pos' => 125,
358
                'type' => 'shield',
359
                'level' => '35',
360
                'age' => 3345,
361
                'color' => '#ff8000',
362
            ],
363
            [
364
                'x_pos' => 293,
365
                'y_pos' => 396,
366
                'type' => 'shield',
367
                'level' => '2',
368
                'age' => 0,
369
                'color' => '#ff8000',
370
            ],
371
            [
372
                'x_pos' => 50,
373
                'y_pos' => 350,
374
                'type' => 'weapon',
375
                'level' => '85',
376
                'age' => 258753,
377
                'color' => '#ff8000',
378
            ],
379
        ];
380
381
        // Test full output
382
        $this->assertSame($expectedItems, $parser->getItems());
383
    }
384
385
    public function testCoordinates()
386
    {
387
        $bot_db = 'irpg-scoreboard.db';
388
        $bot_item = 'mapitems.db';
389
        $config = [
390
            'bot_db' => __DIR__ . '/../testcases/' . $bot_db,
391
            'bot_item' => __DIR__ . '/../testcases/' . $bot_item,
392
        ];
393
394
        $parser = new BotParser($config);
395
396
        $coordinates = $parser->getCoordinates();
397
398
        // Test for total - 12 players plus 4 items
399
        $this->assertEquals(12 + 4, count($coordinates));
400
401
        // Compare first player
402
        $this->assertSame([
403
            'x' => 267,
404
            'y' => 306,
405
            'text' => 'Abram',
406
            'color' => '#0080e1',
407
        ], $coordinates[0]);
408
    }
409
410
    public function testEvents()
411
    {
412
        $testcase = 'modifiers.txt';
413
        $config = [
414
            'bot_mod' => __DIR__ . '/../testcases/' . $testcase,
415
        ];
416
417
        $parser = new BotParser($config);
418
419
        $events = $parser->getEvents(0);
420
421
        // Test total events
422
        $this->assertEquals(100, count($events['items']));
423
424
        // Test total
425
        $this->assertEquals(100, $events['total']);
426
427
        $events = $parser->getEvents(0, 'fAGSO');
428
429
        // Test all events
430
        $this->assertEquals(13, count($events['items']));
431
432
        // Test total
433
        $this->assertEquals(13, $events['total']);
434
435
        // Test first event
436
        $this->assertEquals(
437
            "[06/22/18 09:03:15] fAGSO has fallen ill with the black plague. " .
438
                     "This terrible calamity has slowed them 0 days, 10:51:59 from level 81.",
439
            $events['items'][0]
440
        );
441
442
        // Test for invalid player
443
        $this->assertSame([
444
            'items' => [],
445
            'total' => 0,
446
        ], $parser->getEvents(0, 'ZBR123'));
447
    }
448
}
449