Completed
Pull Request — master (#27)
by Harry
06:09
created

CredentialsAuthMiddlewareTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 88.89 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 96
loc 108
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testKeyAndSecretIsPassedToParams() 23 23 1
A testKeySecretAndUserKeyIsPassedToParams() 23 23 1
A testAccessors() 8 13 1
A testSubscriberDoesNotDoAnythingForNonHttpsRequests() 21 21 1
A testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() 21 21 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;
15
16
use Graze\Gigya\Auth\CredentialsAuthMiddleware;
17
use Graze\Gigya\Test\TestCase;
18
use GuzzleHttp\Handler\MockHandler;
19
use GuzzleHttp\HandlerStack;
20
use GuzzleHttp\Promise\PromiseInterface;
21
use GuzzleHttp\Psr7\Request;
22
use GuzzleHttp\Psr7\Response;
23
use Psr\Http\Message\RequestInterface;
24
25
class CredentialsAuthMiddlewareTest extends TestCase
26
{
27 View Code Duplication
    public function testKeyAndSecretIsPassedToParams()
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...
28
    {
29
        $handler = new MockHandler(
30
            [
31
                function (RequestInterface $request) {
32
                    $params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody());
33
                    $this->assertArrayHasKey('client_id', $params);
34
                    $this->assertEquals('key', $params['client_id']);
35
                    $this->assertArrayHasKey('client_secret', $params);
36
                    $this->assertEquals('client_secret', $params['client_secret']);
37
                    return new Response(200);
38
                },
39
            ]
40
        );
41
42
        $stack = new HandlerStack($handler);
43
        $stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret'));
44
45
        $comp = $stack->resolve();
46
47
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']);
48
        $this->assertInstanceOf(PromiseInterface::class, $promise);
49
    }
50
51 View Code Duplication
    public function testKeySecretAndUserKeyIsPassedToParams()
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
        $handler = new MockHandler(
54
            [
55
                function (RequestInterface $request) {
56
                    $params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody());
57
                    $this->assertArrayHasKey('client_id', $params);
58
                    $this->assertEquals('user', $params['client_id']);
59
                    $this->assertArrayHasKey('client_secret', $params);
60
                    $this->assertEquals('client_secret', $params['client_secret']);
61
                    return new Response(200);
62
                },
63
            ]
64
        );
65
66
        $stack = new HandlerStack($handler);
67
        $stack->push(CredentialsAuthMiddleware::middleware('key', 'client_secret', 'user'));
68
69
        $comp = $stack->resolve();
70
71
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'credentials']);
72
        $this->assertInstanceOf(PromiseInterface::class, $promise);
73
    }
74
75 View Code Duplication
    public function testAccessors()
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...
76
    {
77
        $auth = new CredentialsAuthMiddleware(
78
            function () {
79
            },
80
            'key',
81
            'client_secret',
82
            'user'
83
        );
84
        static::assertEquals('key', $auth->getApiKey());
85
        static::assertEquals('client_secret', $auth->getSecret());
86
        static::assertEquals('user', $auth->getUserKey());
87
    }
88
89 View Code Duplication
    public function testSubscriberDoesNotDoAnythingForNonHttpsRequests()
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...
90
    {
91
        $handler = new MockHandler(
92
            [
93
                function (RequestInterface $request) {
94
                    $params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody());
95
                    $this->assertArrayNotHasKey('client_id', $params);
96
                    $this->assertArrayNotHasKey('client_secret', $params);
97
                    return new Response(200);
98
                },
99
            ]
100
        );
101
102
        $stack = new HandlerStack($handler);
103
        $stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user'));
104
105
        $comp = $stack->resolve();
106
107
        $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'credentials']);
108
        $this->assertInstanceOf(PromiseInterface::class, $promise);
109
    }
110
111 View Code Duplication
    public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests()
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...
112
    {
113
        $handler = new MockHandler(
114
            [
115
                function (RequestInterface $request) {
116
                    $params = \GuzzleHttp\Psr7\parse_query((string)$request->getBody());
117
                    $this->assertArrayNotHasKey('client_id', $params);
118
                    $this->assertArrayNotHasKey('client_secret', $params);
119
                    return new Response(200);
120
                },
121
            ]
122
        );
123
124
        $stack = new HandlerStack($handler);
125
        $stack->push(CredentialsAuthMiddleware::middleware('key', 'secret', 'user'));
126
127
        $comp = $stack->resolve();
128
129
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'oauth']);
130
        $this->assertInstanceOf(PromiseInterface::class, $promise);
131
    }
132
}
133