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

UploadTest::testCancel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\UploadPerPartes\Exceptions;
8
use kalanis\UploadPerPartes\Interfaces;
9
use kalanis\UploadPerPartes\Response;
10
use kalanis\UploadPerPartes\Uploader;
11
use Support;
12
13
14
class UploadTest extends CommonTestClass
15
{
16
    /**
17
     * @throws Exceptions\UploadException
18
     */
19
    public function testSimpleUpload(): void
20
    {
21
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
22
        $content = file_get_contents($this->getTestFile()); // read test content into ram
23
        $maxSize = strlen($content);
24
25
        // step 1 - init driver
26
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
27
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
28
        $bytesPerPart = $result1->jsonSerialize()['partSize'];
29
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
30
        $this->assertEquals(1024, $bytesPerPart);
31
32
        // step 2 - send data
33
        for ($i = 0; $i * $bytesPerPart <= $maxSize; $i++) {
34
            $part = substr($content, $i * $bytesPerPart, $bytesPerPart);
35
            $result2 = $lib->upload($sharedKey, $part);
36
            $this->assertEquals(Response\UploadResponse::STATUS_OK, $result2->jsonSerialize()['status']);
37
        }
38
39
        // step 3 - close upload
40
        /** @var Response\DoneResponse $result3 */
41
        $target = $lib->getLibDriver()->read($sharedKey)->tempLocation;
42
        $result3 = $lib->done($sharedKey);
43
        $this->assertEquals(Response\DoneResponse::STATUS_OK, $result3->jsonSerialize()['status']);
44
45
        // check content
46
        $uploaded = $lib->getStorage()->/** @scrutinizer ignore-call */getAll($target);
47
        $this->assertGreaterThan(0, strlen($uploaded));
48
        $this->assertTrue($content == $uploaded);
49
    }
50
51
    /**
52
     * @throws Exceptions\UploadException
53
     */
54
    public function testStoppedUpload(): void
55
    {
56
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
57
        $content = file_get_contents($this->getTestFile()); // read test content into ram
58
        $maxSize = strlen($content);
59
60
        // step 1 - init driver
61
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
62
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
63
        $bytesPerPart = $result1->jsonSerialize()['partSize'];
64
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
65
        $this->assertEquals(1024, $bytesPerPart);
66
        $this->assertEquals(631, $result1->jsonSerialize()['totalParts']);
67
68
        // step 2 - send first part of data
69
        $limited = floor($maxSize / 2);
70
        for ($i = 0; $i * $bytesPerPart < $limited; $i++) {
71
            $part = substr($content, $i * $bytesPerPart, $bytesPerPart);
72
            $result2 = $lib->upload($sharedKey, $part);
73
            $this->assertEquals(Response\UploadResponse::STATUS_OK, $result2->jsonSerialize()['status']);
74
        }
75
76
        // step 3 - again from the beginning
77
        $result3 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
78
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result3->jsonSerialize()['status']);
79
        $bytesPerPart = $result3->jsonSerialize()['partSize'];
80
        $lastKnownPart = $result3->jsonSerialize()['lastKnownPart'];
81
        $sharedKey = $result3->jsonSerialize()['sharedKey']; // for this test it's zero care
82
        $this->assertEquals(316, $lastKnownPart); // NOT ZERO
83
        $this->assertEquals(1024, $bytesPerPart);
84
85
        // step 4 - check first part
86
        for ($i = 0; $i <= $lastKnownPart; $i++) {
87
            $part = substr($content, $i * $bytesPerPart, $bytesPerPart);
88
            $result4 = $lib->check($sharedKey, $i);
89
            $this->assertEquals(Response\UploadResponse::STATUS_OK, $result4->jsonSerialize()['status']);
90
            if (md5($part) != $result4->jsonSerialize()['checksum']) {
91
                // step 5 - truncate of failed part
92
                $result5 = $lib->truncateFrom($sharedKey, $i - 2);
93
                $this->assertEquals(Response\UploadResponse::STATUS_OK, $result5->jsonSerialize()['status']);
94
                break;
95
            } else {
96
                $this->assertEquals(md5($part), $result4->jsonSerialize()['checksum']);
97
            }
98
        }
99
        if (!isset($result5)) {
100
            $this->assertTrue(false, 'No results');
101
            return;
102
        }
103
        $lastKnownPart = $result5->jsonSerialize()['lastKnownPart'];
104
        $this->assertEquals(314, $lastKnownPart);
105
106
        // step 6 - send second part
107
        for ($i = $lastKnownPart; $i * $bytesPerPart <= $maxSize; $i++) {
108
            $part = substr($content, $i * $bytesPerPart, $bytesPerPart);
109
            $result6 = $lib->upload($sharedKey, $part);
110
            $this->assertEquals(Response\UploadResponse::STATUS_OK, $result6->jsonSerialize()['status']);
111
        }
112
113
        // step 7 - close upload
114
        /** @var Response\DoneResponse $result3 */
115
        $target = $lib->getLibDriver()->read($sharedKey)->tempLocation;
116
        $result7 = $lib->done($sharedKey);
117
        $this->assertEquals(Response\DoneResponse::STATUS_OK, $result7->jsonSerialize()['status']);
118
119
        // check content
120
        $uploaded = $lib->getStorage()->/** @scrutinizer ignore-call */getAll($target);
121
        $this->assertGreaterThan(0, strlen($uploaded));
122
        $this->assertTrue($content == $uploaded);
123
    }
