Completed
Pull Request — master (#21)
by Harry
02:25
created

HttpsAuthMiddlewareTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 71.74 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 66
loc 92
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testKeyAndSecretIsPassedToParams() 18 18 1
A testKeySecretAndUserKeyIsPassedToParams() 0 19 1
A testAccessors() 8 8 1
A testSubscriberDoesNotDoAnythingForNonHttpsRequests() 20 20 2
A testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() 20 20 2

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\HttpsAuthMiddleware;
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 Mockery as m;
24
use Psr\Http\Message\RequestInterface;
25
26
class HttpsAuthMiddlewareTest extends TestCase
27
{
28 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...
29
    {
30
        $handler = new MockHandler([
31
            function (RequestInterface $request, array $options) {
32
                $this->assertEquals('key', $options['query']['apiKey']);
33
                $this->assertEquals('secret', $options['query']['secret']);
34
                return new Response(200);
35
            },
36
        ]);
37
38
        $stack = new HandlerStack($handler);
39
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret'));
40
41
        $comp = $stack->resolve();
42
43
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']);
44
        $this->assertInstanceOf(PromiseInterface::class, $promise);
45
    }
46
47
    public function testKeySecretAndUserKeyIsPassedToParams()
48
    {
49
        $handler = new MockHandler([
50
            function (RequestInterface $request, array $options) {
51
                $this->assertEquals('key', $options['query']['apiKey']);
52
                $this->assertEquals('secret', $options['query']['secret']);
53
                $this->assertEquals('user', $options['query']['userKey']);
54
                return new Response(200);
55
            },
56
        ]);
57
58
        $stack = new HandlerStack($handler);
59
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
60
61
        $comp = $stack->resolve();
62
63
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']);
64
        $this->assertInstanceOf(PromiseInterface::class, $promise);
65
    }
66
67 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...
68
    {
69
        $auth = new HttpsAuthMiddleware(function () {
70
        }, 'key', 'secret', 'user');
71
        static::assertEquals('key', $auth->getApiKey());
72
        static::assertEquals('secret', $auth->getSecret());
73
        static::assertEquals('user', $auth->getUserKey());
74
    }
75
76 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...
77
    {
78
        $handler = new MockHandler([
79
            function (RequestInterface $request, array $options) {
80
                if (isset($options['query'])) {
81
                    $this->assertNotContains('apiKey', $options['query']);
82
                    $this->assertNotContains('secret', $options['query']);
83
                }
84
                return new Response(200);
85
            },
86
        ]);
87
88
        $stack = new HandlerStack($handler);
89
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
90
91
        $comp = $stack->resolve();
92
93
        $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'gigya']);
94
        $this->assertInstanceOf(PromiseInterface::class, $promise);
95
    }
96
97 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...
98
    {
99
        $handler = new MockHandler([
100
            function (RequestInterface $request, array $options) {
101
                if (isset($options['query'])) {
102
                    $this->assertNotContains('apiKey', $options['query']);
103
                    $this->assertNotContains('secret', $options['query']);
104
                }
105
                return new Response(200);
106
            },
107
        ]);
108
109
        $stack = new HandlerStack($handler);
110
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
111
112
        $comp = $stack->resolve();
113
114
        $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'oauth']);
115
        $this->assertInstanceOf(PromiseInterface::class, $promise);
116
    }
117
}
118