ClientTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 170
Duplicated Lines 60.59 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 7
dl 103
loc 170
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B testOk() 0 42 1
A testCurlError() 0 17 1
A testMalformedJsonError() 21 21 1
B testMalformedDataError1() 25 25 1
B testMalformedDataError2() 26 26 1
B testServerErrorException() 31 31 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AlexeyKuperhstokh\LocationBundle\Tests\Client;
4
5
use AlexeyKuperhstokh\LocationBundle\Client\Client;
6
use GuzzleHttp\Client as GuzzleClient;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Handler\MockHandler;
9
use GuzzleHttp\HandlerStack;
10
use GuzzleHttp\Psr7\Request;
11
use GuzzleHttp\Psr7\Response;
12
use GuzzleHttp\Psr7\Uri;
13
14
class ClientTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testOk()
17
    {
18
        $validResponse = <<<JSON
19
{
20
    "data": {
21
        "locations": [
22
            {
23
                "name": "Eiffel Tower",
24
                "coordinates": {
25
                    "lat": 21.12,
26
                    "long": 19.56
27
                }
28
            },
29
            {
30
                "name": "Novosibirsk",
31
                "coordinates": {
32
                    "lat": 54.9700492,
33
                    "long": 82.6692266
34
                }            
35
            }
36
        ]
37
    },
38
    "success": true
39
}
40
JSON;
41
        
42
        $mockRequests = [
43
            new Response(200, ['Content-type' => 'application/json'], $validResponse),
44
        ];
45
        $mockHandler = new MockHandler($mockRequests);
46
        $handler = HandlerStack::create($mockHandler);
47
        $httpClient = new GuzzleClient(['handler' => $handler]);
48
49
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
50
51
        $client = new Client($httpClient, $request);
52
53
        $locations = $client->getLocations();
54
        $this->assertCount(2, $locations);
55
        $this->assertInstanceOf('AlexeyKuperhstokh\LocationBundle\Location\Location', $locations[0]);
56
        $this->assertInstanceOf('AlexeyKuperhstokh\LocationBundle\Location\Location', $locations[1]);
57
    }
58
59
    public function testCurlError()
60
    {
61
        $mockRequests = [
62
            new Response(404),
63
        ];
64
        $mockHandler = new MockHandler($mockRequests);
65
        $handler = HandlerStack::create($mockHandler);
66
        $httpClient = new GuzzleClient(['handler' => $handler]);
67
68
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
69
70
        $client = new Client($httpClient, $request);
71
72
        $this->setExpectedException('GuzzleHttp\Exception\ClientException');
73
74
        $client->getLocations();
75
    }
76
77 View Code Duplication
    public function testMalformedJsonError()
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...
78
    {
79
        $invalidResponse = <<<JSON
80
{{{{{{{
81
JSON;
82
83
        $mockRequests = [
84
            new Response(200, ['Content-type' => 'application/json'], $invalidResponse),
85
        ];
86
        $mockHandler = new MockHandler($mockRequests);
87
        $handler = HandlerStack::create($mockHandler);
88
        $httpClient = new GuzzleClient(['handler' => $handler]);
89
90
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
91
92
        $client = new Client($httpClient, $request);
93
94
        $this->setExpectedException('Symfony\Component\Serializer\Exception\UnexpectedValueException');
95
96
        $client->getLocations();
97
    }
98
99 View Code Duplication
    public function testMalformedDataError1()
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...
100
    {
101
        $validResponse = <<<JSON
102
{
103
    "data": {
104
        "locations": []
105
    }
106
}
107
JSON;
108
109
        $mockRequests = [
110
            new Response(200, ['Content-type' => 'application/json'], $validResponse),
111
        ];
112
        $mockHandler = new MockHandler($mockRequests);
113
        $handler = HandlerStack::create($mockHandler);
114
        $httpClient = new GuzzleClient(['handler' => $handler]);
115
116
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
117
118
        $client = new Client($httpClient, $request);
119
120
        $this->setExpectedException('AlexeyKuperhstokh\LocationBundle\Exceptions\MalformedDataException');
121
122
        $client->getLocations();
123
    }
124
125 View Code Duplication
    public function testMalformedDataError2()
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...
126
    {
127
        $validResponse = <<<JSON
128
{
129
    "data": {
130
        "locations": 1
131
    },
132
    "success": true
133
}
134
JSON;
135
136
        $mockRequests = [
137
            new Response(200, ['Content-type' => 'application/json'], $validResponse),
138
        ];
139
        $mockHandler = new MockHandler($mockRequests);
140
        $handler = HandlerStack::create($mockHandler);
141
        $httpClient = new GuzzleClient(['handler' => $handler]);
142
143
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
144
145
        $client = new Client($httpClient, $request);
146
147
        $this->setExpectedException('AlexeyKuperhstokh\LocationBundle\Exceptions\MalformedDataException');
148
149
        $client->getLocations();
150
    }
151
152 View Code Duplication
    public function testServerErrorException()
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...
153
    {
154
        $validResponse = <<<JSON
155
{
156
    "data": {
157
        "message": "testMessage",
158
        "code": 100
159
    },
160
    "success": false
161
}
162
JSON;
163
164
        $mockRequests = [
165
            new Response(200, ['Content-type' => 'application/json'], $validResponse),
166
        ];
167
        $mockHandler = new MockHandler($mockRequests);
168
        $handler = HandlerStack::create($mockHandler);
169
        $httpClient = new GuzzleClient(['handler' => $handler]);
170
171
        $request = new Request('GET', 'http://localhost/', array('Accept' => 'application/json'));
172
173
        $client = new Client($httpClient, $request);
174
175
        $this->setExpectedException(
176
            'AlexeyKuperhstokh\LocationBundle\Exceptions\ServerErrorException',
177
            'testMessage',
178
            100
179
        );
180
181
        $client->getLocations();
182
    }
183
}
184