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

AbstractNetAcuityDatabaseTest::fetchUsingEdge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 9.597
c 0
b 0
f 0
cc 1
eloc 50
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TraderInteractive\NetAcuity\Databases\Tests;
4
5
use TraderInteractive\NetAcuity\Databases\EdgeDatabase;
6
use TraderInteractive\NetAcuity\Tests\NetAcuityTestSuite;
7
use Exception;
8
9
/**
10
 * @coversDefaultClass \TraderInteractive\NetAcuity\Databases\AbstractNetAcuityDatabase
11
 * @covers ::__construct
12
 * @covers ::<protected>
13
 * @covers ::<private>
14
 */
15
final class AbstractNetAcuityDatabaseTest extends NetAcuityTestSuite
16
{
17
    /**
18
     * @test
19
     * @covers ::fetch
20
     *
21
     * @return void
22
     */
23
    public function fetchUsingEdge()
24
    {
25
        $mockResponse = $this->getMockResponse(
26
            [
27
                'edge-country' => 'america/new_york',
28
                'edge-region' => 'usa',
29
                'edge-city' => 'radford',
30
                'edge-conn-speed' => 'broadband',
31
                'edge-metro-code' => '2',
32
                'edge-latitude' => '123.456',
33
                'edge-longitude' => '789.101',
34
                'edge-postal-code' => '24141',
35
                'edge-country-code' => '112',
36
                'edge-region-code' => '1314',
37
                'edge-city-code' => '1516',
38
                'edge-continent-code' => '1',
39
                'edge-two-letter-country' => 'US',
40
                'edge-area-codes' => '540',
41
                'edge-country-conf' => '30039',
42
                'edge-region-conf' => '33.9056',
43
                'edge-city-conf' => '99',
44
                'edge-postal-conf' => '99',
45
                'edge-gmt-offset' => '-500',
46
                'edge-in-dst' => 'n',
47
                'edge-timezone-name' => 't3',
48
            ]
49
        );
50
        $mockClient = $this->getMockGuzzleClient();
51
        $mockClient->method('send')->willReturn($mockResponse);
52
53
        $database = new EdgeDatabase($mockClient, 'a token');
0 ignored issues
show
Bug introduced by
It seems like $mockClient defined by $this->getMockGuzzleClient() on line 50 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TraderInteractive\NetAcu...Database::__construct() does only seem to accept object<GuzzleHttp\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
        $actual = $database->fetch('192.168.1.1');
55
56
        $expected = [
57
            'area-code' => '540',
58
            'city' => 'radford',
59
            'city-code' => '1516',
60
            'city-conf' => '99',
61
            'conn-speed' => 'broadband',
62
            'continent-code' => '1',
63
            'country' => 'america/new_york',
64
            'country-code' => '112',
65
            'country-conf' => '30039',
66
            'gmt-offset' => '-500',
67
            'in-dist' => 'n',
68
            'latitude' => '123.456',
69
            'longitude' => '789.101',
70
            'metro-code' => '2',
71
            'postal-conf' => '99',
72
            'region' => 'usa',
73
            'region-code' => '1314',
74
            'region-conf' => '33.9056',
75
            'timezone-name' => 't3',
76
            'two-letter-country' => 'US',
77
            'zip-code' => '24141',
78
        ];
79
80
        $this->assertSame($expected, $actual);
81
    }
82
83
    /**
84
     *
85
     * @test
86
     * @covers ::fetch
87
     */
88
    public function getGeoWithExtraFieldEdge()
89
    {
90
        $mockResponse = $this->getMockResponse(
91
            [
92
                'edge-country' => 'USA',
93
                'edge-region' => 'something',
94
                'edge-city' => 'reserved',
95
                'edge-conn-speed' => 'broadband',
96
                'edge-metro-code' => '2',
97
                'edge-latitude' => '123.456',
98
                'edge-longitude' => '789.101',
99
                'edge-postal-code' => '12345',
100
                'edge-country-code' => '112',
101
                'edge-region-code' => '1314',
102
                'edge-city-code' => '1516',
103
                'edge-continent-code' => '1',
104
                'edge-two-letter-country' => 'US',
105
                'edge-area-codes' => '123',
106
                'edge-country-conf' => '2',
107
                'edge-region-conf' => '3',
108
                'edge-city-conf' => '4',
109
                'edge-postal-conf' => '5',
110
                'edge-gmt-offset' => '6',
111
                'edge-in-dst' => '7',
112
                'edge-timezone-name' => 'UTC',
113
                'edge-extra' => 'erroneous',
114
            ]
115
        );
116
        $mockClient = $this->getMockGuzzleClient();
117
        $mockClient->method('send')->willReturn($mockResponse);
118
119
        $database = new EdgeDatabase($mockClient, 'a token');
0 ignored issues
show
Bug introduced by
It seems like $mockClient defined by $this->getMockGuzzleClient() on line 116 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TraderInteractive\NetAcu...Database::__construct() does only seem to accept object<GuzzleHttp\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
121
        $this->assertSame(
122
            [
123
                'area-code' => '123',
124
                'city' => 'reserved',
125
                'city-code' => '1516',
126
                'city-conf' => '4',
127
                'conn-speed' => 'broadband',
128
                'continent-code' => '1',
129
                'country' => 'USA',
130
                'country-code' => '112',
131
                'country-conf' => '2',
132
                'gmt-offset' => '6',
133
                'in-dist' => '7',
134
                'latitude' => '123.456',
135
                'longitude' => '789.101',
136
                'metro-code' => '2',
137
                'postal-conf' => '5',
138
                'region' => 'something',
139
                'region-code' => '1314',
140
                'region-conf' => '3',
141
                'timezone-name' => 'UTC',
142
                'two-letter-country' => 'US',
143
                'zip-code' => '12345',
144
            ],
145
            $database->fetch('1.2.3.4')
146
        );
147
    }
148
149
    /**
150
     * Verify that ip address must be a string.
151
     *
152
     * @test
153
     * @covers ::fetch
154
     * @expectedException Exception
155
     * @expectedExceptionMessage NetAcuity API rejected the request, Reason: Invalid IP (1)
156
     * @expectedExceptionCode 400
157
     */
158 View Code Duplication
    public function getGeoNonStringIp()
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...
159
    {
160
        $mockException = $this->getMockClientException(400, 'Invalid IP (1)');
161
        $mockClient = $this->getMockGuzzleClient();
162
        $mockClient->method('send')->will($this->throwException($mockException));
163
164
        $database = new EdgeDatabase($mockClient, 'a token');
0 ignored issues
show
Bug introduced by
It seems like $mockClient defined by $this->getMockGuzzleClient() on line 161 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TraderInteractive\NetAcu...Database::__construct() does only seem to accept object<GuzzleHttp\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
165
        $database->fetch(1);
166
    }
167
168
    /**
169
     * @test
170
     *
171
     * @covers ::fetch
172
     *
173
     * @expectedException Exception
174
     * @expectedExceptionMessage NetAcuity API rejected the provided api user token.
175
     * @expectedExceptionCode 403
176
     */
177 View Code Duplication
    public function netAcuityUserTokenInvalid()
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...
178
    {
179
        $mockException = $this->getMockClientException(403, 'Invalid IP (1)');
180
        $mockClient = $this->getMockGuzzleClient();
181
        $mockClient->method('send')->will($this->throwException($mockException));
182
183
        $database = new EdgeDatabase($mockClient, 'a token');
0 ignored issues
show
Bug introduced by
It seems like $mockClient defined by $this->getMockGuzzleClient() on line 180 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TraderInteractive\NetAcu...Database::__construct() does only seem to accept object<GuzzleHttp\ClientInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
184
        $database->fetch('127.0.0.1');
185
    }
186
}
187