Completed
Pull Request — master (#888)
by Yassine
02:05
created

ResponseTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 14.44 %

Importance

Changes 0
Metric Value
dl 13
loc 90
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testASuccessfulJsonResponseWillBeDecodedToAGraphNode() 0 14 1
A testASuccessfulJsonResponseWillBeDecodedToAGraphEdge() 0 10 1
A testErrorStatusCanBeCheckedWhenAnErrorResponseIsReturned() 0 9 1
A setUp() 11 11 1
A testASuccessfulUrlEncodedKeyValuePairResponseWillBeDecoded() 0 12 1
A testAProperAppSecretProofCanBeGenerated() 0 7 1
A testAnETagCanBeProperlyAccessed() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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