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
|
|
|
namespace Facebook\Tests; |
25
|
|
|
|
26
|
|
|
use Facebook\FacebookApp; |
27
|
|
|
use Facebook\FacebookRequest; |
28
|
|
|
use Facebook\FacebookResponse; |
29
|
|
|
|
30
|
|
|
class FacebookResponseTest extends \PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \Facebook\FacebookRequest |
34
|
|
|
*/ |
35
|
|
|
protected $request; |
36
|
|
|
|
37
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$app = new FacebookApp('123', 'foo_secret'); |
40
|
|
|
$this->request = new FacebookRequest( |
41
|
|
|
$app, |
42
|
|
|
'foo_token', |
43
|
|
|
'GET', |
44
|
|
|
'/me/photos?keep=me', |
45
|
|
|
['foo' => 'bar'], |
46
|
|
|
'foo_eTag', |
47
|
|
|
'v1337' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testAnETagCanBeProperlyAccessed() |
52
|
|
|
{ |
53
|
|
|
$response = new FacebookResponse($this->request, '', 200, ['ETag' => 'foo_tag']); |
54
|
|
|
|
55
|
|
|
$eTag = $response->getETag(); |
56
|
|
|
|
57
|
|
|
$this->assertEquals('foo_tag', $eTag); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testAProperAppSecretProofCanBeGenerated() |
61
|
|
|
{ |
62
|
|
|
$response = new FacebookResponse($this->request); |
63
|
|
|
|
64
|
|
|
$appSecretProof = $response->getAppSecretProof(); |
65
|
|
|
|
66
|
|
|
$this->assertEquals('df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', $appSecretProof); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testASuccessfulJsonResponseWillBeDecodedToAGraphNode() |
70
|
|
|
{ |
71
|
|
|
$graphResponseJson = '{"id":"123","name":"Foo"}'; |
72
|
|
|
$response = new FacebookResponse($this->request, $graphResponseJson, 200); |
73
|
|
|
|
74
|
|
|
$decodedResponse = $response->getDecodedBody(); |
75
|
|
|
$graphNode = $response->getGraphNode(); |
76
|
|
|
|
77
|
|
|
$this->assertFalse($response->isError(), 'Did not expect Response to return an error.'); |
78
|
|
|
$this->assertEquals([ |
79
|
|
|
'id' => '123', |
80
|
|
|
'name' => 'Foo', |
81
|
|
|
], $decodedResponse); |
82
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphNode', $graphNode); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testASuccessfulJsonResponseWillBeDecodedToAGraphEdge() |
86
|
|
|
{ |
87
|
|
|
$graphResponseJson = '{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}'; |
88
|
|
|
$response = new FacebookResponse($this->request, $graphResponseJson, 200); |
89
|
|
|
|
90
|
|
|
$graphEdge = $response->getGraphEdge(); |
91
|
|
|
|
92
|
|
|
$this->assertFalse($response->isError(), 'Did not expect Response to return an error.'); |
93
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphNode', $graphEdge[0]); |
94
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphNode', $graphEdge[1]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testASuccessfulUrlEncodedKeyValuePairResponseWillBeDecoded() |
98
|
|
|
{ |
99
|
|
|
$graphResponseKeyValuePairs = 'id=123&name=Foo'; |
100
|
|
|
$response = new FacebookResponse($this->request, $graphResponseKeyValuePairs, 200); |
101
|
|
|
|
102
|
|
|
$decodedResponse = $response->getDecodedBody(); |
103
|
|
|
|
104
|
|
|
$this->assertFalse($response->isError(), 'Did not expect Response to return an error.'); |
105
|
|
|
$this->assertEquals([ |
106
|
|
|
'id' => '123', |
107
|
|
|
'name' => 'Foo', |
108
|
|
|
], $decodedResponse); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testErrorStatusCanBeCheckedWhenAnErrorResponseIsReturned() |
112
|
|
|
{ |
113
|
|
|
$graphResponse = '{"error":{"message":"Foo error.","type":"OAuthException","code":190,"error_subcode":463}}'; |
114
|
|
|
$response = new FacebookResponse($this->request, $graphResponse, 401); |
115
|
|
|
|
116
|
|
|
$exception = $response->getThrownException(); |
117
|
|
|
|
118
|
|
|
$this->assertTrue($response->isError(), 'Expected Response to return an error.'); |
119
|
|
|
$this->assertInstanceOf('Facebook\Exceptions\FacebookResponseException', $exception); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.