GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CheckAuthActionTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 129
Duplicated Lines 49.61 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 64
loc 129
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A testExecute() 34 34 1
A testExecuteWithApiError() 30 30 1
A testExecuteWithNoAccessToken() 0 22 1
A prepareAccessToken() 0 16 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
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\FacebookBundle\Tests\Action;
11
12
use Core23\FacebookBundle\Action\CheckAuthAction;
13
use Core23\FacebookBundle\Session\SessionInterface;
14
use Core23\FacebookBundle\Session\SessionManagerInterface;
15
use DateTime;
16
use Facebook\Authentication\AccessToken;
17
use Facebook\Exceptions\FacebookSDKException;
18
use Facebook\Facebook;
19
use Facebook\FacebookResponse;
20
use Facebook\GraphNodes\GraphUser;
21
use Facebook\Helpers\FacebookRedirectLoginHelper;
22
use PHPUnit\Framework\TestCase;
23
use Prophecy\Argument;
24
use Prophecy\Prophecy\ObjectProphecy;
25
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26
use Symfony\Component\Routing\RouterInterface;
27
28
final class CheckAuthActionTest extends TestCase
29
{
30
    private $router;
31
32
    private $facebook;
33
34
    private $sessionManager;
35
36
    private $helper;
37
38
    protected function setUp(): void
39
    {
40
        $this->router         = $this->prophesize(RouterInterface::class);
41
        $this->sessionManager = $this->prophesize(SessionManagerInterface::class);
42
43
        $this->helper = $this->prophesize(FacebookRedirectLoginHelper::class);
44
45
        $this->facebook = $this->prophesize(Facebook::class);
46
        $this->facebook->getRedirectLoginHelper()
47
            ->willReturn($this->helper)
48
        ;
49
    }
50
51 View Code Duplication
    public function testExecute(): void
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...
52
    {
53
        $accessToken = $this->prepareAccessToken();
54
55
        $graphUser = $this->prophesize(GraphUser::class);
56
57
        $response = $this->prophesize(FacebookResponse::class);
58
        $response->getGraphUser()
59
            ->willReturn($graphUser)
60
        ;
61
62
        $this->facebook->get('/me?fields=id,name', $accessToken)
63
            ->willReturn($response)
64
        ;
65
66
        $this->sessionManager->store(Argument::type(SessionInterface::class))
67
            ->shouldBeCalled()
68
        ;
69
70
        $this->router->generate('core23_facebook_success', [], UrlGeneratorInterface::ABSOLUTE_PATH)
71
            ->willReturn('/success')
72
            ->shouldBeCalled()
73
        ;
74
75
        $action = new CheckAuthAction(
76
            $this->router->reveal(),
77
            $this->facebook->reveal(),
78
            $this->sessionManager->reveal()
79
        );
80
81
        $response = $action();
82
83
        static::assertSame('/success', $response->getTargetUrl());
84
    }
85
86 View Code Duplication
    public function testExecuteWithApiError(): void
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...
87
    {
88
        $accessToken = $this->prepareAccessToken();
89
90
        $graphUser = $this->prophesize(GraphUser::class);
91
92
        $response = $this->prophesize(FacebookResponse::class);
93
        $response->getGraphUser()
94
            ->willReturn($graphUser)
95
        ;
96
97
        $this->facebook->get('/me?fields=id,name', $accessToken)
98
            ->willThrow(FacebookSDKException::class)
99
        ;
100
101
        $this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH)
102
            ->willReturn('/error')
103
            ->shouldBeCalled()
104
        ;
105
106
        $action = new CheckAuthAction(
107
            $this->router->reveal(),
108
            $this->facebook->reveal(),
109
            $this->sessionManager->reveal()
110
        );
111
112
        $response = $action();
113
114
        static::assertSame('/error', $response->getTargetUrl());
115
    }
116
117
    public function testExecuteWithNoAccessToken(): void
118
    {
119
        $this->helper = $this->prophesize(FacebookRedirectLoginHelper::class);
120
        $this->helper->getAccessToken()
121
            ->willReturn(null)
122
        ;
123
124
        $this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH)
125
            ->willReturn('/error')
126
            ->shouldBeCalled()
127
        ;
128
129
        $action = new CheckAuthAction(
130
            $this->router->reveal(),
131
            $this->facebook->reveal(),
132
            $this->sessionManager->reveal()
133
        );
134
135
        $response = $action();
136
137
        static::assertSame('/error', $response->getTargetUrl());
138
    }
139
140
    private function prepareAccessToken(): ObjectProphecy
141
    {
142
        $accessToken = $this->prophesize(AccessToken::class);
143
        $accessToken->getValue()
144
            ->willReturn('TOKEN_VALUE')
145
        ;
146
        $accessToken->getExpiresAt()
147
            ->willReturn(new DateTime('tomorrow'))
148
        ;
149
150
        $this->helper->getAccessToken()
151
            ->willReturn($accessToken)
152
        ;
153
154
        return $accessToken;
155
    }
156
}
157