Completed
Pull Request — master (#20)
by Harry
11:39 queued 03:51
created

testGetEventsHandlesBeforeAndSignsRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
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\GigyaAuthInterface;
17
use Graze\Gigya\Auth\HttpsAuth;
18
use Graze\Gigya\Test\TestCase;
19
use GuzzleHttp\Collection;
20
use GuzzleHttp\Event\BeforeEvent;
21
use GuzzleHttp\Event\RequestEvents;
22
use GuzzleHttp\Event\SubscriberInterface;
23
use GuzzleHttp\Message\RequestInterface;
24
use GuzzleHttp\Query;
25
use Mockery as m;
26
27
class HttpsAuthTest extends TestCase
28
{
29
    public function testInstanceOf()
30
    {
31
        $auth = new HttpsAuth('key', 'secret');
32
        static::assertInstanceOf(SubscriberInterface::class, $auth);
33
    }
34
35
    public function testGetEventsHandlesBeforeAndSignsRequest()
36
    {
37
        $auth = new HttpsAuth('key', 'secret');
38
        static::assertEquals(
39
            ['before' => ['sign', RequestEvents::SIGN_REQUEST]],
40
            $auth->getEvents()
41
        );
42
    }
43
44
    public function testKeyAndSecretIsPassedToParams()
45
    {
46
        $request = m::mock(RequestInterface::class);
47
        $event   = m::mock(BeforeEvent::class);
48
        $event->shouldReceive('getRequest')
49
              ->andReturn($request);
50
51
        $query  = m::mock(Query::class);
52
        $config = m::mock(Collection::class);
53
54
        $request->shouldReceive('getScheme')
55
                ->andReturn('https');
56
        $request->shouldReceive('getConfig')
57
                ->andReturn($config);
58
        $request->shouldReceive('getQuery')
59
                ->andReturn($query);
60
61
        $query->shouldReceive('offsetSet')
62
              ->with('apiKey', 'key');
63
        $query->shouldReceive('offsetSet')
64
              ->with('secret', 'secret');
65
        $config->shouldReceive('get')
66
               ->with('auth')
67
               ->andReturn('gigya');
68
69
        $auth = new HttpsAuth('key', 'secret');
70
        $auth->sign($event);
71
    }
72
73
    public function testKeySecretAndUserKeyIsPassedToParams()
74
    {
75
        $request = m::mock(RequestInterface::class);
76
        $event   = m::mock(BeforeEvent::class);
77
        $event->shouldReceive('getRequest')
78
              ->andReturn($request);
79
80
        $query  = m::mock(Query::class);
81
        $config = m::mock(Collection::class);
82
83
        $request->shouldReceive('getScheme')
84
                ->andReturn('https');
85
        $request->shouldReceive('getConfig')
86
                ->andReturn($config);
87
        $request->shouldReceive('getQuery')
88
                ->andReturn($query);
89
90
        $query->shouldReceive('offsetSet')
91
              ->with('apiKey', 'key');
92
        $query->shouldReceive('offsetSet')
93
              ->with('secret', 'secret');
94
        $query->shouldReceive('offsetSet')
95
              ->with('userKey', 'user');
96
        $config->shouldReceive('get')
97
               ->with('auth')
98
               ->andReturn('gigya');
99
100
        $auth = new HttpsAuth('key', 'secret', 'user');
101
        $auth->sign($event);
102
    }
103
104
    public function testAccessors()
105
    {
106
        $auth = new HttpsAuth('key', 'secret', 'user');
107
        static::assertEquals('key', $auth->getApiKey());
108
        static::assertEquals('secret', $auth->getSecret());
109
        static::assertEquals('user', $auth->getUserKey());
110
    }
111
112
    public function testSubscriberDoesNotDoAnythingForNonHttpsRequests()
113
    {
114
        $request = m::mock(RequestInterface::class);
115
        $event   = m::mock(BeforeEvent::class);
116
        $event->shouldReceive('getRequest')
117
              ->andReturn($request);
118
119
        $request->shouldReceive('getScheme')
120
                ->andReturn('http');
121
122
        $auth = new HttpsAuth('key', 'secret', 'user');
123
        $auth->sign($event);
124
    }
125
126
    public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests()
127
    {
128
        $request = m::mock(RequestInterface::class);
129
        $event   = m::mock(BeforeEvent::class);
130
        $event->shouldReceive('getRequest')
131
              ->andReturn($request);
132
133
        $config = m::mock(Collection::class);
134
        $request->shouldReceive('getScheme')
135
                ->andReturn('https');
136
        $request->shouldReceive('getConfig')
137
                ->andReturn($config);
138
139
        $config->shouldReceive('get')
140
               ->with('auth')
141
               ->andReturn('oauth');
142
143
        $auth = new HttpsAuth('key', 'secret', 'user');
144
        $auth->sign($event);
145
    }
146
}
147