Passed
Pull Request — 5.x (#1106)
by
unknown
02:44
created

testFailedClientRequestWillNotThrowAndReturnSameChunk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
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\FacebookFile;
28
use Facebook\FacebookApp;
29
use Facebook\FacebookClient;
30
use Facebook\FileUpload\FacebookResumableUploader;
31
use Facebook\FileUpload\FacebookTransferChunk;
32
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload;
33
34
class FacebookResumableUploaderTest extends \PHPUnit_Framework_TestCase
35
{
36
    /**
37
     * @var FacebookApp
38
     */
39
    private $fbApp;
40
41
    /**
42
     * @var FacebookClient
43
     */
44
    private $client;
45
46
    /**
47
     * @var FakeGraphApiForResumableUpload
48
     */
49
    private $graphApi;
50
51
    /**
52
     * @var FacebookFile
53
     */
54
    private $file;
55
56
    protected function setUp()
57
    {
58
        $this->fbApp = new FacebookApp('app_id', 'app_secret');
59
        $this->graphApi = new FakeGraphApiForResumableUpload();
60
        $this->client = new FacebookClient($this->graphApi);
61
        $this->file = new FacebookFile(__DIR__.'/../foo.txt');
62
    }
63
64
    public function testResumableUploadCanStartTransferAndFinish()
65
    {
66
        $uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
67
        $endpoint = '/me/videos';
68
        $chunk = $uploader->start($endpoint, $this->file);
69
        $this->assertInstanceOf('Facebook\FileUpload\FacebookTransferChunk', $chunk);
70
        $this->assertEquals('42', $chunk->getUploadSessionId());
71
        $this->assertEquals('1337', $chunk->getVideoId());
72
73
        $newChunk = $uploader->transfer($endpoint, $chunk);
74
        $this->assertEquals(20, $newChunk->getStartOffset());
75
        $this->assertNotSame($newChunk, $chunk);
76
77
        $finalResponse = $uploader->finish($endpoint, $chunk->getUploadSessionId(), []);
78
        $this->assertTrue($finalResponse);
79
    }
80
81
    /**
82
     * @expectedException \Facebook\Exceptions\FacebookResponseException
83
     */
84
    public function testStartWillLetErrorResponsesThrow()
85
    {
86
        $this->graphApi->failOnStart();
87
        $uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
88
89
        $uploader->start('/me/videos', $this->file);
90
    }
91
92
    public function testFailedResumableTransferWillNotThrowAndReturnSameChunk()
93
    {
94
        $this->graphApi->failOnTransfer();
95
        $uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
96
97
        $chunk = new FacebookTransferChunk($this->file, '1', '2', '3', '4');
98
        $newChunk = $uploader->transfer('/me/videos', $chunk);
99
        $this->assertSame($newChunk, $chunk);
100
    }
101
102
    public function testFailedResumableTransferWillNotThrowAndReturnNewChunk()
103
    {
104
        $this->graphApi->failOnTransferAndUploadNewChunk();
105
        $uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
106
107
        $chunk = new FacebookTransferChunk($this->file, '1', '2', '3', '4');
108
        $newChunk = $uploader->transfer('/me/videos', $chunk);
109
        $this->assertEquals(40, $newChunk->getStartOffset());
110
        $this->assertEquals(50, $newChunk->getEndOffset());
111
    }
112
113
    public function testFailedClientRequestWillNotThrowAndReturnSameChunk()
114
    {
115
        $this->graphApi->failOnClientRequest();
116
        $uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');
117
118
        $chunk = new FacebookTransferChunk($this->file, '1', '2', '3', '4');
119
        $newChunk = $uploader->transfer('/me/videos', $chunk);
120
        $this->assertSame($newChunk, $chunk);
121
    }
122
}
123