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

RequestTest::testAMissingMethodWillThrow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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
namespace Facebook\Tests;
25
26
use Facebook\Application;
27
use Facebook\Request;
28
use Facebook\FileUpload\File;
29
use Facebook\FileUpload\Video;
30
use PHPUnit\Framework\TestCase;
31
32
class RequestTest extends TestCase
33
{
34
    public function testAnEmptyRequestEntityCanInstantiate()
35
    {
36
        $app = new Application('123', 'foo_secret');
37
        $request = new Request($app);
38
39
        $this->assertInstanceOf(Request::class, $request);
40
    }
41
42
    /**
43
     * @expectedException \Facebook\Exception\SDKException
44
     */
45
    public function testAMissingAccessTokenWillThrow()
46
    {
47
        $app = new Application('123', 'foo_secret');
48
        $request = new Request($app);
49
50
        $request->validateAccessToken();
51
    }
52
53
    /**
54
     * @expectedException \Facebook\Exception\SDKException
55
     */
56
    public function testAMissingMethodWillThrow()
57
    {
58
        $app = new Application('123', 'foo_secret');
59
        $request = new Request($app);
60
61
        $request->validateMethod();
62
    }
63
64
    /**
65
     * @expectedException \Facebook\Exception\SDKException
66
     */
67
    public function testAnInvalidMethodWillThrow()
68
    {
69
        $app = new Application('123', 'foo_secret');
70
        $request = new Request($app, 'foo_token', 'FOO');
71
72
        $request->validateMethod();
73
    }
74
75
    public function testGetHeadersWillAutoAppendETag()
76
    {
77
        $app = new Application('123', 'foo_secret');
78
        $request = new Request($app, null, 'GET', '/foo', [], 'fooETag');
79
80
        $headers = $request->getHeaders();
81
82
        $expectedHeaders = Request::getDefaultHeaders();
83
        $expectedHeaders['If-None-Match'] = 'fooETag';
84
85
        $this->assertEquals($expectedHeaders, $headers);
86
    }
87
88
    public function testGetParamsWillAutoAppendAccessTokenAndAppSecretProof()
89
    {
90
        $app = new Application('123', 'foo_secret');
91
        $request = new Request($app, 'foo_token', 'POST', '/foo', ['foo' => 'bar']);
92
93
        $params = $request->getParams();
94
95
        $this->assertEquals([
96
            'foo' => 'bar',
97
            'access_token' => 'foo_token',
98
            'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
99
        ], $params);
100
    }
101
102
    public function testAnAccessTokenCanBeSetFromTheParams()
103
    {
104
        $app = new Application('123', 'foo_secret');
105
        $request = new Request($app, null, 'POST', '/me', ['access_token' => 'bar_token']);
106
107
        $accessToken = $request->getAccessToken();
108
109
        $this->assertEquals('bar_token', $accessToken);
110
    }
111
112
    /**
113
     * @expectedException \Facebook\Exception\SDKException
114
     */
115
    public function testAccessTokenConflictsWillThrow()
116
    {
117
        $app = new Application('123', 'foo_secret');
118
        new Request($app, 'foo_token', 'POST', '/me', ['access_token' => 'bar_token']);
119
    }
120
121
    public function testAProperUrlWillBeGenerated()
122
    {
123
        $app = new Application('123', 'foo_secret');
124
        $getRequest = new Request($app, 'foo_token', 'GET', '/foo', ['foo' => 'bar']);
125
126
        $getUrl = $getRequest->getUrl();
127
        $expectedParams = 'foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9';
128
        $expectedUrl = '/foo?' . $expectedParams;
129
130
        $this->assertEquals($expectedUrl, $getUrl);
131
132
        $postRequest = new Request($app, 'foo_token', 'POST', '/bar', ['foo' => 'bar'], null, 'v0.0');
133
134
        $postUrl = $postRequest->getUrl();
135
        $expectedUrl = '/v0.0/bar';
136
137
        $this->assertEquals($expectedUrl, $postUrl);
138
    }
139
140
    public function testAuthenticationParamsAreStrippedAndReapplied()
141
    {
142
        $app = new Application('123', 'foo_secret');
143
144
        $request = new Request(
145
            $app,
146
            $accessToken = 'foo_token',
147
            $method = 'GET',
148
            $endpoint = '/foo',
149
            $params = [
150
                'access_token' => 'foo_token',
151
                'appsecret_proof' => 'bar_app_secret',
152
                'bar' => 'baz',
153
            ]
154
        );
155
156
        $url = $request->getUrl();
157
158
        $expectedParams = 'bar=baz&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9';
159
        $expectedUrl = '/foo?' . $expectedParams;
160
        $this->assertEquals($expectedUrl, $url);
161
162
        $params = $request->getParams();
163
164
        $expectedParams = [
165
            'access_token' => 'foo_token',
166
            'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
167
            'bar' => 'baz',
168
        ];
169
        $this->assertEquals($expectedParams, $params);
170
    }
171
172 View Code Duplication
    public function testAFileCanBeAddedToParams()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $myFile = new File(__DIR__ . '/foo.txt');
175
        $params = [
176
            'name' => 'Foo Bar',
177
            'source' => $myFile,
178
        ];
179
        $app = new Application('123', 'foo_secret');
180
        $request = new Request($app, 'foo_token', 'POST', '/foo/photos', $params);
181
182
        $actualParams = $request->getParams();
183
184
        $this->assertTrue($request->containsFileUploads());
185
        $this->assertFalse($request->containsVideoUploads());
186
        $this->assertFalse(isset($actualParams['source']));
187
        $this->assertEquals('Foo Bar', $actualParams['name']);
188
    }
189
190 View Code Duplication
    public function testAVideoCanBeAddedToParams()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $myFile = new Video(__DIR__ . '/foo.txt');
193
        $params = [
194
            'name' => 'Foo Bar',
195
            'source' => $myFile,
196
        ];
197
        $app = new Application('123', 'foo_secret');
198
        $request = new Request($app, 'foo_token', 'POST', '/foo/videos', $params);
199
200
        $actualParams = $request->getParams();
201
202
        $this->assertTrue($request->containsFileUploads());
203
        $this->assertTrue($request->containsVideoUploads());
204
        $this->assertFalse(isset($actualParams['source']));
205
        $this->assertEquals('Foo Bar', $actualParams['name']);
206
    }
207
}
208