Passed
Pull Request — master (#888)
by Tobias
01:50
created

testResumableUploadCanStartTransferAndFinish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
namespace Facebook\Tests\FileUpload;
26
27
use Facebook\FileUpload\File;
28
use Facebook\Application;
29
use Facebook\Client;
30
use Facebook\FileUpload\ResumableUploader;
31
use Facebook\FileUpload\TransferChunk;
32
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload;
33
use PHPUnit\Framework\TestCase;
34
35
class ResumableUploaderTest extends TestCase
36
{
37
    /**
38
     * @var Application
39
     */
40
    private $fbApp;
41
42
    /**
43
     * @var Client
44
     */
45
    private $client;
46
47
    /**
48
     * @var FakeGraphApiForResumableUpload
49
     */
50
    private $graphApi;
51
52
    /**
53
     * @var File
54
     */
55
    private $file;
56
57
    protected function setUp()
58
    {
59
        $this->fbApp = new Application('app_id', 'app_secret');
60
        $this->graphApi = new FakeGraphApiForResumableUpload();
61
        $this->client = new Client($this->graphApi);
62
        $this->file = new File(__DIR__.'/../foo.txt');
63
    }
64
65
    public function testResumableUploadCanStartTransferAndFinish()
66
    {
67
        $uploader = new ResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
68
        $endpoint = '/me/videos';
69
        $chunk = $uploader->start($endpoint, $this->file);
70
        $this->assertInstanceOf(TransferChunk::class, $chunk);
71
        $this->assertEquals('42', $chunk->getUploadSessionId());
72
        $this->assertEquals('1337', $chunk->getVideoId());
73
74
        $newChunk = $uploader->transfer($endpoint, $chunk);
75
        $this->assertEquals(20, $newChunk->getStartOffset());
76
        $this->assertNotSame($newChunk, $chunk);
77
78
        $finalResponse = $uploader->finish($endpoint, $chunk->getUploadSessionId(), []);
79
        $this->assertTrue($finalResponse);
80
    }
81
82
    /**
83
     * @expectedException \Facebook\Exception\ResponseException
84
     */
85
    public function testStartWillLetErrorResponsesThrow()
86
    {
87
        $this->graphApi->failOnStart();
88
        $uploader = new ResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
89
90
        $uploader->start('/me/videos', $this->file);
91
    }
92
93
    public function testFailedResumableTransferWillNotThrowAndReturnSameChunk()
94
    {
95
        $this->graphApi->failOnTransfer();
96
        $uploader = new ResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
97
98
        $chunk = new TransferChunk($this->file, '1', '2', '3', '4');
99
        $newChunk = $uploader->transfer('/me/videos', $chunk);
100
        $this->assertSame($newChunk, $chunk);
101
    }
102
}
103