Passed
Push — master ( 1b6475...610123 )
by Petr
10:18
created

DriveFileTest::testThru()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 13
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\UploadPerPartes\Exceptions;
8
use kalanis\UploadPerPartes\InfoFormat;
9
use kalanis\UploadPerPartes\Uploader;
10
use Support;
11
12
13
class DriveFileTest extends CommonTestClass
14
{
15
    public function tearDown(): void
16
    {
17
        $driveFile = $this->getDriveFile();
18
        if ($driveFile->exists($this->mockKey())) {
19
            $driveFile->remove($this->mockKey());
20
        }
21
        parent::tearDown();
22
    }
23
24
    /**
25
     * @throws Exceptions\UploadException
26
     */
27
    public function testThru(): void
28
    {
29
        $driveFile = $this->getDriveFile();
30
        $this->assertTrue($driveFile->write($this->mockKey(), $this->mockData()));
31
        $data = $driveFile->read($this->mockKey());
32
        $this->assertInstanceOf(InfoFormat\Data::class, $data);
33
        $this->assertEquals('abcdef', $data->fileName);
34
        $this->assertEquals($this->getTestDir() . 'abcdef', $data->tempLocation);
35
        $this->assertEquals(123456, $data->fileSize);
36
        $this->assertEquals(12, $data->partsCount);
37
        $this->assertEquals(64, $data->bytesPerPart);
38
        $this->assertEquals(7, $data->lastKnownPart);
39
        $driveFile->remove($this->mockKey());
40
    }
41
42
    /**
43
     * @throws Exceptions\UploadException
44
     */
45
    public function testWriteFail(): void
46
    {
47
        $driveFile = $this->getDriveFile();
48
        $data = $this->mockData();
49
        $this->assertTrue($driveFile->write($this->mockKey(), $data));
50
        $this->assertTrue($driveFile->exists($this->mockKey()));
51
        $this->expectException(Exceptions\ContinuityUploadException::class);
52
        $driveFile->write($this->mockKey(), $data, true); // fail
53
        $this->expectExceptionMessageMatches('DRIVEFILE ALREADY EXISTS');
54
    }
55
56
    /**
57
     * @throws Exceptions\UploadException
58
     */
59
    public function testUpdate(): void
60
    {
61
        $driveFile = $this->getDriveFile();
62
        $data = $this->mockData();
63
        $this->assertTrue($driveFile->write($this->mockKey(), $data));
64
        $this->assertTrue($driveFile->updateLastPart($this->mockKey(), $data, $data->lastKnownPart + 1));
65
        $driveFile->remove($this->mockKey());
66
    }
67
68
    /**
69
     * @throws Exceptions\UploadException
70
     */
71
    public function testUpdateFail(): void
72
    {
73
        $driveFile = $this->getDriveFile();
74
        $data = $this->mockData();
75
        $this->assertTrue($driveFile->write($this->mockKey(), $data));
76
        $this->expectException(Exceptions\UploadException::class);
77
        $driveFile->updateLastPart($this->mockKey(), $data, $data->lastKnownPart + 5); // fail
78
        $this->expectExceptionMessageMatches('DRIVEFILE IS NOT CONTINUOUS');
79
    }
80
81
    protected function mockKey(): string
82
    {
83
        return 'fghjkl' . Uploader\TargetSearch::FILE_DRIVER_SUFF;
84
    }
85
86
    protected function getDriveFile(): Uploader\DriveFile
87
    {
88
        $lang = new Uploader\Translations();
89
        $storage = new Support\InfoRam($lang);
90
        $target = new Uploader\TargetSearch($storage, new Support\DataRam($lang), $lang);
91
        $key = new Support\Key($target, $lang);
92
        $format = new InfoFormat\Json();
93
        return new Uploader\DriveFile($storage, $format, $key, $lang);
94
    }
95
}
96