Completed
Push — develop ( fe8c75...3bcd98 )
by Tom
02:26
created

testGetClassifiersVerbose()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message;
4
5
use Bobbyshaw\WatsonVisualRecognition\Client;
6
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifiersResponse;
7
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifiersRequest;
8
use Bobbyshaw\WatsonVisualRecognition\Tests\Base;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Uri;
11
12
/**
13
 * Class GetClassifiersRequestTest
14
 * @package Bobbyshaw\WatsonVisualRecognition\Tests\Message
15
 */
16
class GetClassifiersRequestTest extends Base
17
{
18
    /** @var Client */
19
    private $client;
20
    private $config;
21
    private $username = 'username';
22
    private $password = 'password';
23
    private $version = '2015-12-02';
24
25 View Code Duplication
    public function setUp()
1 ignored issue
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...
26
    {
27
        $this->config = array(
28
            'username' => $this->username,
29
            'password' => $this->password,
30
            'version' => $this->version
31
        );
32
33
        $this->client = new Client();
34
        $this->client->initialize($this->config);
35
    }
36
37
    /**
38
     * Test that params are as expected
39
     */
40
    public function testGetData()
41
    {
42
        /** @var GetClassifiersRequest $request */
43
        $request = $this->client->getClassifiers();
44
45
        $this->assertEquals($this->config, $request->getData());
46
    }
47
48
    /**
49
     * Test the getClassifiers function HTTP request and response
50
     */
51
    public function testGetClassifiers()
52
    {
53
        $container = [];
54
        $guzzle = $this->getMockHttpClientWithHistoryAndResponses(
55
            $container,
56
            [$this->getMockHttpResponse('GetClassifiersSuccess.txt')]
57
        );
58
59
        $this->client = new Client($guzzle);
60
        $this->client->initialize(['username' => $this->username, 'password' => $this->password]);
61
62
        /** @var GetClassifiersRequest $request */
63
        $classifiersRequest = $this->client->getClassifiers();
64
65
        $response = $classifiersRequest->send();
66
        $response->getClassifiers();
67
68
69
        // One request should be sent
70
        $this->assertCount(1, $container);
71
72
        $transaction = $container[0];
73
74
        /** @var Request $request */
75
        $request = $transaction['request'];
76
77
        /** @var Uri $uri */
78
        $uri = $request->getUri();
79
80
        // Check method
81
        $this->assertEquals('GET', $request->getMethod());
82
83
        // Check we're talking to the right host
84
        $this->assertEquals('https', $uri->getScheme());
85
        $this->assertEquals('gateway.watsonplatform.net', $uri->getHost());
86
87
        // Check version query parameter
88
        $query = \GuzzleHttp\Psr7\parse_query($uri->getQuery());
89
        $this->assertArrayHasKey('version', $query);
90
91
        // Check path
92
        $this->assertEquals('/visual-recognition-beta/api/v2/classifiers/', $uri->getPath());
93
94
        // Check basic auth
95
        $auth = $request->getHeader('Authorization');
96
        $this->assertEquals('Basic ' . base64_encode($this->username . ':' . $this->password), $auth[0]);
97
98
        $this->assertEquals($response, $classifiersRequest->getResponse());
99
    }
100
101
    /**
102
     * Test getClassifiers failed auth is handled appropriately
103
     *
104
     * @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException
105
     */
106 View Code Duplication
    public function testGetClassifiersFailedAuthResponse()
1 ignored issue
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...
107
    {
108
        $container = [];
109
        $response = $this->getMockHttpResponse('FailedAuth.txt', 401);
110
        $httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]);
111
112
        $this->client = new Client($httpClient);
113
        $this->client->initialize(['username' => $this->username, 'password' => $this->password]);
114
115
        /** @var GetClassifiersRequest $request */
116
        $request = $this->client->getClassifiers();
117
        $request->send();
118
    }
119
120
    public function testGetClassifiersVerbose()
121
    {
122
        $container = [];
123
        $guzzle = $this->getMockHttpClientWithHistoryAndResponses(
124
            $container,
125
            [$this->getMockHttpResponse('GetClassifiersSuccess.txt')]
126
        );
127
128
        $this->client = new Client($guzzle);
129
        $this->client->initialize(['username' => $this->username, 'password' => $this->password]);
130
131
        /** @var GetClassifiersRequest $request */
132
        $classifiersRequest = $this->client->getClassifiers(['verbose' => 'true']);
133
134
        /** @var ClassifiersResponse $response */
135
        $response = $classifiersRequest->send();
136
        $response->getClassifiers();
137
138
        $transaction = $container[0];
139
140
        /** @var Request $request */
141
        $request = $transaction['request'];
142
143
        /** @var Uri $uri */
144
        $uri = $request->getUri();
145
146
        // Check verbose query parameter
147
        $query = \GuzzleHttp\Psr7\parse_query($uri->getQuery());
148
149
        $this->assertArrayHasKey('verbose', $query);
150
    }
151
}
152