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

testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
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 Psr\Http\Message\RequestInterface;
24
25
class HttpsAuthMiddlewareTest 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($request->getBody());
33
                    $this->assertArrayHasKey('apiKey', $params);
34
                    $this->assertEquals('key', $params['apiKey']);
35
                    $this->assertArrayHasKey('secret', $params);
36
                    $this->assertEquals('secret', $params['secret']);
37
                    return new Response(200);
38
                },
39
            ]
40
        );
41
42
        $stack = new HandlerStack($handler);
43
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret'));
44
45
        $comp = $stack->resolve();
46
47
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']);
48
        $this->assertInstanceOf(PromiseInterface::class, $promise);
49
    }
50
51
    public function testKeySecretAndUserKeyIsPassedToParams()
52
    {
53
        $handler = new MockHandler(
54
            [
55
                function (RequestInterface $request) {
56
                    $params = \GuzzleHttp\Psr7\parse_query($request->getBody());
57
                    $this->assertArrayHasKey('apiKey', $params);
58
                    $this->assertEquals('key', $params['apiKey']);
59
                    $this->assertArrayHasKey('secret', $params);
60
                    $this->assertEquals('secret', $params['secret']);
61
                    $this->assertArrayHasKey('userKey', $params);
62
                    $this->assertEquals('user', $params['userKey']);
63
                    return new Response(200);
64
                },
65
            ]
66
        );
67
68
        $stack = new HandlerStack($handler);
69
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
70
71
        $comp = $stack->resolve();
72
73
        $promise = $comp(new Request('GET', 'https://example.com'), ['auth' => 'gigya']);
74
        $this->assertInstanceOf(PromiseInterface::class, $promise);
75
    }
76
77 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...
78
    {
79
        $auth = new HttpsAuthMiddleware(
80
            function () {
81
            },
82
            'key',
83
            'secret',
84
            'user'
85
        );
86
        static::assertEquals('key', $auth->getApiKey());
87
        static::assertEquals('secret', $auth->getSecret());
88
        static::assertEquals('user', $auth->getUserKey());
89
    }
90
91 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...
92
    {
93
        $handler = new MockHandler(
94
            [
95
                function (RequestInterface $request) {
96
                    $params = \GuzzleHttp\Psr7\parse_query($request->getBody());
97
                    $this->assertArrayNotHasKey('apiKey', $params);
98
                    $this->assertArrayNotHasKey('secret', $params);
99
                    $this->assertArrayNotHasKey('userKey', $params);
100
                    return new Response(200);
101
                },
102
            ]
103
        );
104
105
        $stack = new HandlerStack($handler);
106
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
107
108
        $comp = $stack->resolve();
109
110
        $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'gigya']);
111
        $this->assertInstanceOf(PromiseInterface::class, $promise);
112
    }
113
114 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...
115
    {
116
        $handler = new MockHandler(
117
            [
118
                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...
119
                    $params = \GuzzleHttp\Psr7\parse_query($request->getBody());
120
                    $this->assertArrayNotHasKey('apiKey', $params);
121
                    $this->assertArrayNotHasKey('secret', $params);
122
                    $this->assertArrayNotHasKey('userKey', $params);
123
                    return new Response(200);
124
                },
125
            ]
126
        );
127
128
        $stack = new HandlerStack($handler);
129
        $stack->push(HttpsAuthMiddleware::middleware('key', 'secret', 'user'));
130
131
        $comp = $stack->resolve();
132
133
        $promise = $comp(new Request('GET', 'http://example.com'), ['auth' => 'oauth']);
134
        $this->assertInstanceOf(PromiseInterface::class, $promise);
135
    }
136
}
137