Completed
Pull Request — master (#594)
by Andreas
03:19
created

testFailedResumableTransferWillNotThrowAndReturnSameChunk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 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\FakeGraphApi\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