124
125
    /**
126
     * @throws Exceptions\UploadException
127
     */
128
    public function testCancel(): void
129
    {
130
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
131
        $content = file_get_contents($this->getTestFile()); // read test content into ram
132
        $maxSize = strlen($content);
133
134
        // step 1 - init driver
135
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
136
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
137
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
138
139
        // step 2 - send data
140
        $result2 = $lib->upload($sharedKey, $content); // flush it all
141
        $this->assertEquals(Response\UploadResponse::STATUS_OK, $result2->jsonSerialize()['status']);
142
143
        // step 3 - cancel upload
144
        /** @var Response\CancelResponse $result3 */
145
        $target = $lib->getLibDriver()->read($sharedKey)->tempLocation;
146
        $result3 = $lib->cancel($sharedKey);
147
        $this->assertEquals(Response\CancelResponse::STATUS_OK, $result3->jsonSerialize()['status']);
148
149
        // check content
150
        $this->assertEmpty($lib->getStorage()->/** @scrutinizer ignore-call */getAll($target));
151
    }
152
153
    /**
154
     * @throws Exceptions\UploadException
155
     */
156
    public function testInitFail(): void
157
    {
158
        $lib = new UploaderMock();
159
        // init data - but there is failure
160
        $result1 = $lib->init('', '', 123456);
161
        $this->assertEquals(Response\InitResponse::STATUS_FAIL, $result1->jsonSerialize()['status']);
162
    }
163
164
    /**
165
     * @throws Exceptions\UploadException
166
     */
167
    public function testCheckFail(): void
168
    {
169
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
170
        $content = file_get_contents($this->getTestFile()); // read test content into ram
171
        $maxSize = strlen($content);
172
173
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
174
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
175
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
176
177
        // step 2 - check data - non existing segment
178
        $result2 = $lib->check($sharedKey, 35);
179
        $this->assertEquals(Response\UploadResponse::STATUS_FAIL, $result2->jsonSerialize()['status']);
180
181
        // step 3 - cancel upload
182
        /** @var Response\CancelResponse $result3 */
183
        $result3 = $lib->cancel($sharedKey);
184
        $this->assertEquals(Response\CancelResponse::STATUS_OK, $result3->jsonSerialize()['status']);
185
    }
