summaryrefslogtreecommitdiff
path: root/lib/facebook-graph-sdk/tests/Fixtures/FakeGraphApiForResumableUpload.php
blob: 1c2d0f875c6bb603a98bd17c65a6f04c126e7fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
 * Copyright 2017 Facebook, Inc.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
 * shall be included in all copies or substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

namespace Facebook\Tests\Fixtures;

use Facebook\Http\GraphRawResponse;
use Facebook\HttpClients\FacebookHttpClientInterface;

class FakeGraphApiForResumableUpload implements FacebookHttpClientInterface
{
    public $transferCount = 0;
    private $respondWith = 'SUCCESS';

    public function failOnStart()
    {
        $this->respondWith = 'FAIL_ON_START';
    }

    public function failOnTransfer()
    {
        $this->respondWith = 'FAIL_ON_TRANSFER';
    }

    public function send($url, $method, $body, array $headers, $timeOut)
    {
        // Could be start, transfer or finish
        if (strpos($body, 'transfer') !== false) {
            return $this->respondTransfer();
        } elseif (strpos($body, 'finish') !== false) {
            return $this->respondFinish();
        }

        return $this->respondStart();
    }

    private function respondStart()
    {
        if ($this->respondWith == 'FAIL_ON_START') {
            return new GraphRawResponse(
                "HTTP/1.1 500 OK\r\nFoo: Bar",
                '{"error":{"message":"Error validating access token: Session has expired on Monday, ' .
                '10-Aug-15 01:00:00 PDT. The current time is Monday, 10-Aug-15 01:14:23 PDT.",' .
                '"type":"OAuthException","code":190,"error_subcode":463}}'
            );
        }

        return new GraphRawResponse(
            "HTTP/1.1 200 OK\r\nFoo: Bar",
            '{"video_id":"1337","start_offset":"0","end_offset":"20","upload_session_id":"42"}'
        );
    }

    private function respondTransfer()
    {
        if ($this->respondWith == 'FAIL_ON_TRANSFER') {
            return new GraphRawResponse(
                "HTTP/1.1 500 OK\r\nFoo: Bar",
                '{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",' .
                '"type":"FacebookApiException","code":6000,"error_subcode":1363019}}'
            );
        }

        switch ($this->transferCount) {
            case 0:
                $data = ['start_offset' => 20, 'end_offset' => 40];
                break;
            case 1:
                $data = ['start_offset' => 40, 'end_offset' => 50];
                break;
            default:
                $data = ['start_offset' => 50, 'end_offset' => 50];
                break;
        }

        $this->transferCount++;

        return new GraphRawResponse(
            "HTTP/1.1 200 OK\r\nFoo: Bar",
            json_encode($data)
        );
    }

    private function respondFinish()
    {
        return new GraphRawResponse(
            "HTTP/1.1 200 OK\r\nFoo: Bar",
            '{"success":true}'
        );
    }
}