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

SignedRequestFromInputHelperTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 25.42 %

Importance

Changes 0
Metric Value
dl 15
loc 59
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testAnAccessTokenCanBeInstantiatedWhenRedirectReturnsACode() 7 7 1
A setUp() 0 4 1
A testSignedRequestDataCanBeRetrievedFromCookieData() 0 7 1
A testSignedRequestDataCanBeRetrievedFromPostData() 0 7 1
A testAccessTokenWillBeNullWhenAUserHasNotYetAuthorizedTheApp() 0 6 1
A testAnAccessTokenCanBeInstantiatedWhenRedirectReturnsAnAccessToken() 7 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\Helper;
24
25
use Facebook\Application;
26
use Facebook\Tests\Fixtures\FooSignedRequestHelper;
27
use Facebook\Tests\Fixtures\FooSignedRequestHelperClient;
28
use Facebook\Authentication\AccessToken;
29
use PHPUnit\Framework\TestCase;
30
31
class SignedRequestFromInputHelperTest extends TestCase
32
{
33
    /**
34
     * @var FooSignedRequestHelper
35
     */
36
    protected $helper;
37
38
    public $rawSignedRequestAuthorizedWithAccessToken = 'vdZXlVEQ5NTRRTFvJ7Jeo_kP4SKnBDvbNP0fEYKS0Sg=.eyJvYXV0aF90b2tlbiI6ImZvb190b2tlbiIsImFsZ29yaXRobSI6IkhNQUMtU0hBMjU2IiwiaXNzdWVkX2F0IjoxNDAyNTUxMDMxLCJ1c2VyX2lkIjoiMTIzIn0=';
39
    public $rawSignedRequestAuthorizedWithCode = 'oBtmZlsFguNQvGRETDYQQu1-PhwcArgbBBEK4urbpRA=.eyJjb2RlIjoiZm9vX2NvZGUiLCJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTQwNjMxMDc1MiwidXNlcl9pZCI6IjEyMyJ9';
40
    public $rawSignedRequestUnauthorized = 'KPlyhz-whtYAhHWr15N5TkbS_avz-2rUJFpFkfXKC88=.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTQwMjU1MTA4Nn0=';
41
42
    protected function setUp()
43
    {
44
        $app = new Application('123', 'foo_app_secret');
45
        $this->helper = new FooSignedRequestHelper($app, new FooSignedRequestHelperClient(), 'v0.0');
46
    }
47
48
    public function testSignedRequestDataCanBeRetrievedFromPostData()
49
    {
50
        $_POST['signed_request'] = 'foo_signed_request';
51
52
        $rawSignedRequest = $this->helper->getRawSignedRequestFromPost();
53
54
        $this->assertEquals('foo_signed_request', $rawSignedRequest);
55
    }
56
57
    public function testSignedRequestDataCanBeRetrievedFromCookieData()
58
    {
59
        $_COOKIE['fbsr_123'] = 'foo_signed_request';
60
61
        $rawSignedRequest = $this->helper->getRawSignedRequestFromCookie();
62
63
        $this->assertEquals('foo_signed_request', $rawSignedRequest);
64
    }
65
66
    public function testAccessTokenWillBeNullWhenAUserHasNotYetAuthorizedTheApp()
67
    {
68
        $this->helper->instantiateSignedRequest($this->rawSignedRequestUnauthorized);
69
        $accessToken = $this->helper->getAccessToken();
70
71
        $this->assertNull($accessToken);
72
    }
73
74 View Code Duplication
    public function testAnAccessTokenCanBeInstantiatedWhenRedirectReturnsAnAccessToken()
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...
75
    {
76
        $this->helper->instantiateSignedRequest($this->rawSignedRequestAuthorizedWithAccessToken);
77
        $accessToken = $this->helper->getAccessToken();
78
79
        $this->assertInstanceOf(AccessToken::class, $accessToken);
80
        $this->assertEquals('foo_token', $accessToken->getValue());
81
    }
82
83 View Code Duplication
    public function testAnAccessTokenCanBeInstantiatedWhenRedirectReturnsACode()
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...
84
    {
85
        $this->helper->instantiateSignedRequest($this->rawSignedRequestAuthorizedWithCode);
86
        $accessToken = $this->helper->getAccessToken();
87
88
        $this->assertInstanceOf(AccessToken::class, $accessToken);
89
        $this->assertEquals('foo_access_token_from:foo_code', $accessToken->getValue());
90
    }
91
}
92