186
187
    /**
188
     * @throws Exceptions\UploadException
189
     */
190
    public function testTruncateFail(): void
191
    {
192
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
193
        $content = file_get_contents($this->getTestFile()); // read test content into ram
194
        $maxSize = strlen($content);
195
196
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
197
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
198
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
199
200
        // step 2 - truncate data - non existing segment
201
        $result2 = $lib->truncateFrom($sharedKey, 35);
202
        $this->assertEquals(Response\UploadResponse::STATUS_FAIL, $result2->jsonSerialize()['status']);
203
204
        // step 3 - cancel upload
205
        /** @var Response\CancelResponse $result3 */
206
        $result3 = $lib->cancel($sharedKey);
207
        $this->assertEquals(Response\CancelResponse::STATUS_OK, $result3->jsonSerialize()['status']);
208
    }
209
210
    /**
211
     * @throws Exceptions\UploadException
212
     */
213
    public function testUploadFail(): void
214
    {
215
        $lib = new UploaderMock(); // must stay same, because it's only in the ram
216
        $content = file_get_contents($this->getTestFile()); // read test content into ram
217
        $maxSize = strlen($content);
218
219
        $result1 = $lib->init($this->getTestDir(), 'lorem-ipsum.txt', $maxSize);
220
        $this->assertEquals(Response\InitResponse::STATUS_OK, $result1->jsonSerialize()['status']);
221
        $sharedKey = $result1->jsonSerialize()['sharedKey']; // for this test it's zero care
222
223
        // step 2 - upload data - not continuous
224
        $result2 = $lib->upload($sharedKey, Support\Strings::substr($content, 23, 47564), 66);
225
        $this->assertEquals(Response\UploadResponse::STATUS_FAIL, $result2->jsonSerialize()['status']);
226
227
        // step 3 - cancel upload
228
        /** @var Response\CancelResponse $result3 */
229
        $result3 = $lib->cancel($sharedKey);
230
        $this->assertEquals(Response\CancelResponse::STATUS_OK, $result3->jsonSerialize()['status']);
231
    }
232
233
    /**
234
     * @throws Exceptions\UploadException
235
     */
236
    public function testCancelFail(): void
237
    {
238
        $lib = new UploaderMock();
239
        // cancel data - but there is nothing
240
        $result2 = $lib->cancel('qwertzuiop');
241
        $this->assertEquals(Response\CancelResponse::STATUS_FAIL, $result2->jsonSerialize()['status']);
242
    }
243
244
    /**
245
     * @throws Exceptions\UploadException
246
     */
247
    public function testDoneFail(): void
248
    {
249
        $lib = new UploaderMock();
250
        // done data - but there is nothing
251
        $result2 = $lib->done('qwertzuiop');
252
        $this->assertEquals(Response\DoneResponse::STATUS_FAIL, $result2->jsonSerialize()['status']);
253
    }
254
}
255
256
257
class UploaderMock extends Uploader
258
{
259
    protected function getInfoStorage(?Interfaces\IUPPTranslations $lang = null): Interfaces\IInfoStorage
260
    {
261
        parent::getInfoStorage($lang);
262
        return new Support\InfoRam($lang);
263
    }
264
265
    protected function getDataStorage(?Interfaces\IUPPTranslations $lang = null): Interfaces\IDataStorage
266
    {
267
        parent::getDataStorage($lang);
268
        return new Support\DataRam($lang);
269
    }
270
271
    protected function getCalc(): Uploader\Calculates
272
    {
273
        parent::getCalc();
274
        return new Uploader\Calculates(1024);
275
    }
276
277
    /**
278
     * @return Support\DataRam
279
     */
280
    public function getStorage(): Interfaces\IDataStorage
281
    {
282
        return $this->dataStorage;
283
    }
284
285
    public function getLibDriver(): Uploader\DriveFile
286
    {
287
        return $this->driver;
288
    }
289
}
290