facebook /
php-graph-sdk
| 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 | namespace Facebook\Tests; |
||
| 24 | |||
| 25 | use Facebook\Facebook; |
||
| 26 | use Facebook\Client; |
||
| 27 | use Facebook\Request; |
||
| 28 | use Facebook\Authentication\AccessToken; |
||
| 29 | use Facebook\GraphNode\GraphEdge; |
||
| 30 | use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload; |
||
| 31 | use Facebook\Tests\Fixtures\FooHttpClientInterface; |
||
| 32 | use Facebook\Tests\Fixtures\FooPersistentDataInterface; |
||
| 33 | use Facebook\Tests\Fixtures\FooUrlDetectionInterface; |
||
| 34 | use Facebook\PersistentData\InMemoryPersistentDataHandler; |
||
| 35 | use Facebook\Tests\Fixtures\MyFooHttpClient; |
||
| 36 | use Facebook\Url\UrlDetectionHandler; |
||
| 37 | use Facebook\Response; |
||
| 38 | use Facebook\GraphNode\GraphUser; |
||
| 39 | use Http\Factory\Guzzle\RequestFactory; |
||
| 40 | use Http\Factory\Guzzle\StreamFactory; |
||
| 41 | use PHPUnit\Framework\Error\Error; |
||
| 42 | use PHPUnit\Framework\TestCase; |
||
| 43 | |||
| 44 | class Test extends TestCase |
||
| 45 | { |
||
| 46 | protected $config = [ |
||
| 47 | 'app_id' => '1337', |
||
| 48 | 'app_secret' => 'foo_secret', |
||
| 49 | 'default_graph_version' => 'v0.0', |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @expectedException \Facebook\Exception\SDKException |
||
| 54 | */ |
||
| 55 | public function testInstantiatingWithoutAppIdThrows() |
||
| 56 | { |
||
| 57 | // unset value so there is no fallback to test expected Exception |
||
| 58 | putenv(Facebook::APP_ID_ENV_NAME.'='); |
||
| 59 | $config = [ |
||
| 60 | 'app_secret' => 'foo_secret', |
||
| 61 | 'default_graph_version' => 'v0.0', |
||
| 62 | ]; |
||
| 63 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @expectedException \Facebook\Exception\SDKException |
||
| 68 | */ |
||
| 69 | public function testInstantiatingWithoutAppSecretThrows() |
||
| 70 | { |
||
| 71 | // unset value so there is no fallback to test expected Exception |
||
| 72 | putenv(Facebook::APP_SECRET_ENV_NAME.'='); |
||
| 73 | $config = [ |
||
| 74 | 'app_id' => 'foo_id', |
||
| 75 | 'default_graph_version' => 'v0.0', |
||
| 76 | ]; |
||
| 77 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @expectedException \InvalidArgumentException |
||
| 82 | */ |
||
| 83 | public function testInstantiatingWithoutDefaultGraphVersionThrows() |
||
| 84 | { |
||
| 85 | $config = [ |
||
| 86 | 'app_id' => 'foo_id', |
||
| 87 | 'app_secret' => 'foo_secret', |
||
| 88 | ]; |
||
| 89 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @expectedException \InvalidArgumentException |
||
| 94 | */ |
||
| 95 | public function testSettingAnInvalidPersistentDataHandlerThrows() |
||
| 96 | { |
||
| 97 | $config = array_merge($this->config, [ |
||
| 98 | 'persistent_data_handler' => 'foo_handler', |
||
| 99 | ]); |
||
| 100 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testPersistentDataHandlerCanBeForced() |
||
| 104 | { |
||
| 105 | $config = array_merge($this->config, [ |
||
| 106 | 'persistent_data_handler' => 'memory' |
||
| 107 | ]); |
||
| 108 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 109 | $this->assertInstanceOf( |
||
| 110 | InMemoryPersistentDataHandler::class, |
||
| 111 | $fb->getRedirectLoginHelper()->getPersistentDataHandler() |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @expectedException Error |
||
| 117 | */ |
||
| 118 | public function testSettingAnInvalidUrlHandlerThrows() |
||
| 119 | { |
||
| 120 | $config = array_merge($this->config, [ |
||
| 121 | 'url_detection_handler' => 'foo_handler', |
||
| 122 | ]); |
||
| 123 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testTheUrlHandlerWillDefaultToTheImplementation() |
||
| 127 | { |
||
| 128 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config); |
||
| 129 | $this->assertInstanceOf(UrlDetectionHandler::class, $fb->getUrlDetectionHandler()); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testAnAccessTokenCanBeSetAsAString() |
||
| 133 | { |
||
| 134 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config); |
||
| 135 | $fb->setDefaultAccessToken('foo_token'); |
||
| 136 | $accessToken = $fb->getDefaultAccessToken(); |
||
| 137 | |||
| 138 | $this->assertInstanceOf(AccessToken::class, $accessToken); |
||
| 139 | $this->assertEquals('foo_token', (string)$accessToken); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity() |
||
| 143 | { |
||
| 144 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $this->config); |
||
| 145 | $fb->setDefaultAccessToken(new AccessToken('bar_token')); |
||
| 146 | $accessToken = $fb->getDefaultAccessToken(); |
||
| 147 | |||
| 148 | $this->assertInstanceOf(AccessToken::class, $accessToken); |
||
| 149 | $this->assertEquals('bar_token', (string)$accessToken); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @expectedException \InvalidArgumentException |
||
| 154 | */ |
||
| 155 | public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows() |
||
| 156 | { |
||
| 157 | $config = array_merge($this->config, [ |
||
| 158 | 'default_access_token' => 123, |
||
| 159 | ]); |
||
| 160 | new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testCreatingANewRequestWillDefaultToTheProperConfig() |
||
| 164 | { |
||
| 165 | $config = array_merge($this->config, [ |
||
| 166 | 'default_access_token' => 'foo_token', |
||
| 167 | 'enable_beta_mode' => true, |
||
| 168 | 'default_graph_version' => 'v1337', |
||
| 169 | ]); |
||
| 170 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 171 | |||
| 172 | $request = $fb->request('FOO_VERB', '/foo'); |
||
| 173 | $this->assertEquals('1337', $request->getApplication()->getId()); |
||
| 174 | $this->assertEquals('foo_secret', $request->getApplication()->getSecret()); |
||
| 175 | $this->assertEquals('foo_token', (string)$request->getAccessToken()); |
||
| 176 | $this->assertEquals('v1337', $request->getGraphVersion()); |
||
| 177 | $this->assertEquals( |
||
| 178 | Client::BASE_GRAPH_URL_BETA, |
||
| 179 | $fb->getClient()->getBaseGraphUrl() |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function testCreatingANewBatchRequestWillDefaultToTheProperConfig() |
||
| 184 | { |
||
| 185 | $config = array_merge($this->config, [ |
||
| 186 | 'default_access_token' => 'foo_token', |
||
| 187 | 'enable_beta_mode' => true, |
||
| 188 | 'default_graph_version' => 'v1337', |
||
| 189 | ]); |
||
| 190 | $fb = new Facebook(new MyFooHttpClient(), new RequestFactory(), new StreamFactory(), $config); |
||
| 191 | |||
| 192 | $batchRequest = $fb->newBatchRequest(); |
||
| 193 | $this->assertEquals('1337', $batchRequest->getApplication()->getId()); |
||
| 194 | $this->assertEquals('foo_secret', $batchRequest->getApplication()->getSecret()); |
||
| 195 | $this->assertEquals('foo_token', (string)$batchRequest->getAccessToken()); |
||
| 196 | $this->assertEquals('v1337', $batchRequest->getGraphVersion()); |
||
| 197 | $this->assertEquals( |
||
| 198 | Client::BASE_GRAPH_URL_BETA, |
||
| 199 | $fb->getClient()->getBaseGraphUrl() |
||
| 200 | ); |
||
| 201 | $this->assertInstanceOf('Facebook\BatchRequest', $batchRequest); |
||
| 202 | $this->assertCount(0, $batchRequest->getRequests()); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function testCanInjectCustomHandlers() |
||
| 206 | { |
||
| 207 | $config = array_merge($this->config, [ |
||
| 208 | 'persistent_data_handler' => new FooPersistentDataInterface(), |
||
| 209 | 'url_detection_handler' => new FooUrlDetectionInterface(), |
||
| 210 | ]); |
||
| 211 | $fb = new Facebook(new FooHttpClientInterface(), new RequestFactory(), new StreamFactory(), $config); |
||
| 212 | |||
| 213 | $this->assertInstanceOf( |
||
| 214 | FooHttpClientInterface::class, |
||
| 215 | $fb->getClient()->getHttpClient() |
||
| 216 | ); |
||
| 217 | $this->assertInstanceOf( |
||
| 218 | FooPersistentDataInterface::class, |
||
| 219 | $fb->getRedirectLoginHelper()->getPersistentDataHandler() |
||
| 220 | ); |
||
| 221 | $this->assertInstanceOf( |
||
| 222 | FooUrlDetectionInterface::class, |
||
| 223 | $fb->getRedirectLoginHelper()->getUrlDetectionHandler() |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function testPaginationReturnsProperResponse() |
||
| 228 | { |
||
| 229 | $fb = new Facebook(new FooHttpClientInterface(), new RequestFactory(), new StreamFactory(), $this->config); |
||
| 230 | |||
| 231 | $request = new Request($fb->getApplication(), 'foo_token', 'GET'); |
||
| 232 | $graphEdge = new GraphEdge( |
||
| 233 | $request, |
||
| 234 | [], |
||
| 235 | [ |
||
| 236 | 'paging' => [ |
||
| 237 | 'cursors' => [ |
||
| 238 | 'after' => 'bar_after_cursor', |
||
| 239 | 'before' => 'bar_before_cursor', |
||
| 240 | ], |
||
| 241 | 'previous' => 'previous_url', |
||
| 242 | 'next' => 'next_url', |
||
| 243 | ] |
||
| 244 | ], |
||
| 245 | '/1337/photos', |
||
| 246 | GraphUser::class |
||
| 247 | ); |
||
| 248 | |||
| 249 | $nextPage = $fb->next($graphEdge); |
||
| 250 | $this->assertInstanceOf(GraphEdge::class, $nextPage); |
||
| 251 | $this->assertInstanceOf(GraphUser::class, $nextPage[0]); |
||
| 252 | $this->assertEquals('Foo', $nextPage[0]->getField('name')); |
||
| 253 | |||
| 254 | $lastResponse = $fb->getLastResponse(); |
||
| 255 | $this->assertInstanceOf(Response::class, $lastResponse); |
||
| 256 | $this->assertEquals(1337, $lastResponse->getHttpStatusCode()); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function testCanGetSuccessfulTransferWithMaxTries() |
||
| 260 | { |
||
| 261 | $fb = new Facebook(new FakeGraphApiForResumableUpload(), new RequestFactory(), new StreamFactory(), $this->config); |
||
| 262 | $response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 263 | $this->assertEquals([ |
||
| 264 | 'video_id' => '1337', |
||
| 265 | 'success' => true, |
||
| 266 | ], $response); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @expectedException \Facebook\Exception\ResponseException |
||
| 271 | */ |
||
| 272 | public function testMaxingOutRetriesWillThrow() |
||
| 273 | { |
||
| 274 | $client = new FakeGraphApiForResumableUpload(); |
||
| 275 | $client->failOnTransfer(); |
||
| 276 | |||
| 277 | $fb = new Facebook($client, new RequestFactory(), new StreamFactory(), $this->config); |
||
| 278 | $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3); |
||
| 279 | } |
||
| 280 | } |
||
| 281 |