ResponseTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 34.41 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 32
loc 93
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testResponseIsNotSuccessful() 0 4 1
A testErrorCode() 0 4 1
A testErrorMessage() 0 7 1
A testNoError() 0 10 1
A testInitialisingRequestAfterResponse() 16 16 1
A testSetUsernameOnRequestAfterResponse() 16 16 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 Bobbyshaw\WatsonVisualRecognition\Tests\Message;
4
5
use Bobbyshaw\WatsonVisualRecognition\Client;
6
use Bobbyshaw\WatsonVisualRecognition\Message\AbstractRequest;
7
use Bobbyshaw\WatsonVisualRecognition\Message\Response;
8
use Bobbyshaw\WatsonVisualRecognition\Tests\Base;
9
use GuzzleHttp\Client as HttpClient;
10
11
class ResponseTest extends Base
12
{
13
14
    /**
15
     * @var AbstractRequest
16
     */
17
    protected $request;
18
19
    /**
20
     * @var Response
21
     */
22
    protected $response;
23
24
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $data = file_get_contents('Tests/Mock/ErrorResponse.txt');
30
31
        $this->request = $this->getMockForAbstractClass(AbstractRequest::class, [new HttpClient()]);
32
        $this->response = new Response($this->request, $data);
33
    }
34
35
    public function testResponseIsNotSuccessful()
36
    {
37
        $this->assertEquals(false, $this->response->isSuccessful());
38
    }
39
40
    public function testErrorCode()
41
    {
42
        $this->assertEquals(400, $this->response->getErrorCode());
43
    }
44
45
    public function testErrorMessage()
46
    {
47
        $this->assertEquals(
48
            'Upload problem - check that all fields were specified and upload size was less than 100MB',
49
            $this->response->getErrorMessage()
50
        );
51
    }
52
53
    public function testNoError()
54
    {
55
        /** @var \GuzzleHttp\Psr7\Response $response */
56
        $response = $this->getMockHttpResponse('GetClassifiersSuccess.txt');
57
58
        $this->response = new Response($this->request, $response->getBody()->getContents());
59
60
        $this->assertEquals(false, $this->response->getErrorCode());
61
        $this->assertEquals('', $this->response->getErrorMessage());
62
    }
63
64
    /**
65
     * @expectedException \RuntimeException
66
     */
67 View Code Duplication
    public function testInitialisingRequestAfterResponse()
68
    {
69
        $container = [];
70
        $guzzle = $this->getMockHttpClientWithHistoryAndResponses(
71
            $container,
72
            [$this->getMockHttpResponse('GetClassifiersSuccess.txt')]
73
        );
74
75
        $client = new Client($guzzle);
76
        $client->initialize(['username' => 'test', 'password' => 'test']);
77
78
        $request = $client->getClassifiers();
79
        $request->send();
80
81
        $request->initialize();
82
    }
83
84
    /**
85
     * @expectedException \RuntimeException
86
     */
87 View Code Duplication
    public function testSetUsernameOnRequestAfterResponse()
88
    {
89
        $container = [];
90
        $guzzle = $this->getMockHttpClientWithHistoryAndResponses(
91
            $container,
92
            [$this->getMockHttpResponse('GetClassifiersSuccess.txt')]
93
        );
94
95
        $client = new Client($guzzle);
96
        $client->initialize(['username' => 'test', 'password' => 'test']);
97
98
        $request = $client->getClassifiers();
99
        $request->send();
100
101
        $request->setUsername('test2');
102
    }
103
}
104