UidSignatureValidatorTest::testInvalidTimestamp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 9.44
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\Response\ResponseInterface;
17
use Graze\Gigya\Test\TestCase;
18
use Graze\Gigya\Validation\ResponseValidatorInterface;
19
use Graze\Gigya\Validation\Signature;
20
use Graze\Gigya\Validation\UidSignatureValidator;
21
use Illuminate\Support\Collection;
22
use Mockery as m;
23
24
class UidSignatureValidatorTest extends TestCase
25
{
26
    const SECRET = '8j9h0g-opko;dk]=id0f[sjo';
27
28
    /** @var mixed */
29
    private $signature;
30
    /** @var UidSignatureValidator */
31
    private $validator;
32
33
    public function setUp()
34
    {
35
        $this->signature = m::mock(Signature::class);
36
        $this->validator = new UidSignatureValidator($this->signature, static::SECRET);
0 ignored issues
show
Bug introduced by
$this->signature of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Validation\Signature expected by parameter $signature of Graze\Gigya\Validation\U...alidator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        $this->validator = new UidSignatureValidator(/** @scrutinizer ignore-type */ $this->signature, static::SECRET);
Loading history...
37
    }
38
39
    public function tearDown()
40
    {
41
        $this->signature = $this->validator = null;
42
    }
43
44
    public function testInstanceOf()
45
    {
46
        static::assertInstanceOf(ResponseValidatorInterface::class, $this->validator);
47
    }
48
49
    public function testUidSignature()
50
    {
51
        $response = m::mock(ResponseInterface::class);
52
        $collection = m::mock(Collection::class);
53
        $response->shouldReceive('getData')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getData'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        $response->/** @scrutinizer ignore-call */ 
54
                   shouldReceive('getData')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
54
                 ->andReturn($collection);
55
        $collection->shouldReceive('has')
56
                   ->with('UID')
57
                   ->andReturn(true);
58
        $collection->shouldReceive('has')
59
                   ->with('UIDSignature')
60
                   ->andReturn(true);
61
        $collection->shouldReceive('has')
62
                   ->with('signatureTimestamp')
63
                   ->andReturn(true);
64
        $collection->shouldReceive('get')
65
                   ->with('UID')
66
                   ->andReturn('some_uid');
67
        $collection->shouldReceive('get')
68
                   ->with('UIDSignature')
69
                   ->andReturn('some_signature');
70
        $collection->shouldReceive('get')
71
                   ->with('signatureTimestamp')
72
                   ->andReturn(12345667);
73
        static::assertTrue($this->validator->canValidate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...alidator::canValidate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        static::assertTrue($this->validator->canValidate(/** @scrutinizer ignore-type */ $response));
Loading history...
74
75
        $this->signature->shouldReceive('checkTimestamp')
76
                        ->with(12345667)
77
                        ->andReturn(true);
78
        $this->signature->shouldReceive('getUidSignature')
79
                        ->with('some_uid', 12345667, static::SECRET)
80
                        ->andReturn('some_signature');
81
        static::assertTrue($this->validator->validate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...reValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        static::assertTrue($this->validator->validate(/** @scrutinizer ignore-type */ $response));
Loading history...
82
    }
83
84
    /**
85
     * @expectedException \Graze\Gigya\Exception\InvalidUidSignatureException
86
     */
87
    public function testInvalidUidSignature()
88
    {
89
        $response = m::mock(ResponseInterface::class);
90
        $collection = m::mock(Collection::class);
91
        $response->shouldReceive('getData')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getData'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $response->/** @scrutinizer ignore-call */ 
92
                   shouldReceive('getData')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
92
                 ->andReturn($collection);
93
        $collection->shouldReceive('has')
94
                   ->with('UID')
95
                   ->andReturn(true);
96
        $collection->shouldReceive('has')
97
                   ->with('UIDSignature')
98
                   ->andReturn(true);
99
        $collection->shouldReceive('has')
100
                   ->with('signatureTimestamp')
101
                   ->andReturn(true);
102
        $collection->shouldReceive('get')
103
                   ->with('UID')
104
                   ->andReturn('some_uid');
105
        $collection->shouldReceive('get')
106
                   ->with('UIDSignature')
107
                   ->andReturn('some_signature');
108
        $collection->shouldReceive('get')
109
                   ->with('signatureTimestamp')
110
                   ->andReturn(12345667);
111
        static::assertTrue($this->validator->canValidate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...alidator::canValidate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        static::assertTrue($this->validator->canValidate(/** @scrutinizer ignore-type */ $response));
Loading history...
112
113
        $this->signature->shouldReceive('checkTimestamp')
114
                        ->with(12345667)
115
                        ->andReturn(true);
116
        $this->signature->shouldReceive('getUidSignature')
117
                        ->with('some_uid', 12345667, static::SECRET)
118
                        ->andReturn('incorrect_signature');
119
        static::assertFalse($this->validator->validate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...reValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        static::assertFalse($this->validator->validate(/** @scrutinizer ignore-type */ $response));
Loading history...
120
121
        $response->shouldReceive('getErrorCode')
122
                 ->andReturn(0);
123
124
        $this->validator->assert($response);
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...tureValidator::assert(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        $this->validator->assert(/** @scrutinizer ignore-type */ $response);
Loading history...
125
    }
126
127
    /**
128
     * @expectedException \Graze\Gigya\Exception\InvalidTimestampException
129
     */
130
    public function testInvalidTimestamp()
131
    {
132
        $response = m::mock(ResponseInterface::class);
133
        $collection = m::mock(Collection::class);
134
        $response->shouldReceive('getData')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getData'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        $response->/** @scrutinizer ignore-call */ 
135
                   shouldReceive('getData')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
135
                 ->andReturn($collection);
136
        $collection->shouldReceive('has')
137
                   ->with('UID')
138
                   ->andReturn(true);
139
        $collection->shouldReceive('has')
140
                   ->with('UIDSignature')
141
                   ->andReturn(true);
142
        $collection->shouldReceive('has')
143
                   ->with('signatureTimestamp')
144
                   ->andReturn(true);
145
        $collection->shouldReceive('get')
146
                   ->with('UID')
147
                   ->andReturn('some_uid');
148
        $collection->shouldReceive('get')
149
                   ->with('UIDSignature')
150
                   ->andReturn('some_signature');
151
        $collection->shouldReceive('get')
152
                   ->with('signatureTimestamp')
153
                   ->andReturn(12345667);
154
        static::assertTrue($this->validator->canValidate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...alidator::canValidate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
        static::assertTrue($this->validator->canValidate(/** @scrutinizer ignore-type */ $response));
Loading history...
155
156
        $this->signature->shouldReceive('checkTimestamp')
157
                        ->with(12345667)
158
                        ->andReturn(false);
159
        static::assertFalse($this->validator->validate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...reValidator::validate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        static::assertFalse($this->validator->validate(/** @scrutinizer ignore-type */ $response));
Loading history...
160
161
        $response->shouldReceive('getErrorCode')
162
                 ->andReturn(0);
163
164
        $this->validator->assert($response);
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...tureValidator::assert(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        $this->validator->assert(/** @scrutinizer ignore-type */ $response);
Loading history...
165
    }
166
167
    public function testCanValidationReturnsFalseWhenTheRequiredFieldsAreNotPresent()
168
    {
169
        $response = m::mock(ResponseInterface::class);
170
        $collection = m::mock(Collection::class);
171
        $response->shouldReceive('getData')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'getData'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
        $response->/** @scrutinizer ignore-call */ 
172
                   shouldReceive('getData')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
172
                 ->andReturn($collection);
173
        $collection->shouldReceive('has')
174
                   ->with('UID')
175
                   ->andReturn(true, true, true, false);
176
        $collection->shouldReceive('has')
177
                   ->with('UIDSignature')
178
                   ->andReturn(true, true, false, true);
179
        $collection->shouldReceive('has')
180
                   ->with('signatureTimestamp')
181
                   ->andReturn(true, false, true, true);
182
        static::assertTrue($this->validator->canValidate($response));
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type Graze\Gigya\Response\ResponseInterface expected by parameter $response of Graze\Gigya\Validation\U...alidator::canValidate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
        static::assertTrue($this->validator->canValidate(/** @scrutinizer ignore-type */ $response));
Loading history...
183
        static::assertFalse($this->validator->canValidate($response));
184
        static::assertFalse($this->validator->canValidate($response));
185
        static::assertFalse($this->validator->canValidate($response));
186
    }
187
}
188