Completed
Pull Request — master (#18)
by Harry
04:11
created

UidSignatureValidatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 7
c 3
b 2
f 0
lcom 1
cbo 5
dl 0
loc 165
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 4 1
A testInstanceOf() 0 4 1
B testUidSignature() 0 34 1
B testInvalidUidSignature() 0 40 1
B testInvalidTimestamp() 0 37 1
A testCanValidationReturnsFalseWhenTheRequiredFieldsAreNotPresent() 0 20 1
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\Validation;
15
16
use Graze\Gigya\Exception\InvalidTimestampException;
17
use Graze\Gigya\Exception\InvalidUidSignatureException;
18
use Graze\Gigya\Response\ResponseInterface;
19
use Graze\Gigya\Test\TestCase;
20
use Graze\Gigya\Validation\ResponseValidatorInterface;
21
use Graze\Gigya\Validation\Signature;
22
use Graze\Gigya\Validation\UidSignatureValidator;
23
use Illuminate\Support\Collection;
24
use Mockery as m;
25
use Mockery\MockInterface;
26
27
class UidSignatureValidatorTest extends TestCase
28
{
29
    const SECRET = '8j9h0g-opko;dk]=id0f[sjo';
30
31
    /**
32
     * @var MockInterface|Signature
33
     */
34
    private $signature;
35
36
    /**
37
     * @var UidSignatureValidator
38
     */
39
    private $validator;
40
41
    public function setUp()
42
    {
43
        $this->signature = m::mock(Signature::class);
44
        $this->validator = new UidSignatureValidator($this->signature, static::SECRET);
45
    }
46
47
    public function tearDown()
48
    {
49
        $this->signature = $this->validator = null;
50
    }
51
52
    public function testInstanceOf()
53
    {
54
        static::assertInstanceOf(ResponseValidatorInterface::class, $this->validator);
55
    }
56
57
    public function testUidSignature()
58
    {
59
        $response   = m::mock(ResponseInterface::class);
60
        $collection = m::mock(Collection::class);
61
        $response->shouldReceive('getData')
62
                 ->andReturn($collection);
63
        $collection->shouldReceive('has')
64
                   ->with('UID')
65
                   ->andReturn(true);
66
        $collection->shouldReceive('has')
67
                   ->with('UIDSignature')
68
                   ->andReturn(true);
69
        $collection->shouldReceive('has')
70
                   ->with('signatureTimestamp')
71
                   ->andReturn(true);
72
        $collection->shouldReceive('get')
73
                   ->with('UID')
74
                   ->andReturn('some_uid');
75
        $collection->shouldReceive('get')
76
                   ->with('UIDSignature')
77
                   ->andReturn('some_signature');
78
        $collection->shouldReceive('get')
79
                   ->with('signatureTimestamp')
80
                   ->andReturn(12345667);
81
        static::assertTrue($this->validator->canValidate($response));
82
83
        $this->signature->shouldReceive('checkTimestamp')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Gigya\Validation\Signature.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
                        ->with(12345667)
85
                        ->andReturn(true);
86
        $this->signature->shouldReceive('getUidSignature')
87
                        ->with('some_uid', 12345667, static::SECRET)
88
                        ->andReturn('some_signature');
89
        static::assertTrue($this->validator->validate($response));
90
    }
91
92
    public function testInvalidUidSignature()
93
    {
94
        $response   = m::mock(ResponseInterface::class);
95
        $collection = m::mock(Collection::class);
96
        $response->shouldReceive('getData')
97
                 ->andReturn($collection);
98
        $collection->shouldReceive('has')
99
                   ->with('UID')
100
                   ->andReturn(true);
101
        $collection->shouldReceive('has')
102
                   ->with('UIDSignature')
103
                   ->andReturn(true);
104
        $collection->shouldReceive('has')
105
                   ->with('signatureTimestamp')
106
                   ->andReturn(true);
107
        $collection->shouldReceive('get')
108
                   ->with('UID')
109
                   ->andReturn('some_uid');
110
        $collection->shouldReceive('get')
111
                   ->with('UIDSignature')
112
                   ->andReturn('some_signature');
113
        $collection->shouldReceive('get')
114
                   ->with('signatureTimestamp')
115
                   ->andReturn(12345667);
116
        static::assertTrue($this->validator->canValidate($response));
117
118
        $this->signature->shouldReceive('checkTimestamp')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Gigya\Validation\Signature.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
119
                        ->with(12345667)
120
                        ->andReturn(true);
121
        $this->signature->shouldReceive('getUidSignature')
122
                        ->with('some_uid', 12345667, static::SECRET)
123
                        ->andReturn('incorrect_signature');
124
        static::assertFalse($this->validator->validate($response));
125
126
        $response->shouldReceive('getErrorCode')
127
                 ->andReturn(0);
128
129
        static::setExpectedException(InvalidUidSignatureException::class);
130
        $this->validator->assert($response);
131
    }
132
133
    public function testInvalidTimestamp()
134
    {
135
        $response   = m::mock(ResponseInterface::class);
136
        $collection = m::mock(Collection::class);
137
        $response->shouldReceive('getData')
138
                 ->andReturn($collection);
139
        $collection->shouldReceive('has')
140
                   ->with('UID')
141
                   ->andReturn(true);
142
        $collection->shouldReceive('has')
143
                   ->with('UIDSignature')
144
                   ->andReturn(true);
145
        $collection->shouldReceive('has')
146
                   ->with('signatureTimestamp')
147
                   ->andReturn(true);
148
        $collection->shouldReceive('get')
149
                   ->with('UID')
150
                   ->andReturn('some_uid');
151
        $collection->shouldReceive('get')
152
                   ->with('UIDSignature')
153
                   ->andReturn('some_signature');
154
        $collection->shouldReceive('get')
155
                   ->with('signatureTimestamp')
156
                   ->andReturn(12345667);
157
        static::assertTrue($this->validator->canValidate($response));
158
159
        $this->signature->shouldReceive('checkTimestamp')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\Gigya\Validation\Signature.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
160
                        ->with(12345667)
161
                        ->andReturn(false);
162
        static::assertFalse($this->validator->validate($response));
163
164
        $response->shouldReceive('getErrorCode')
165
                 ->andReturn(0);
166
167
        static::setExpectedException(InvalidTimestampException::class);
168
        $this->validator->assert($response);
169
    }
170
171
    public function testCanValidationReturnsFalseWhenTheRequiredFieldsAreNotPresent()
172
    {
173
        $response   = m::mock(ResponseInterface::class);
174
        $collection = m::mock(Collection::class);
175
        $response->shouldReceive('getData')
176
                 ->andReturn($collection);
177
        $collection->shouldReceive('has')
178
                   ->with('UID')
179
                   ->andReturn(true, true, true, false);
180
        $collection->shouldReceive('has')
181
                   ->with('UIDSignature')
182
                   ->andReturn(true, true, false, true);
183
        $collection->shouldReceive('has')
184
                   ->with('signatureTimestamp')
185
                   ->andReturn(true, false, true, true);
186
        static::assertTrue($this->validator->canValidate($response));
187
        static::assertFalse($this->validator->canValidate($response));
188
        static::assertFalse($this->validator->canValidate($response));
189
        static::assertFalse($this->validator->canValidate($response));
190
    }
191
}
192