Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

AnswerMock::getResponseDirReal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 53
nc 1
nop 0
dl 0
loc 67
rs 9.0254
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ProtocolsTests\Fsp;
4
5
6
use CommonTestClass;
7
use kalanis\RemoteRequest\Connection;
8
use kalanis\RemoteRequest\Protocols\Fsp;
9
use kalanis\RemoteRequest\RequestException;
10
use kalanis\RemoteRequest\Translations;
11
12
13
class AnswerMock extends Connection\Processor
14
{
15
    public function getResponseVersion()
16
    {
17
        return Common::makeDummyString([
18
            0x10, # CC_VERSION
19
            0x31, # checksum
20
            0x01, 0x02, # key
21
            0x03, 0x04, # sequence
22
            0x00, 0x0A, # data_length
23
            0x00, 0x00, 0x00, 0x01, # position
24
            'Test v0.1', 0x00, # content
25
            0b01000100, # extra data - settings
26
        ]);
27
    }
28
29
    public function getResponseVersionPayload()
30
    {
31
        return Common::makeDummyQuery([
32
            0x10, # CC_VERSION
33
            0x94, # checksum
34
            0x01, 0x02, # key
35
            0x03, 0x04, # sequence
36
            0x00, 0x0A, # data_length
37
            0x00, 0x00, 0x00, 0x05, # position
38
            'Test v0.2', 0x00, # content
39
            0b10011100, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00 # extra data - settings
40
        ]);
41
    }
42
43
    public function getResponseDir()
44
    {
45
        return Common::makeDummyQuery([
46
            0x41, # CC_GET_DIR
47
            0x71, # checksum
48
            0x01, 0x02, # key
49
            0x03, 0x04, # sequence
50
            0x00, 0x55, # data_length
51
            0x00, 0x00, 0x00, 0x0C, # position
52
            # content...
53
            0x12, 0x34, 0x56, 0x78, # time
54
            0x00, 0x00, 0x04, 0x00, # size
55
            0x01,                   # type
56
            'foo-bar-baz.txt', 0x00, # filename - "foo-bar-baz.txt\0"
57
            0x00, 0x00, 0x00, # padding 28
58
59
            0x5e, 0xff, 0x3f, 0xe6, // time
60
            0x00, 0x00, 0x00, 0x1c, // size
61
            0x03, // link
62
            0x31, 0x35, 0x39, 0x33, 0x36, 0x37, 0x39, 0x34, 0x30, 0x38, 0x37, 0x38, 0x32, 0x2e, 0x6a, 0x70, 0x67, "\n", // 1593679408782.jpg
63
            0x31, 0x35, 0x39, 0x33, 0x36, 0x37, 0x37, 0x38, 0x34, 0x34, 0x37, 0x38, 0x39, 0x2e, 0x6a, 0x70, 0x67, 0x00, // 1593677844789.jpg
64
            0x00, 0x00, 0x00, // padding 48
65
66
            0x00, 0x00, 0x00, 0x00, // 0
67
            0x00, 0x00, 0x00, 0x00, // 0
68
            0x00, // type end 9 ; 85 databytes
69
        ]);
70
    }
71
72
    public function getResponseDirReal()
73
    {
74
        return Common::makeDummyQuery([
75
            0x41, # CC_GET_DIR
76
            0xd7, # checksum
77
            0x68, 0x7f, # key
78
            0x01, 0xd3, # sequence
79
            0x01, 0x01, # data length - 257
80
            0x00, 0x00, 0x00, 0x00, # position (seek)
81
82
            # content...
83
            0x5f, 0x01, 0xac, 0x71, // 1593945201
84
            0x00, 0x00, 0x10, 0x00, // 4096
85
            0x02, // dir
86
            0x2e, 0x2e, 0x00, // ..
87
            // no padding
88
89
            0x5e, 0xe7, 0x78, 0x2c, // 1592227884
90
            0x00, 0x05, 0x47, 0xf5, // 346101
91
            0x01, // file
92
            0x6f, 0x70, 0x65, 0x6e, 0x20, 0x6c, 0x65, 0x74, 0x74, 0x65, 0x72, 0x2e, 0x70, 0x6e, 0x67, 0x00, // open letter.pdf
93
            0x00, 0x00, 0x00, // padding
94
95
            0x5e, 0x44, 0x06, 0xc9, // 1581516489
96
            0x00, 0x01, 0x50, 0x6d, // 86125
97
            0x01, // file
98
            0x61, 0x73, 0x79, 0x6e, 0x63, 0x69, 0x6f, 0x2e, 0x70, 0x64, 0x66, 0x00, // asyncio.pdf
99
            0x00, 0x00, 0x00, // padding
100
101
            0x5e, 0xec, 0xec, 0x36, // 1592585270
102
            0x00, 0x01, 0x47, 0xf4, // 83956
103
            0x01, // file
104
            0x6d, 0x61, 0x6b, 0x6f, 0x74, 0x6f, 0x5f, 0x6b, 0x69, 0x6e, 0x6f, 0x5f, 0x62, 0x79, 0x5f, // makoto_kino_by_
105
            0x69, 0x73, 0x61, 0x63, 0x6b, 0x35, 0x30, 0x33, 0x5f, 0x64, 0x38, 0x74, 0x35, 0x31, 0x65, // isack503_d8t51e
106
            0x72, 0x2d, 0x66, 0x75, 0x6c, 0x6c, 0x76, 0x69, 0x65, 0x77, 0x2e, 0x6a, 0x70, 0x67, 0x00, // r-fullview.jpg ; 150
107
            0x00, 0x00, // padding
108
109
            0x5e, 0xec, 0xe8, 0xcb, // 1592584395
110
            0x00, 0x84, 0x9c, 0xb1, // 8690865
111
            0x01, // file
112
            0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x2d, 0x74, 0x68, 0x65, 0x2d, 0x77, 0x65, 0x64, 0x64, // before-the-wedd
113
            0x69, 0x6e, 0x67, 0x2d, 0x6d, 0x61, 0x6b, 0x6f, 0x74, 0x6f, 0x2d, 0x6b, 0x69, 0x6e, 0x6f, // ing-makoto-kino
114
            0x2d, 0x63, 0x68, 0x69, 0x62, 0x69, 0x63, 0x68, 0x69, 0x62, 0x69, 0x2d, 0x39, 0x70, 0x5a, // -chibichibi-9pZ
115
            0x57, 0x2e, 0x6a, 0x70, 0x67, 0x00, // W.jpg
116
            // no padding
117
118
            0x5e, 0xff, 0x3f, 0xe6, // 1593786342
119
            0x00, 0x04, 0x35, 0xc4, // 275908
120
            0x01, // file
121
            0x31, 0x35, 0x39, 0x33, 0x36, 0x37, 0x39, 0x34, 0x30, 0x38, 0x37, 0x38, 0x32, 0x2e, 0x6a, 0x70, 0x67, 0x00, // 1593679408782.jpg
122
            0x00, // padding
123
124
            0x5e, 0xff, 0x3f, 0x7e, // 1593786238
125
            0x00, 0x01, 0x7e, 0x3d, // 97853
126
            0x01, // file
127
            0x31, 0x35, 0x39, 0x33, 0x36, 0x37, 0x37, 0x38, 0x34, 0x34, 0x37, 0x38, 0x39, 0x2e, 0x6a, 0x70, 0x67, 0x00, // 1593677844789.jpg
128
            0x00, // padding
129
130
            0x5f, 0x01, 0xac, 0x71, // 1593945201
131
            0x00, 0x00, 0x10, 0x00, // 4096
132
            0x02, // dir
133
            0x2e, 0x00, // .
134
            0x00, // padding
135
136
            0x00, 0x00, 0x00, 0x00, // 0
137
            0x00, 0x00, 0x00, 0x00, // 0
138
            0x00, // type end; 257 databytes
139
        ]);
140
    }
141
142
    public function getResponseError()
143
    {
144
        return Common::makeDummyQuery([
145
            0x40, # CC_ERR
146
            0xD8, # checksum
147
            0x01, 0x02, # key
148
            0x03, 0x04, # sequence
149
            0x00, 0x10, # data_length
150
            0x00, 0x00, 0x00, 0x00, # position
151
            'Testing error 1', 0x00, # content
152
             # extra data - empty
153
        ]);
154
    }
155
156
    public function getResponseErrorDetails()
157
    {
158
        return Common::makeDummyQuery([
159
            0x40, # CC_ERR
160
            0x19, # checksum
161
            0x01, 0x02, # key
162
            0x03, 0x04, # sequence
163
            0x00, 0x10, # data_length
164
            0x00, 0x00, 0x00, 0x00, # position
165
            'Testing error 2', 0x00, # content
166
            0x3F, # extra data - code 63 - "?"
167
        ]);
168
    }
169
170
    public function getResponseTest()
171
    {
172
        return Common::makeDummyQuery([
173
            0x81, # CC_TEST
174
            0x8B, # checksum
175
            0x01, 0x02, # key
176
            0x03, 0x04, # sequence
177
            0x00, 0x00, # data_length
178
            0x00, 0x00, 0x00, 0x00, # position
179
            # no content
180
            # no extra data
181
        ]);
182
    }
183
184
    public function getResponseNothing()
185
    {
186
        return Common::makeDummyQuery([
187
            0x4A, # CC_BYE
188
            0x54, # checksum
189
            0x01, 0x02, # key
190
            0x03, 0x04, # sequence
191
            0x00, 0x00, # data_length
192
            0x00, 0x00, 0x00, 0x00, # position
193
            # no content
194
            # no extra data
195
        ]);
196
    }
197
198
    public function getResponseGetFile()
199
    {
200
        return Common::makeDummyQuery([
201
            0x42, # CC_GET_FILE
202
            0x4D, # checksum
203
            0x01, 0x02, # key
204
            0x03, 0x04, # sequence
205
            0x00, 0x0F, # data_length
206
            0x00, 0x00, 0x04, 0x00, # position
207
            'Testing data 1', 0x00, # content
208
            # no extra data
209
        ]);
210
    }
211
212
    public function getResponseUpload()
213
    {
214
        return Common::makeDummyQuery([
215
            0x43, # CC_UP_LOAD
216
            0x4F, # checksum
217
            0x01, 0x02, # key
218
            0x03, 0x04, # sequence
219
            0x00, 0x00, # data_length
220
            0x00, 0x00, 0x02, 0x00, # position
221
            # no content
222
            # no extra data
223
        ]);
224
    }
225
226
    public function getResponseProtection()
227
    {
228
        return Common::makeDummyQuery([
229
            0x47, # CC_GET_PRO
230
            0xEB, # checksum
231
            0x01, 0x02, # key
232
            0x03, 0x04, # sequence
233
            0x00, 0x08, # data_length
234
            0x00, 0x00, 0x00, 0x01, # position
235
            'foo/bar', 0x00, # content - directory "foo/bar"
236
            0b11100110, # no extra data
237
        ]);
238
    }
239
240
    public function getResponseStats()
241
    {
242
        return Common::makeDummyQuery([
243
            0x4D, # CC_STATS
244
            0x34, # checksum
245
            0x01, 0x02, # key
246
            0x03, 0x04, # sequence
247
            0x00, 0x09, # data_length
248
            0x00, 0x00, 0x00, 0x00, # position
249
            0x5E, 0x0B, 0xE1, 0x01,    0x00, 0x23, 0x44, 0x00,    0b00100000, # file stats - 2020-01-01T00:00:01+00:00, 2311168 B file
250
            # no extra data
251
        ]);
252
    }
253
}
254
255
class AnswerTest extends CommonTestClass
256
{
257
    /**
258
     * @throws RequestException
259
     */
260
    public function testAnswerVersion(): void
261
    {
262
        $lang = new Translations();
263
        $mock = new AnswerMock($lang);
264
        $read = new Fsp\Answer($lang);
265
//        $read->canDump = true;
266
        $read->setResponse($mock->getResponseVersion())->process();
267
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
268
        /** @var Fsp\Answer\Version $process */
269
        $this->assertEquals(258, $process->getDataClass()->getKey());
270
        $this->assertEquals(772, $process->getDataClass()->getSequence());
271
        $this->assertEquals('Test v0.1', $process->getVersion());
272
        $this->assertFalse($process->isServerLogging());
273
        $this->assertTrue($process->isReadOnly());
274
        $this->assertFalse($process->wantReverseLookup());
275
        $this->assertFalse($process->isPrivateMode());
276
        $this->assertTrue($process->acceptsExtra());
277
    }
278
279
    /**
280
     * @throws RequestException
281
     */
282
    public function testAnswerVersionPayload(): void
283
    {
284
        $lang = new Translations();
285
        $mock = new AnswerMock($lang);
286
        $read = new Fsp\Answer($lang);
287
        $read->setResponse($mock->getResponseVersionPayload())->process();
288
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
289
        /** @var Fsp\Answer\Version $process */
290
        $this->assertEquals('Test v0.2', $process->getVersion());
291
        $this->assertTrue($process->isServerLogging());
292
        $this->assertFalse($process->isReadOnly());
293
        $this->assertFalse($process->wantReverseLookup());
294
        $this->assertTrue($process->isPrivateMode());
295
        $this->assertTrue($process->acceptsExtra());
296
        $this->assertTrue($process->canThruControl());
297
        $this->assertEquals(1024, $process->thruControlMaxAllowed());
298
        $this->assertEquals(512, $process->thruControlMaxPayload());
299
    }
300
301
    /**
302
     * @throws RequestException
303
     */
304
    public function testAnswerDir(): void
305
    {
306
        $lang = new Translations();
307
        $mock = new AnswerMock($lang);
308
        $read = new Fsp\Answer($lang);
309
//        $read->canDump = true;
310
        $read->setResponse($mock->getResponseDir())->process();
311
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
312
        /** @var Fsp\Answer\GetDir $process */
313
        $files = $process->getFiles();
314
        $file = reset($files);
315
        /** @var Fsp\Answer\GetDir\FileInfo $file */
316
        $this->assertEquals('foo-bar-baz.txt', $file->getFileName());
317
        $this->assertEquals('', $file->getLinkTarget());
318
        $this->assertEquals('txt', $file->getExtension());
319
        $this->assertEquals(305419896, $file->getCTime());
320
        $this->assertEquals(305419896, $file->getMTime());
321
        $this->assertEquals(305419896, $file->getATime());
322
        $this->assertEquals(1024, $file->getSize());
323
        $this->assertEquals('file', $file->getType());
324
        $this->assertEquals(Fsp::RDTYPE_FILE, $file->getOrigType());
325
        $this->assertTrue($file->isReadable());
326
        $this->assertTrue($file->isWritable());
327
        $this->assertTrue($file->isFile());
328
        $this->assertFalse($file->isDir());
329
        $this->assertFalse($file->isLink());
330
        $this->assertFalse($file->isExecutable());
331
        $this->assertEquals(0, $file->getOwner());
332
        $this->assertEquals(0, $file->getGroup());
333
        $this->assertEquals(0, $file->getInode());
334
        $this->assertEquals(0666, $file->getPerms());
335
        $file = next($files);
336
        $file->setPath('foo/baz');
337
        $this->assertEquals('foo/baz', $file->getPath());
338
        $this->assertEquals('1593679408782.jpg', $file->getFileName());
339
        $this->assertEquals('1593677844789.jpg', $file->getLinkTarget());
340
        $this->assertEquals('foo/baz/1593679408782.jpg', $file->getPathname());
341
        $this->assertEquals('foo/baz/1593679408782.jpg', $file->getRealPath());
342
    }
343
344
    /**
345
     * @throws RequestException
346
     */
347
    public function testAnswerDirReal(): void
348
    {
349
        $lang = new Translations();
350
        $mock = new AnswerMock($lang);
351
        $read = new Fsp\Answer($lang);
352
        $read->setResponse($mock->getResponseDirReal())->process();
353
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
354
        /** @var Fsp\Answer\GetDir $process */
355
        $files = $process->getFiles();
356
        reset($files);
357
        $file = next($files);
358
        /** @var Fsp\Answer\GetDir\FileInfo $file */
359
        $this->assertEquals('open letter.png', $file->getFileName());
360
        $this->assertEquals(1592227884, $file->getMTime());
361
        $this->assertEquals(346101, $file->getSize());
362
        $this->assertFalse($file->isDir());
363
        $this->assertTrue($file->isFile());
364
        $file = next($files);
365
        $this->assertEquals('asyncio.pdf', $file->getFileName());
366
        $this->assertEquals(1581516489, $file->getMTime());
367
        $this->assertEquals(86125, $file->getSize());
368
        $this->assertFalse($file->isDir());
369
        $this->assertTrue($file->isFile());
370
        $file = next($files);
371
        $this->assertEquals('makoto_kino_by_isack503_d8t51er-fullview.jpg', $file->getFileName());
372
        $this->assertEquals(1592585270, $file->getMTime());
373
        $this->assertEquals(83956, $file->getSize());
374
        $this->assertFalse($file->isDir());
375
        $this->assertTrue($file->isFile());
376
    }
377
378
    /**
379
     * @throws RequestException
380
     */
381
    public function testAnswerError(): void
382
    {
383
        $lang = new Translations();
384
        $mock = new AnswerMock($lang);
385
        $read = new Fsp\Answer($lang);
386
//        $read->canDump = true;
387
        $read->setResponse($mock->getResponseError())->process();
388
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
389
        /** @var Fsp\Answer\Error $process */
390
        $this->assertEquals('Testing error 1', $process->getError()->getMessage());
391
        $this->assertEmpty($process->getError()->getCode());
392
    }
393
394
    /**
395
     * @throws RequestException
396
     */
397
    public function testAnswerErrorDetails(): void
398
    {
399
        $lang = new Translations();
400
        $mock = new AnswerMock($lang);
401
        $read = new Fsp\Answer($lang);
402
        $read->setResponse($mock->getResponseErrorDetails())->process();
403
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
404
        /** @var Fsp\Answer\Error $process */
405
        $this->assertEquals('Testing error 2', $process->getError()->getMessage());
406
        $this->assertEquals(63, $process->getError()->getCode());
407
    }
408
409
    /**
410
     * @throws RequestException
411
     */
412
    public function testAnswerErrorRun(): void
413
    {
414
        $lang = new Translations();
415
        $mock = new AnswerMock($lang);
416
        $read = new Fsp\Answer($lang);
417
        $read->setResponse($mock->getResponseError())->process();
418
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
419
        /** @var Fsp\Answer\Error $process */
420
        $this->expectException(RequestException::class);
421
        $process->setHardWay(true)->getError();
422
    }
423
424
    /**
425
     * @throws RequestException
426
     */
427
    public function testAnswerTest(): void
428
    {
429
        $lang = new Translations();
430
        $mock = new AnswerMock($lang);
431
        $read = new Fsp\Answer($lang);
432
        $read->setResponse($mock->getResponseTest())->process();
433
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
434
        /** @var Fsp\Answer\Test $process */
435
        $this->assertTrue(true); // nothing to do
436
    }
437
438
    /**
439
     * @throws RequestException
440
     */
441
    public function testAnswerNothing(): void
442
    {
443
        $lang = new Translations();
444
        $mock = new AnswerMock($lang);
445
        $read = new Fsp\Answer($lang);
446
        $read->setResponse($mock->getResponseNothing())->process();
447
        Fsp\Answer\AnswerFactory::getObject($read)->process();
448
        $this->assertTrue(true); // nothing to do
449
    }
450
451
    /**
452
     * @throws RequestException
453
     */
454
    public function testAnswerGetFile(): void
455
    {
456
        $lang = new Translations();
457
        $mock = new AnswerMock($lang);
458
        $read = new Fsp\Answer($lang);
459
        $read->setResponse($mock->getResponseGetFile())->process();
460
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
461
        /** @var Fsp\Answer\GetFile $process */
462
        $this->assertEquals('Testing data 1', $process->getContent());
463
        $this->assertEquals(1024, $process->getSeek());
464
    }
465
466
    /**
467
     * @throws RequestException
468
     */
469
    public function testAnswerUpload(): void
470
    {
471
        $lang = new Translations();
472
        $mock = new AnswerMock($lang);
473
        $read = new Fsp\Answer($lang);
474
//        $read->canDump = true;
475
        $read->setResponse($mock->getResponseUpload())->process();
476
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
477
        /** @var Fsp\Answer\Upload $process */
478
        $this->assertEquals(512, $process->getSeek());
479
    }
480
481
    /**
482
     * @throws RequestException
483
     */
484
    public function testAnswerProtection(): void
485
    {
486
        $lang = new Translations();
487
        $mock = new AnswerMock($lang);
488
        $read = new Fsp\Answer($lang);
489
        $read->setResponse($mock->getResponseProtection())->process();
490
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
491
        /** @var Fsp\Answer\Protection $process */
492
        $this->assertEquals('foo/bar', $process->getDirectory());
493
        $this->assertFalse($process->isMy());
494
        $this->assertTrue($process->canList());
495
        $this->assertFalse($process->canReadOnlyOwner());
496
        $this->assertTrue($process->canCreateFile());
497
        $this->assertTrue($process->canRenameFile());
498
        $this->assertTrue($process->canDeleteFile());
499
        $this->assertFalse($process->canCreateDir());
500
        $this->assertTrue($process->containsReadme());
501
    }
502
503
    /**
504
     * @throws RequestException
505
     */
506
    public function testAnswerStats(): void
507
    {
508
        $lang = new Translations();
509
        $mock = new AnswerMock($lang);
510
        $read = new Fsp\Answer($lang);
511
        $read->setResponse($mock->getResponseStats())->process();
512
        $process = Fsp\Answer\AnswerFactory::getObject($read)->process();
513
        /** @var Fsp\Answer\Stats $process */
514
        $this->assertEquals(1577836801, $process->getTime()); // 2020-01-01T00:00:01+00:00
515
        $this->assertEquals(2311168, $process->getSize());
516
        $this->assertEquals(32, $process->getType());
517
    }
518
}
519