Passed
Pull Request — develop (#19)
by Paulius
02:32
created

ClientTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 42
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Dokobit\Gateway\Tests;
3
4
use Dokobit\Gateway\Client;
5
6
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dokobit\Gateway\Tests\TestCase. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
class ClientTest extends TestCase
9
{
10
    /** @var Dokobit\Gateway\Query\QueryInterface */
0 ignored issues
show
Bug introduced by
The type Dokobit\Gateway\Tests\Do...ay\Query\QueryInterface was not found. Did you mean Dokobit\Gateway\Query\QueryInterface? If so, make sure to prefix the type with \.
Loading history...
11
    private $methodStub;
12
13
    /** @var Dokobit\Gateway\Http\ClientInterface */
0 ignored issues
show
Bug introduced by
The type Dokobit\Gateway\Tests\Do...ay\Http\ClientInterface was not found. Did you mean Dokobit\Gateway\Http\ClientInterface? If so, make sure to prefix the type with \.
Loading history...
14
    private $clientStub;
15
16
    /** @var Dokobit\Gateway\ResponseMapperInterface */
0 ignored issues
show
Bug introduced by
The type Dokobit\Gateway\Tests\Do...ResponseMapperInterface was not found. Did you mean Dokobit\Gateway\ResponseMapperInterface? If so, make sure to prefix the type with \.
Loading history...
17
    private $responseMapperStub;
18
19
    /** @var Symfony\Component\Validator\Validator */
0 ignored issues
show
Bug introduced by
The type Dokobit\Gateway\Tests\Sy...ent\Validator\Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
    private $validatorStub;
21
22
    /** @var Client */
23
    private $client;
24
25
    protected function setUp(): void
26
    {
27
        $this->methodStub = $this->getMockBuilder('Dokobit\Gateway\Query\QueryInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Dokobit\Gateway\Tests\Do...ay\Query\QueryInterface of property $methodStub.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

27
        $this->methodStub = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('Dokobit\Gateway\Query\QueryInterface')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
28
            ->setMethods(['getAction', 'getMethod', 'getFields', 'createResult', 'getValidationConstraints'])
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
        $this->methodStub->method('getAction')
32
            ->willReturn('login');
33
        $this->methodStub->method('getMethod')
34
            ->willReturn('post');
35
        $this->methodStub->method('getFields')
36
            ->willReturn(['phone' => '+3706xxxxxxx', 'code' => 'xxxxxxxxxxx'])
37
        ;
38
39
40
        $this->clientStub = $this->getMockBuilder('Dokobit\Gateway\Http\ClientInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Dokobit\Gateway\Tests\Do...ay\Http\ClientInterface of property $clientStub.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

40
        $this->clientStub = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('Dokobit\Gateway\Http\ClientInterface')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
41
            ->setMethods(['requestJson', 'requestBody', 'sendRequest'])
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
45
        $this->responseMapperStub = $this->getMockBuilder('Dokobit\Gateway\ResponseMapperInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Dokobit\Gateway\Tests\Do...ResponseMapperInterface of property $responseMapperStub.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

45
        $this->responseMapperStub = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('Dokobit\Gateway\ResponseMapperInterface')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
46
            ->setMethods(['map'])
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
50
        $this->validatorStub = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Dokobit\Gateway\Tests\Sy...ent\Validator\Validator of property $validatorStub.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
            ->disableOriginalConstructor()
52
            // ->setMethods(['validateValue'])
53
            ->getMock();
54
55
        $this->client = new Client(
56
            $this->clientStub,
57
            $this->responseMapperStub,
58
            $this->validatorStub,
59
            ['apiKey' => 'xxx', 'sandbox' => true]
60
        );
61
    }
62
63
    public function testFactoryCreate()
64
    {
65
        $client = Client::create(['sandbox' => true, 'apiKey' => 'xxx']);
66
        $this->assertInstanceOf('Dokobit\Gateway\Client', $client);
67
        $this->assertTrue($client->isSandbox());
68
    }
69
70
    public function testFactoryCreateWithLogger()
71
    {
72
        $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')
73
            ->getMock()
74
        ;
75
        $client = Client::create(
76
            ['sandbox' => true, 'apiKey' => 'xxx'],
77
            $logger
78
        );
79
        $this->assertInstanceOf('Dokobit\Gateway\Client', $client);
80
    }
81
82
    public function testDefaultClientConfiguration()
83
    {
84
        $client = new Client(
85
            $this->clientStub,
86
            $this->responseMapperStub,
87
            $this->validatorStub,
88
            ['apiKey' => 'xxx']
89
        );
90
91
        $this->assertSame(false, $client->isSandbox());
92
        $this->assertSame('https://gateway.dokobit.com', $client->getUrl());
93
        $this->assertSame('https://gateway-sandbox.dokobit.com', $client->getSandboxUrl());
94
    }
95
96
    public function testCustomClientConfiguration()
97
    {
98
        $client = new Client(
99
            $this->clientStub,
100
            $this->responseMapperStub,
101
            $this->validatorStub,
102
            [
103
                'sandbox' => true,
104
                'apiKey' => 'l33t',
105
                'url' => 'https://custom-api.dokobit.com',
106
                'sandboxUrl' => 'https://custom-sandbox.dokobit.com',
107
            ]
108
        );
109
        $this->assertSame(true, $client->isSandbox());
110
        $this->assertSame('l33t', $client->getApiKey());
111
        $this->assertSame('https://custom-api.dokobit.com', $client->getUrl());
112
        $this->assertSame('https://custom-sandbox.dokobit.com', $client->getSandboxUrl());
113
    }
114
115
    public function testApiKeyRequired()
116
    {
117
        $this->expectException(\Dokobit\Gateway\Exception\InvalidApiKey::class);
118
        $client = new Client(
0 ignored issues
show
Unused Code introduced by
The assignment to $client is dead and can be removed.
Loading history...
119
            $this->clientStub,
120
            $this->responseMapperStub,
121
            $this->validatorStub
122
        );
123
    }
124
125
    public function testGetFullMethodUrlForProduction()
126
    {
127
        $client = new Client(
128
            $this->clientStub,
129
            $this->responseMapperStub,
130
            $this->validatorStub,
131
            ['apiKey' => 'xxxxxx']
132
        );
133
        $this->assertEquals(
134
            'https://gateway.dokobit.com/api/archive.json',
135
            $client->getFullMethodUrl('archive')
136
        );
137
    }
138
139
    public function testGetFullMethodUrlForSandbox()
140
    {
141
        $this->assertEquals(
142
            'https://gateway-sandbox.dokobit.com/api/archive.json',
143
            $this->client->getFullMethodUrl('archive')
144
        );
145
    }
146
147
    public function testGet()
148
    {
149
        $this->methodStub
150
            ->expects($this->once())
151
            ->method('createResult')
152
            ->willReturn(
153
                $this->getMockBuilder('Dokobit\Gateway\Result\ResultInterface')
154
                    ->disableOriginalConstructor()
155
                    ->getMock()
156
            )
157
        ;
158
        $this->methodStub
159
            ->expects($this->once())
160
            ->method('getMethod')
161
        ;
162
        $this->methodStub
163
            ->expects($this->once())
164
            ->method('getAction')
165
        ;
166
        $this->methodStub
167
            ->expects($this->once())
168
            ->method('getValidationConstraints')
169
        ;
170
171
        $this->responseMapperStub
172
            ->expects($this->once())
173
            ->method('map')
174
        ;
175
176
        $this->clientStub
177
            ->expects($this->once())
178
            ->method('requestJson')
179
            ->willReturn([])
180
        ;
181
182
        $this->validatorStub
183
            ->expects($this->once())
184
            ->method('validate')
185
            ->willReturn([])
186
        ;
187
188
        $this->client->get($this->methodStub);
189
    }
190
191
    /**
192
     * The client has a few methods to generate view URLs. This tests them.
193
     */
194
    public function testGetViewUrls()
195
    {
196
        $client = Client::create(['sandbox' => true, 'apiKey' => 'xxx']);
197
198
        $signingToken = 'MyToken123';
199
        $accessToken = 'MyAccessToken';
200
        $this->assertSame(
201
            'https://gateway-sandbox.dokobit.com/open/'.$signingToken,
202
            $client->getOpenUrl($signingToken)
203
        );
204
        $this->assertSame(
205
            'https://gateway-sandbox.dokobit.com/signing/'.$signingToken.'?access_token='.$accessToken,
206
            $client->getSigningUrl($signingToken, $accessToken)
207
        );
208
        $this->assertSame(
209
            'https://gateway-sandbox.dokobit.com/signing/batch/'.$signingToken,
210
            $client->getBatchSigningUrl($signingToken)
211
        );
212
        $this->assertSame(
213
            'https://gateway-sandbox.dokobit.com/signing/sequence/'.$signingToken,
214
            $client->getSequenceSigningUrl($signingToken)
215
        );
216
    }
217
218
    public function testGetValidationFailed()
219
    {
220
        $this->expectException(\Dokobit\Gateway\Exception\QueryValidator::class);
221
        $this->expectExceptionMessage('Query parameters validation failed');
222
        $violations = $this->getMockBuilder('Symfony\Component\Validator\ConstraintViolationList')
223
            ->disableOriginalConstructor()
224
            ->getMock();
225
        $violations
226
            ->method('count')
227
            ->willReturn(1)
228
        ;
229
230
        $this->validatorStub
231
            ->expects($this->once())
232
            ->method('validate')
233
            ->willReturn($violations)
234
        ;
235
236
        $this->client->get($this->methodStub);
237
    }
238
}
239