ResponseTest::testResponseIsNotSuccessful()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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