Completed
Pull Request — master (#21)
by Harry
06:22
created

OAuth2MiddlewareTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 165
Duplicated Lines 44.24 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 11
dl 73
loc 165
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testSign() 21 21 1
A testErrorThatIsNot401() 21 21 1
B testErrorThatIsNotRetried() 5 26 1
B testErrorThatIsRetried() 5 34 2
A testErrorThatIsNotOauthAuth() 21 21 1
B testErrorWhenNoTokenIsReturnedWillNotIntercept() 0 26 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
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit\Auth\OAuth2;
15
16
use Graze\Gigya\Auth\OAuth2\AccessToken;
17
use Graze\Gigya\Auth\OAuth2\GrantInterface;
18
use Graze\Gigya\Auth\OAuth2\OAuth2Middleware;
19
use Graze\Gigya\Test\TestCase;
20
use GuzzleHttp\Handler\MockHandler;
21
use GuzzleHttp\HandlerStack;
22
use GuzzleHttp\Promise\PromiseInterface;
23
use GuzzleHttp\Psr7\Request;
24
use GuzzleHttp\Psr7\Response;
25
use Mockery as m;
26
use Psr\Http\Message\RequestInterface;
27
use Psr\Http\Message\ResponseInterface;
28
29
class OAuth2MiddlewareTest extends TestCase
30
{
31
    /** @var mixed */
32
    private $grant;
33
34
    public function setUp()
35
    {
36
        $this->grant = m::mock(GrantInterface::class);
37
    }
38
39 View Code Duplication
    public function testSign()
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
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
42
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
                $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization'));
44
                return new Response(200);
45
            },
46
        ]);
47
48
        $token = new AccessToken('test');
49
        $this->grant->shouldReceive('getToken')
50
                    ->andReturn($token);
51
52
        $stack = new HandlerStack($h);
53
        $stack->push(OAuth2Middleware::middleware($this->grant));
54
55
        $comp = $stack->resolve();
56
57
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
58
        $this->assertInstanceOf(PromiseInterface::class, $p);
59
    }
60
61 View Code Duplication
    public function testErrorThatIsNot401()
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...
62
    {
63
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
64
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
                $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization'));
66
                return new Response(503);
67
            },
68
        ]);
69
70
        $token = new AccessToken('test');
71
        $this->grant->shouldReceive('getToken')
72
                    ->andReturn($token);
73
74
        $stack = new HandlerStack($h);
75
        $stack->push(OAuth2Middleware::middleware($this->grant));
76
77
        $comp = $stack->resolve();
78
79
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
80
        $this->assertInstanceOf(PromiseInterface::class, $p);
81
    }
82
83
    public function testErrorThatIsNotRetried()
84
    {
85
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
86 View Code Duplication
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization'));
88
                $this->assertEquals(1, $options['retries']);
89
                return new Response(401);
90
            },
91
        ]);
92
93
        $token = new AccessToken('test');
94
        $this->grant->shouldReceive('getToken')
95
                    ->andReturn($token);
96
97
        $stack = new HandlerStack($h);
98
        $stack->push(OAuth2Middleware::middleware($this->grant));
99
100
        $comp = $stack->resolve();
101
102
        /** @var PromiseInterface $p */
103
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2', 'retries' => 1]);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
104
        $this->assertInstanceOf(PromiseInterface::class, $p);
105
        /** @var ResponseInterface $response */
106
        $response = $p->wait();
107
        $this->assertEquals(401, $response->getStatusCode());
108
    }
109
110
    public function testErrorThatIsRetried()
111
    {
112
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
113
            function (RequestInterface $request, array $options) {
114
                $this->assertEquals('OAuth test', $request->getHeaderLine('Authorization'));
115
                if (isset($options['retries'])) {
116
                    $this->assertEquals(0, $options['retries']);
117
                }
118
                return new Response(401);
119
            },
120 View Code Duplication
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
121
                $this->assertEquals('OAuth test2', $request->getHeaderLine('Authorization'));
122
                $this->assertEquals(1, $options['retries']);
123
                return new Response(200);
124
            },
125
        ]);
126
127
        $token1 = new AccessToken('test');
128
        $token2 = new AccessToken('test2');
129
        $this->grant->shouldReceive('getToken')
130
                    ->andReturn($token1, $token2);
131
132
        $stack = new HandlerStack($h);
133
        $stack->push(OAuth2Middleware::middleware($this->grant));
134
135
        $comp = $stack->resolve();
136
137
        /** @var PromiseInterface $p */
138
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
139
        $this->assertInstanceOf(PromiseInterface::class, $p);
140
        /** @var ResponseInterface $response */
141
        $response = $p->wait();
142
        $this->assertEquals(200, $response->getStatusCode());
143
    }
144
145 View Code Duplication
    public function testErrorThatIsNotOauthAuth()
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...
146
    {
147
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
148
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
                $this->assertEquals('', $request->getHeaderLine('Authorization'));
150
                return new Response(401);
151
            },
152
        ]);
153
154
        $stack = new HandlerStack($h);
155
        $stack->push(OAuth2Middleware::middleware($this->grant));
156
157
        $comp = $stack->resolve();
158
159
        /** @var PromiseInterface $p */
160
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'none']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
161
        $this->assertInstanceOf(PromiseInterface::class, $p);
162
        /** @var ResponseInterface $response */
163
        $response = $p->wait(true);
164
        $this->assertEquals(401, $response->getStatusCode());
165
    }
166
167
    public function testErrorWhenNoTokenIsReturnedWillNotIntercept()
168
    {
169
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
170
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
                $this->assertEquals('', $request->getHeaderLine('Authorization'));
172
                return new Response(401);
173
            },
174
        ]);
175
176
        $this->grant->shouldReceive('getToken')
177
                    ->atLeast()
178
                    ->once()
179
                    ->andReturn(null);
180
181
        $stack = new HandlerStack($h);
182
        $stack->push(OAuth2Middleware::middleware($this->grant));
183
184
        $comp = $stack->resolve();
185
186
        /** @var PromiseInterface $p */
187
        $p = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya-oauth2']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
188
        $this->assertInstanceOf(PromiseInterface::class, $p);
189
        /** @var ResponseInterface $response */
190
        $response = $p->wait(true);
191
        $this->assertEquals(401, $response->getStatusCode());
192
    }
193
}
194